Class Ocean
java.lang.Object
Ocean
- All Implemented Interfaces:
OceanInterface
This class manages the game state by keeping track of what entity is
contained in each position on the game board.
- Author:
- harry
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
The number of times a shot hit a ship.protected Ship[][]
A 10x10 2D array of Ships, which can be used to quickly determine which ship is in any given location.protected int
The number of ships totally sunk.protected int
The total number of shots fired by the user -
Constructor Summary
ConstructorDescriptionOcean()
Creates an "empty" ocean, filling every space in theships
array with EmptySea objects. -
Method Summary
Modifier and TypeMethodDescriptionint
Ship[][]
Provides access to the grid of ships in this Ocean.int
int
boolean
boolean
isOccupied
(int row, int column) Checks if this coordinate is not empty; that is, if this coordinate does not contain an EmptySea reference.void
Place all ten ships randomly on the (initially empty) ocean.void
print()
Prints the ocean.boolean
shootAt
(int row, int column) Fires a shot at this coordinate.
-
Field Details
-
ships
A 10x10 2D array of Ships, which can be used to quickly determine which ship is in any given location. -
shotsFired
protected int shotsFiredThe total number of shots fired by the user -
hitCount
protected int hitCountThe number of times a shot hit a ship. If the user shoots the same part of a ship more than once, every hit is counted, even though the additional "hits" don't do the user any good. -
shipsSunk
protected int shipsSunkThe number of ships totally sunk.
-
-
Constructor Details
-
Ocean
public Ocean()Creates an "empty" ocean, filling every space in theships
array with EmptySea objects. Should also initialize the other instance variables appropriately.
-
-
Method Details
-
placeAllShipsRandomly
public void placeAllShipsRandomly()Place all ten ships randomly on the (initially empty) ocean. Larger ships must be placed before smaller ones to avoid cases where it may be impossible to place the larger ships.- Specified by:
placeAllShipsRandomly
in interfaceOceanInterface
- See Also:
-
isOccupied
public boolean isOccupied(int row, int column) Checks if this coordinate is not empty; that is, if this coordinate does not contain an EmptySea reference.- Specified by:
isOccupied
in interfaceOceanInterface
- Parameters:
row
- the row (0 to 9) in which to check for a floating shipcolumn
- the column (0 to 9) in which to check for a floating ship- Returns:
- true if the given location contains a ship, and false otherwise.
-
shootAt
public boolean shootAt(int row, int column) Fires a shot at this coordinate. This will update the number of shots that have been fired (and potentially the number of hits, as well). If a location contains a real, not sunk ship, this method should return true every time the user shoots at that location. If the ship has been sunk, additional shots at this location should return false.- Specified by:
shootAt
in interfaceOceanInterface
- Parameters:
row
- the row (0 to 9) in which to shootcolumn
- the column (0 to 9) in which to shoot- Returns:
- true if the given location contains an afloat ship (not an EmptySea), false if it does not.
-
getShotsFired
public int getShotsFired()- Specified by:
getShotsFired
in interfaceOceanInterface
- Returns:
- the number of shots fired in this game.
-
getHitCount
public int getHitCount()- Specified by:
getHitCount
in interfaceOceanInterface
- Returns:
- the number of hits recorded in this game.
-
getShipsSunk
public int getShipsSunk()- Specified by:
getShipsSunk
in interfaceOceanInterface
- Returns:
- the number of ships sunk in this game.
-
isGameOver
public boolean isGameOver()- Specified by:
isGameOver
in interfaceOceanInterface
- Returns:
- true if all ships have been sunk, otherwise false.
-
getShipArray
Provides access to the grid of ships in this Ocean. The methods in the Ship class that take an Ocean parameter must be able to read and even modify the contents of this array. While it is generally undesirable to allow methods in one class to directly access instancce variables in another class, in this case there is no clear and elegant alternatives.- Specified by:
getShipArray
in interfaceOceanInterface
- Returns:
- the 10x10 array of ships.
-
print
public void print()Prints the ocean. To aid the user, row numbers should be displayed along the left edge of the array, and column numbers should be displayed along the top. Numbers should be 0 to 9, not 1 to 10. The top left corner square should be 0, 0.- Use 'S' to indicate a location that you have fired upon and hit a (real) ship
- '-' to indicate a location that you have fired upon and found nothing there
- 'x' to indicate a location containing a sunken ship
- '.' (a period) to indicate a location that you have never fired upon.
- Specified by:
print
in interfaceOceanInterface
-