Class Board

java.lang.Object
  extended by Board

public class Board
extends java.lang.Object

Represents the board .


Field Summary
static int MISSED
          A state to represent one of several possible states for a Cell object
static int NOT_SHOT_AT
          A state to represent one of several possible states for a Cell object
static int SAME_TARGET
          A state to represent one of several possible states for a Cell object
static int SHIP_HIT
          A state to represent one of several possible states for a Cell object
static int SHIP_SUNK
          A state to represent one of several possible states for a Cell object
 
Constructor Summary
Board(int x, int y)
          This constructor for the Board object should initialize the board, setting its x and y dimensions to the given values and initializing the cells and shiplist to their initial states.
 
Method Summary
 boolean allShipsSunk()
          This method returns true if all ships on the board have been sunk, and it returns false otherwise.
 void clearBoard()
          This method intializes the cell array and ship list references.
 void generateRandomBoard(int numberOfShips)
          This method resets the board to a new state containing the number of ships given by the numberOfShips parameter.
 Cell getCellAt(int x, int y)
          Given the x and y coordinates of a cell in the board, this method returns a reference to the cell at that position (x,y).
 Ship getShip(int index)
          Given an integer index to a ship in the ship list, this method returns the ship at the requested index.
 int getShipNumber(Ship ship)
          Given a reference to a ship in the ship list, this method returns the integer index of the given ship in the list.
 boolean isValidBoard()
          This method returns true if the board configuration is valid, and it returns false otherwise.
 void placeShip(Ship aShip)
          This method places the given ship (aShip) on the board by marking each cell that the ship occupies (with a call to setShipOnCell) and then by adding the ship to the shipList.
 void placeShips(ShipList ships)
          This method takes a ShipList object as a parameter.
 void printShipList()
          Prints the ship list by calling the print method of the ship list.
 int processShot(int x, int y)
          This method processes a shot fired at cell x,y.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOT_SHOT_AT

public static final int NOT_SHOT_AT
A state to represent one of several possible states for a Cell object

See Also:
Constant Field Values

MISSED

public static final int MISSED
A state to represent one of several possible states for a Cell object

See Also:
Constant Field Values

SAME_TARGET

public static final int SAME_TARGET
A state to represent one of several possible states for a Cell object

See Also:
Constant Field Values

SHIP_HIT

public static final int SHIP_HIT
A state to represent one of several possible states for a Cell object

See Also:
Constant Field Values

SHIP_SUNK

public static final int SHIP_SUNK
A state to represent one of several possible states for a Cell object

See Also:
Constant Field Values
Constructor Detail

Board

public Board(int x,
             int y)
This constructor for the Board object should initialize the board, setting its x and y dimensions to the given values and initializing the cells and shiplist to their initial states. This method should use the clearBoard method (specified next, below) to initialize the cells and ships.

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Method Detail

clearBoard

public void clearBoard()
This method intializes the cell array and ship list references. When this method completes, the board should be a in state that is ready for play to begin.


getShipNumber

public int getShipNumber(Ship ship)
Given a reference to a ship in the ship list, this method returns the integer index of the given ship in the list.

Parameters:
ship - the instance to be searched.
Returns:
the index.

getShip

public Ship getShip(int index)
Given an integer index to a ship in the ship list, this method returns the ship at the requested index.

Parameters:
index - the index requested.
Returns:
the Ship instance

getCellAt

public Cell getCellAt(int x,
                      int y)
Given the x and y coordinates of a cell in the board, this method returns a reference to the cell at that position (x,y). Indexing is zero-based. That is, the first cell is at index (0,0)

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Returns:
the Cell object.

placeShips

public void placeShips(ShipList ships)
This method takes a ShipList object as a parameter. For each ship in the list, this method uses the placeShip method (below) to place the ship on the board.

Parameters:
ships - the list of ships.

allShipsSunk

public boolean allShipsSunk()
This method returns true if all ships on the board have been sunk, and it returns false otherwise. This method uses the allShipsSunk method of the ShipList class to compute the required result.

Returns:
true if all the ships are sunk, false otherwise.

isValidBoard

public boolean isValidBoard()
This method returns true if the board configuration is valid, and it returns false otherwise. A board is considered valid if and only if there are no two ships occupying the same cell on the board. This method uses the getShipCount, getShip, and hasPoint methods of the Ship and ShipList classes.


generateRandomBoard

public void generateRandomBoard(int numberOfShips)
This method resets the board to a new state containing the number of ships given by the numberOfShips parameter. The method should work by clearing the board, randomly placing the given number of ships (using the placeship method, and then testing the board to see if it's in a valid state. If the board is valid, the method terminates, otherwise it tries again and keeps trying (in a do/while loop) until a valid board is obtained.

Parameters:
numberOfShips - number of Ships.

placeShip

public void placeShip(Ship aShip)
This method places the given ship (aShip) on the board by marking each cell that the ship occupies (with a call to setShipOnCell) and then by adding the ship to the shipList. The cells to be marked start with x and y coordinates of the given ship and include the additional cells determined by the ship length its orientation (which are obtained by use of the appropriate methods of the Ship class).

Parameters:
aShip - the instance of ship to be placed

processShot

public int processShot(int x,
                       int y)
This method processes a shot fired at cell x,y. It returns the status of the targeted cell in the form of one of the status constant values defined above. If the targeted cell has already been hit, then this method simply returns SAME_TARGET. If the cell has not previously been hit then the following steps are taken. First the cell is marked as hit using the setIsHit method of the Cell class. Second, if there is not a ship on the targeted cell, then the method returns the status MISSED. Otherwise the ship occupying the targeted cell is marked as hit (by a call to the ship's hit method); then if the targeted ship is sunk (to be determined by a call to the ship's isSunk method) then this method returns SHIP_SUNK status, otherwise it returns SHIP_HIT status.

Parameters:
x - the x coordinate of the target.
y - the y coordinate of the target.

printShipList

public void printShipList()
Prints the ship list by calling the print method of the ship list.