Package student.micro.battleship
Class ShipPlacementMove
java.lang.Object
student.micro.battleship.Move
student.micro.battleship.ShipPlacementMove
Represents a move that sets the positions of all the player's ships
on his or her board in the game "Battleship". Each player makes one
of these moves at the start of the game to position their ships.
In "devious" variations of Battleship, players may get an opportunity
to make these moves later to "reposition" their ships, within limits.
-
Constructor Summary
ConstructorsConstructorDescriptionShipPlacementMove
(GameState gameState) Creates a new move.ShipPlacementMove
(GameState gameState, String... layout) Creates a new move from a series of strings representing ship positions, one string per row. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Get the specified ship associated with this placement move.getShips()
Get list of the specified ships associated with this placement move.int
hashCode()
boolean
isValid()
Determine whether this move is a valid one, according to the rules of the game.void
Place the player's ship of the specified type at the given position with the specified orientation.toString()
-
Constructor Details
-
ShipPlacementMove
Creates a new move.- Parameters:
gameState
- The current state of the game, which includes this player's board and its current arrangement of ships, if any.
-
ShipPlacementMove
Creates a new move from a series of strings representing ship positions, one string per row. The strings together should represent a 10x10 character grid (newlines or spaces are OK, and optional) where each "cell" is represented by a single character:- '.' (a period) represents a space not occupied by a ship.
- 'C' represents a space occupied by the Carrier.
- 'B' represents a space occupied by the Battleship.
- 'D' represents a space occupied by the Destroyer.
- 'S' represents a space occupied by the Submarine.
- 'P' represents a space occupied by the Patrol Boat.
The layout can be provided as a series of strings (one string per row) separated by commas for convenience.
- Parameters:
gameState
- The current state of the game, which includes this player's board and its current arrangement of ships, if any.layout
- A series of strings (one per row) representing the placement of all ships.
-
-
Method Details
-
placeShip
Place the player's ship of the specified type at the given position with the specified orientation. The position is specified by giving the location of the bow of the ship. Ships can be laid out horizontally, to the right of the given position, or vertically, downward from the given position. The specified placement cannot cause this ship to extend past the edge of the game board, or cause it to lay on top of another one of this player's ships.- Parameters:
shipType
- The type of ship in your fleet that is being placed (or moved).x
- The x-coordinate of the new position for the ship's bow (its upper-leftmost location on the grid).y
- The y-coordinate of the new position for the ship's bow (its upper-leftmost location on the grid).orientedHorizontally
- If true, the ship extends horizontally to the right of the specified position. If false, the ship extends vertically down from the specified position.- Throws:
IllegalArgumentException
- If the specified ship placement would cause this ship to extend out of bounds, or would cause it to overlap another ship.
-
getShip
Get the specified ship associated with this placement move. If the given ship type has never been placed on the board, null will be returned instead. Otherwise, aShip
object indicating the given ship type's intended position as a result of this move will be returned.- Parameters:
type
- The type of ship to retrieve.- Returns:
- A
Ship
object representing the desired position of the specified ship type after this move (which may be unchanged, if the ship has already been placed on the board and no call to#placeShip()
has indicated it should be moved).
-
getShips
Get list of the specified ships associated with this placement move. The list will only includeShip
objects for the ships that have already been placed on the board, or the ships that will be added to the board as the result of this move.- Returns:
- A list of
Ship
objects representing the desired position of the entire fleet after this move.
-
isValid
public boolean isValid()Determine whether this move is a valid one, according to the rules of the game. A ship placement move is valid if all ships will be contained within the borders of the game board, and if no ships overlap. Further, any hits or misses already recorded on the game board due to the opponent's previous fired shots, if any, must still remain valid if this ship placement is applied to the board.- Returns:
- True if this ship placement is valid, or false if it is not.
-
equals
-
hashCode
public int hashCode() -
toString
-