Class Ship

Direct Known Subclasses:
Battleship, Cruiser, Destroyer, EmptySea, Submarine

public abstract class Ship extends Object
Ship is the abstract class for all of the ships and sea tiles that will make up the game of Battleship. Ships of all kinds are always considered to be facing up or to the left, meaning that any portion of the ship that is not the bow will be at a higher numbered row or column than the bow.
Author:
harry
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The column (0 to 9) which contains the bow (front) of the ship.
    protected int
    The row (0 to 9) which contains the bow (front) of the ship.
    protected boolean[]
    hit is an array of four booleans telling whether that part of the ship has been hit.
    protected boolean
    true if the ship occupies a single row, false otherwise.
    protected int
    The number of tiles occupied by the ship.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    int
     
    int
     
    abstract String
     
    boolean
     
    boolean
    Returns true if this ship has been sunk and false otherwise.
    boolean
    okToPlaceShipAt(int row, int column, boolean horizontal, Ocean ocean)
    Determines whether or not this is represents a valid placement configuration for this Ship in this Ocean.
    void
    placeShipAt(int row, int column, boolean horizontal, Ocean ocean)
    Puts the Ship in the Ocean.
    void
    setBowColumn(int bowColumn)
     
    void
    setBowRow(int bowRow)
     
    void
    setHorizontal(boolean horizontal)
     
    boolean
    shootAt(int row, int column)
    If a part of this ship occupies this coordinate, and if the ship hasn't been sunk, mark the part of the ship at that coordinate as "hit".
    Returns a single character String to use in the Ocean's print method.

    Methods inherited from class java.lang.Object

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

    • bowRow

      protected int bowRow
      The row (0 to 9) which contains the bow (front) of the ship.
    • bowColumn

      protected int bowColumn
      The column (0 to 9) which contains the bow (front) of the ship.
    • length

      protected int length
      The number of tiles occupied by the ship. Empty sea locations have a length of 1.
    • horizontal

      protected boolean horizontal
      true if the ship occupies a single row, false otherwise.
    • hit

      protected boolean[] hit
      hit is an array of four booleans telling whether that part of the ship has been hit. Only battleships will use all four locations; cruisers use only the first three, destroyers the first two, submarines and empty sea one.
  • Constructor Details

    • Ship

      public Ship()
  • Method Details

    • getLength

      public int getLength()
      Returns:
      the length of the ship
    • getBowRow

      public int getBowRow()
      Returns:
      the row of the bow (front) of the ship
    • getBowColumn

      public int getBowColumn()
      Returns:
      the column of the bow (front) of the ship
    • setBowColumn

      public void setBowColumn(int bowColumn)
      Parameters:
      bowColumn - the bowColumn to set
    • isHorizontal

      public boolean isHorizontal()
      Returns:
      true if this boat is horizontal (facing left), false otherwise.
    • setHorizontal

      public void setHorizontal(boolean horizontal)
      Parameters:
      horizontal - the horizontal to set
    • setBowRow

      public void setBowRow(int bowRow)
      Parameters:
      bowRow - the bowRow to set
    • getShipType

      public abstract String getShipType()
      Returns:
      the String representing the type of this ship.
    • okToPlaceShipAt

      public boolean okToPlaceShipAt(int row, int column, boolean horizontal, Ocean ocean)
      Determines whether or not this is represents a valid placement configuration for this Ship in this Ocean. Ship objects in an Ocean must not overlap other Ship objects or touch them vertically, horizontally, or diagonally. Additionally, the placement cannot be such that the Ship would extend beyond the extents of the 2D array in which it is placed. Calling this method should not actually change either the Ship or the provided Ocean.
      Parameters:
      row - the candidate row to place the ship
      column - the candidate column to place the ship
      horizontal - whether or not to have the ship facing to the left
      ocean - the Ocean in which this ship might be placed
      Returns:
      true if it is valid to place this ship of this length in this location with this orientation, and false otherwise.
    • placeShipAt

      public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean)
      Puts the Ship in the Ocean. This will give values to the bowRow, bowColumn, and horizontal instance variables in the Ship. This should also place a reference to this Ship in each of the one or more locations (up to four) in the corresponding Ocean array this Ship is being placed in. Each of the references placed in the Ocean will be identical since it is not possible to refer to a "part" of a ship, only the whole ship.
      Parameters:
      row - the row to place the ship
      column - the column to place the ship
      horizontal - whether or not to have the ship facing to the left
      ocean - the Ocean in which this ship will be placed
    • shootAt

      public boolean shootAt(int row, int column)
      If a part of this ship occupies this coordinate, and if the ship hasn't been sunk, mark the part of the ship at that coordinate as "hit".
      Parameters:
      row - the row of the shot
      column - the column of the shot
      Returns:
      true if this ship hasn't been sunk and a part of this ship occupies the given row and column and false otherwise.
    • isSunk

      public boolean isSunk()
      Returns true if this ship has been sunk and false otherwise.
      Returns:
      true if every part of the ship has been hit, and false otherwise.
    • toString

      public String toString()
      Returns a single character String to use in the Ocean's print method. This method should return "x" if the ship has been sunk, and "S" if it has not yet been sunk. This method can only be used to print out locations in the ocean that have been shot at; it should not be used to print locations that have not been the target of a shot yet.
      Overrides:
      toString in class Object
      Returns:
      "x" if this ship has been sunk, and "S" otherwise.