Class ShipPlacementMove

java.lang.Object
student.micro.battleship.Move
student.micro.battleship.ShipPlacementMove

public class ShipPlacementMove extends Move
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

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    boolean
    Get the specified ship associated with this placement move.
    Get list of the specified ships associated with this placement move.
    int
    boolean
    Determine whether this move is a valid one, according to the rules of the game.
    void
    placeShip(ShipType shipType, int x, int y, boolean orientedHorizontally)
    Place the player's ship of the specified type at the given position with the specified orientation.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ShipPlacementMove

      public ShipPlacementMove(GameState gameState)
      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

      public ShipPlacementMove(GameState gameState, String... layout)
      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

      public void placeShip(ShipType shipType, int x, int y, boolean orientedHorizontally)
      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

      public Ship getShip(ShipType type)
      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, a Ship 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

      public List<Ship> getShips()
      Get list of the specified ships associated with this placement move. The list will only include Ship 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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object