问题描述
/**
Amazon is building a multi-player game inspired by Battleship. Battleship is a guessing game for two players. Each player owns one 10x10 grids and a fleet of ships of different lengths. On one grid the player arranges ships and records the shots by the opponent. On the other grid the player records his own shots.
Before the game begins, each player secretly arranges their ships on their main grid. Each ship occupies a number of consecutive squares on the grid, arranged either horizontally or vertically. The number of squares for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). The types and numbers of ships allowed are the same for each player.
After the ships have been positioned, the game proceeds in a series of rounds. In each round, each player takes a turn to announce a target square in the opponent's main grid which is to be shot at. The opponent announces whether or not the square is occupied by a ship, and if it is a "hit" the player marks this on his secondary grid.
When all of the squares of a ship have been hit, the ship is sunk. If all of a player's ships have been sunk, the game is over and their opponent wins
Game consists of two phases:
1. Setup: game gets created, players place their ships
2. Play: Each player takes a turn to shoot a missile. Repeat this step until there's no ship left for either of the players.
Rules
- Size of the board is 10x10
- Each player gets to place 5 ships (1 of each type) which may be placed vertically/horizontally
- Ship types are:
- Destroyer (2 squares)
- Submarine (3 squares)
- Cruiser (3 squares)
- Battleship (4 squares)
- Aircraft Carrier (5 squares)
- All the grids must be hit to sink the ship
Problem Statement:
We want you to design and code the APIs and objects required for 2 people to play the game against each other (setup APIs are excluded).
*
*
* Player1 Player2
* 0 0 * * * * * * * *
* * * * * * * * 1 * *
* * * * * * * * * * *
* * * * * * * * * * 1
* * * * * * * * * * 1
*
* Game start:
*
* Round 1:
*
* player1: (0, 0) -> MISS
* player2: (0, 0) -> HIT
*
* Round 2:
* player1: (0, 1) -> MISS
* player2: (0, 1) -> HIT
*
* Game end:
*
* Player 2 wins
*
* *//
现场Work
// 1. understand, clarify, edge cases:
- board class
// 2. solutions:
// - ships class
// - player class
//
// -
// - Game class:
- run game
- players
- boards
- ships
- shoot(int x, int y);
- placeShip(board, ships, int x, int y, int dir)
- checkState(board)
class players {
// 0 ->
vector<vector<char>> board; // '*' is the empty board, '1' represnt ship occupied, '0' represent ship fallen
vetcor<Ships> shipLists; // max ships it can store is 5
public:
bool receive_hit(int x, int y); // return true if hits, false if not hit
bool placeShip( int x, int y, Ship ship, int dir) // place a ship inside our board, dir = 1 vertical, dir = 0 horizatoal, return true if placed succesffuly
bool isAlive() // check if we have exist ships on the board, return true if we no ships on board
// place a ship inside our board, dir = 1 vertical, dir = 0 horizatoal, return true if placed succesffuly
bool placeShip(int x, int y, Ship ship, int dir) {
if (x < 0 || x > board.size() || y < 0 || y > board[0].size() || board[x][y] != '*') return false;
if (dir = 1) {
if (y + Ship.size() > board.size()) return false
// check if ships out of board or if the place is occupied
for (int i = y; i < ship.size(); i++) {
if (board[x][i] != '*') return false;
else board[x][i] = '1';
}
} else {
}
}
};
abstract class Ship {
int getSize();
}
Enum Ship {
int size
}
// - Ship class:
- len
- Type type
// 3. Use Cases: