Class BattleshipState

java.lang.Object
  extended by BattleshipState

public class BattleshipState
extends java.lang.Object

BattleshipState.java Created: Mon Mar 31 08:09:18 2003


Constructor Summary
BattleshipState()
          Creates a new BattleshipState instance with size 10.
BattleshipState(int size)
          Creates a new BattleshipState instance with given size.
 
Method Summary
 int getSize()
          getSize - return the size of the grid.
 boolean hasShip(int row, int col)
          hasShip - return whether or there is a ship at the given row and column
 boolean isGuessed(int row, int col)
          isGuessed - return whether or not a given row or column has been guessed
 boolean isWon()
          isWon - returns whether or not all ship segment positions have been guessed.
 boolean makeGuess(int row, int col)
          makeGuess - mark position as guessed and return whether or not there is a ship segment at that location.
 void placeShip(int rowMin, int colMin, int rowMax, int colMax)
          placeShip - place a ship at all positions in the rectangle defined by the corners (rowMin, colMin) and (rowMax, colMax)
 void setPlaying(boolean playing)
          setPlaying - set whether or not the state is in "playing" mode, affecting output of toString
 java.lang.String toString()
          toString - return a String representation of the current state grid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BattleshipState

public BattleshipState()
Creates a new BattleshipState instance with size 10.


BattleshipState

public BattleshipState(int size)
Creates a new BattleshipState instance with given size.

Parameters:
size - an int value - number of rows/cols
Method Detail

getSize

public int getSize()
getSize - return the size of the grid.

Returns:
an int value - grid size = number of rows = number of columns

placeShip

public void placeShip(int rowMin,
                      int colMin,
                      int rowMax,
                      int colMax)
placeShip - place a ship at all positions in the rectangle defined by the corners (rowMin, colMin) and (rowMax, colMax)

Parameters:
rowMin - an int value - minimum row
colMin - an int value - minimum column
rowMax - an int value - maximum row
colMax - an int value - maximum column

makeGuess

public boolean makeGuess(int row,
                         int col)
makeGuess - mark position as guessed and return whether or not there is a ship segment at that location.

Parameters:
row - an int value - row of guess
col - an int value - column of guess
Returns:
a boolean value - whether or not guess is correct

hasShip

public boolean hasShip(int row,
                       int col)
hasShip - return whether or there is a ship at the given row and column

Parameters:
row - an int value - row to check
col - an int value - column to check
Returns:
a boolean value - whether or not there is a ship at the given row and column

isGuessed

public boolean isGuessed(int row,
                         int col)
isGuessed - return whether or not a given row or column has been guessed

Parameters:
row - an int value - row to check
col - an int value - column to check
Returns:
a boolean value - whether or not a given row and column has been guessed

isWon

public boolean isWon()
isWon - returns whether or not all ship segment positions have been guessed.

Returns:
a boolean value - whether or not all ship segment positions have been guessed.

setPlaying

public void setPlaying(boolean playing)
setPlaying - set whether or not the state is in "playing" mode, affecting output of toString

Parameters:
playing - a boolean value - whether or not the state is in playing mode

toString

public java.lang.String toString()
toString - return a String representation of the current state grid. A grid position with/without a ship is denoted as '+'/'-'. If the state is in playing mode and the position has not yet been guessed, the grid position is denoted '?'.

Overrides:
toString in class java.lang.Object
Returns:
a String value