Package student.micro.battleship
Class TestableBoard
java.lang.Object
student.micro.battleship.Board
student.micro.battleship.TestableBoard
A special subclass of
Board
that adds extra features useful
in writing test cases.-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Update the board as if its owner had played the specified move (of any kind).fireAt
(int x, int y) Update the board as if a shot were fired at the specified position.void
firePattern
(String... layout) Fire at a series of cells, using the layout contained in the series of parameter strings.void
Place the player's ship of the specified type at the given position with the specified orientation.void
placeShips
(String... layout) Place the player's ships all on the board using a "textual" grid layout contained in the provided series of strings, one per row.void
By default, an opponent's ships are hidden (seeBoard.hideShips()
), but this method will reveal the positions of any ships occupying cells that have not yet been fired on for testing purposes.Methods inherited from class student.micro.battleship.Board
areAllShipsSunk, canFireAt, equals, getLastAttackX, getLastAttackY, getLastShipSunk, getStatusAt, getSunkenShips, hashCode, hideShips, lastAttackSunkAShip, toString
-
Constructor Details
-
TestableBoard
public TestableBoard()Create a new, empty board.
-
-
Method Details
-
showShips
public void showShips()By default, an opponent's ships are hidden (seeBoard.hideShips()
), but this method will reveal the positions of any ships occupying cells that have not yet been fired on for testing purposes. -
applyMove
Update the board as if its owner had played the specified move (of any kind).- Parameters:
move
- The move to play on the current board.
-
fireAt
Update the board as if a shot were fired at the specified position. This is the same as applying aCallShotMove
from the opponent of this board.- Parameters:
x
- The x-coordinate of the position to fire at.y
- The y-coordinate of the position to fire at.- Returns:
- The new status of the cell at the specified position, after firing (either a HIT or a MISS).
-
firePattern
Fire at a series of cells, using the layout contained in the series of parameter strings. The series of strings 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 that is unchanged.
- '*' represents a space that will be fired on.
- Parameters:
layout
- A series of strings (one per row) representing the placement of all shots fired.
-
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.
-
placeShips
Place the player's ships all on the board using a "textual" grid layout contained in the provided series of strings, one 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.- Throws:
IllegalArgumentException
- If the specified ship placement would cause this ship to extend out of bounds, or would cause it to overlap another ship.
-