Class Grid

java.lang.Object
  extended by Grid

public class Grid
extends java.lang.Object

This class models a game board. It is multi-purpose, in that the same object can present different "views" -- one to the player who has placed the ships, and one to her opponent, who only sees the results of strikes. Note that some of the methods in this class will need to update save-game state -- in particular, place() and registerStrike().


Constructor Summary
Grid()
           
 
Method Summary
 boolean canPlace(Ship s)
          Returns true if the given Ship can be legally placed on the board, false otherwise.
 java.lang.String explainIllegalPlacement(Ship s)
          Returns a textual explanation for why a given Ship cannot legally be placed on the board.
 Ship getLastStruck()
          Returns a reference to the Ship hit by the last strike, or null if the last strike missed all ships.
 Ship[] getLiveShips()
          Returns a newly-allocated array of references to the ships on this grid that have not yet been destroyed.
 int getNumRows()
          Returns the number of rows in this Grid.
 Ship[] getSunkenShips()
          Returns a newly-allocated array of references to the ships on this grid that have been destroyed.
 void place(Ship s)
          Places a given Ship on the board.
 boolean registerStrike(BoardPoint strike)
          Registers a strike on this grid.
 void setPlayerView()
          Sets this Grid to "player view mode." In player view mode, the positions of ships and the results of strikes are shown.
 void setResultsView()
          Sets this Grid to "results view mode." In results view mode, only the results of strikes are shown.
 java.lang.String stringForRow(int row)
          Returns a textual representation of the given row.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Grid

public Grid()
Method Detail

canPlace

public boolean canPlace(Ship s)
Returns true if the given Ship can be legally placed on the board, false otherwise.

Parameters:
s - a Ship.
Returns:
true if the given Ship can be legally placed on the board, false otherwise.

explainIllegalPlacement

public java.lang.String explainIllegalPlacement(Ship s)
Returns a textual explanation for why a given Ship cannot legally be placed on the board. Examples include Precondition: canPlace(s) returns false.

Parameters:
s - a Ship
Returns:
a String explaining why s cannot be placed on the board legally

place

public void place(Ship s)
Places a given Ship on the board. Precondition: canPlace(s) returns true.

Parameters:
s - a Ship.

getLiveShips

public Ship[] getLiveShips()
Returns a newly-allocated array of references to the ships on this grid that have not yet been destroyed.

Returns:
a newly-allocated array of references to the ships on this grid that have not yet been destroyed.

getSunkenShips

public Ship[] getSunkenShips()
Returns a newly-allocated array of references to the ships on this grid that have been destroyed.

Returns:
a newly-allocated array of references to the ships on this grid that have been destroyed.

registerStrike

public boolean registerStrike(BoardPoint strike)
Registers a strike on this grid. If the strike hit a Ship, update its status. If the strike destroyed a Ship, register it as a sunken ship.

Parameters:
strike - a BoardPoint
Returns:
true if the strike hit a Ship

getLastStruck

public Ship getLastStruck()
Returns a reference to the Ship hit by the last strike, or null if the last strike missed all ships.

Returns:
a reference to the Ship hit by the last strike, or null if the last strike missed all ships.

setPlayerView

public void setPlayerView()
Sets this Grid to "player view mode." In player view mode, the positions of ships and the results of strikes are shown.


setResultsView

public void setResultsView()
Sets this Grid to "results view mode." In results view mode, only the results of strikes are shown. (You will set the Grid corresponding to the computer player's board to results view mode, so the human player can't see the locations of the computer player's Ships.)


stringForRow

public java.lang.String stringForRow(int row)
Returns a textual representation of the given row. See the image of the status report in the project description for an example. If this Grid is in player view mode, this representation will include the representations of all Ships on the Grid as well as the results of strikes -- both hits and misses. If this Grid is in results view mode, only the results of strikes will be visible in the textual representation.

Parameters:
row - the row to convert to a String
Returns:

getNumRows

public int getNumRows()
Returns the number of rows in this Grid.

Returns:
the number of rows in this Grid