Class Board

java.lang.Object
student.micro.battleship.Board
Direct Known Subclasses:
TestableBoard

public class Board extends Object
Represents one player's board in the classic board game "Battleship". A game involves two boards--one for the player, and one for the opponent. One board consists of a 10x10 grid of cells that have one player's fleet placed on it, as well as all the hits and misses that have been fired by the player's opponent so far.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The height of a board (10 cells).
    static final int
    The width of a board (10 cells).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new, empty board.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Determine if all ships on this board have been sunk.
    boolean
    canFireAt(int x, int y)
    Determine if firing at the specified location would be a legal move at this Position in the game (i.e., if it has not been fired on before).
    boolean
    equals(Object other)
    int
    Get the x-coordinate of the position that was fired on in the previous turn.
    int
    Get the y-coordinate of the position that was fired on in the previous turn.
    Get the most recent ship that has been sunk on this board (no matter how long ago the most recent sinking occurred).
    getStatusAt(int x, int y)
    Get the status of a single cell on this board.
    Get a list of all ships sunk so far on this board.
    int
    void
    By default, ships are "visible" by calling getStatusAt() on cells that have not been fired on.
    boolean
    Determine if the last cell that was fired on caused a ship to be sunk.
    Generate a human-readable representation of this board, showing all of the hits (as X's) and misses (as O's).

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • WIDTH

      public static final int WIDTH
      The width of a board (10 cells).
      See Also:
    • HEIGHT

      public static final int HEIGHT
      The height of a board (10 cells).
      See Also:
  • Constructor Details

    • Board

      public Board()
      Create a new, empty board.
  • Method Details

    • getStatusAt

      public CellStatus getStatusAt(int x, int y)
      Get the status of a single cell on this board.
      Parameters:
      x - The x-coordinate (0-9) of the cell to inspect.
      y - The y-coordinate (0-9) of the cell to inspect.
      Returns:
      The status of the specified cell.
    • canFireAt

      public boolean canFireAt(int x, int y)
      Determine if firing at the specified location would be a legal move at this Position in the game (i.e., if it has not been fired on before).
      Parameters:
      x - The x-coordinate of the cell.
      y - The y-coordinate of the cell.
      Returns:
      True if the specified cell has not been fired on before, or false if it has been fired on already.
    • areAllShipsSunk

      public boolean areAllShipsSunk()
      Determine if all ships on this board have been sunk.
      Returns:
      True if every ship on this board has been sunk.
    • getSunkenShips

      public List<ShipType> getSunkenShips()
      Get a list of all ships sunk so far on this board.
      Returns:
      A list of the (types of) ships that have been sunk.
    • getLastShipSunk

      public ShipType getLastShipSunk()
      Get the most recent ship that has been sunk on this board (no matter how long ago the most recent sinking occurred).
      Returns:
      The ship that was sunk most recently.
    • getLastAttackX

      public int getLastAttackX()
      Get the x-coordinate of the position that was fired on in the previous turn.
      Returns:
      The x-coordinate of the cell that was fired on last, or 0 if no cells have ever been fired on (e.g., at the start of the game).
    • getLastAttackY

      public int getLastAttackY()
      Get the y-coordinate of the position that was fired on in the previous turn.
      Returns:
      The y-coordinate of the cell that was fired on last, or 0 if no cells have ever been fired on (e.g., at the start of the game).
    • lastAttackSunkAShip

      public boolean lastAttackSunkAShip()
      Determine if the last cell that was fired on caused a ship to be sunk. In other words, did the last turn firing on this board sink a ship?
      Returns:
      True if the last attack did sink a ship.
    • hideShips

      public void hideShips()
      By default, ships are "visible" by calling getStatusAt() on cells that have not been fired on. Calling this method will "hide" the presence of any ships in cells that have not yet been fired on, giving the "opponent's view" of this board (the opponent cannot see where the ships are placed--only where the hits and misses have happened).
    • equals

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

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

      public String toString()
      Generate a human-readable representation of this board, showing all of the hits (as X's) and misses (as O's). If the ships on the board are not hidden, their positions will also be shown.
      Overrides:
      toString in class Object
      Returns:
      A string containing a human-readable representation of this board, shown as a grid of cells in text form.