Package student.micro.battleship
Class Board
java.lang.Object
student.micro.battleship.Board
- Direct Known Subclasses:
TestableBoard
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
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
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
hashCode()
void
By default, ships are "visible" by callinggetStatusAt()
on cells that have not been fired on.boolean
Determine if the last cell that was fired on caused a ship to be sunk.toString()
Generate a human-readable representation of this board, showing all of the hits (as X's) and misses (as O's).
-
Field Details
-
WIDTH
public static final int WIDTHThe width of a board (10 cells).- See Also:
-
HEIGHT
public static final int HEIGHTThe height of a board (10 cells).- See Also:
-
-
Constructor Details
-
Board
public Board()Create a new, empty board.
-
-
Method Details
-
getStatusAt
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
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
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 callinggetStatusAt()
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
-
hashCode
public int hashCode() -
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.
-