Class Ocean

All Implemented Interfaces:
OceanInterface

public class Ocean extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    protected 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

    Constructors
    Constructor
    Description
    Creates an "empty" ocean, filling every space in the ships array with EmptySea objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    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
    Prints the ocean.
    boolean
    shootAt(int row, int column)
    Fires a shot at this coordinate.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ships

      protected Ship[][] ships
      A 10x10 2D array of Ships, which can be used to quickly determine which ship is in any given location.
    • shotsFired

      protected int shotsFired
      The total number of shots fired by the user
    • hitCount

      protected int hitCount
      The 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 shipsSunk
      The number of ships totally sunk.
  • Constructor Details

    • Ocean

      public Ocean()
      Creates an "empty" ocean, filling every space in the ships 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 interface OceanInterface
      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 interface OceanInterface
      Parameters:
      row - the row (0 to 9) in which to check for a floating ship
      column - 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 interface OceanInterface
      Parameters:
      row - the row (0 to 9) in which to shoot
      column - 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 interface OceanInterface
      Returns:
      the number of shots fired in this game.
    • getHitCount

      public int getHitCount()
      Specified by:
      getHitCount in interface OceanInterface
      Returns:
      the number of hits recorded in this game.
    • getShipsSunk

      public int getShipsSunk()
      Specified by:
      getShipsSunk in interface OceanInterface
      Returns:
      the number of ships sunk in this game.
    • isGameOver

      public boolean isGameOver()
      Specified by:
      isGameOver in interface OceanInterface
      Returns:
      true if all ships have been sunk, otherwise false.
    • getShipArray

      public Ship[][] 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 interface OceanInterface
      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.
      This is the only method in Ocean that has any printing capability, and it should never be called from within the Ocean class except for the purposes of debugging.
      Specified by:
      print in interface OceanInterface