Class Ship

java.lang.Object
student.micro.battleship.Ship

public class Ship extends Object
Represents a single ship in a player's fleet in the classic board game "Battleship".
  • Constructor Summary

    Constructors
    Constructor
    Description
    Ship(ShipType shipType, int x, int y, boolean orientedHorizontally)
    Create a brand new ship (that has never been hit).
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    int
    How many times has this ship been hit so far?
    Get the type of this ship.
    int
    Get the x-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
    int
    Get the y-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
    int
    boolean
    Get whether this ship is oriented horizontally (left-right) or vertically (up-down) on the board.
    boolean
    Determine whether this ship has been sunk in the game.
    boolean
    occupies(int x, int y)
    Determine if this ship occupies the specified location.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Ship

      public Ship(ShipType shipType, int x, int y, boolean orientedHorizontally)
      Create a brand new ship (that has never been hit).
      Parameters:
      shipType - The type of ship to create.
      x - The x-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
      y - The y-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
      orientedHorizontally - If true, then this ship is oriented horizontally to the right of the given position; if false, then the ship will be oriented vertically down from the given position.
  • Method Details

    • getShipType

      public ShipType getShipType()
      Get the type of this ship.
      Returns:
      This ship's type.
    • getX

      public int getX()
      Get the x-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
      Returns:
      The x-coordinate of this ship's bow.
    • getY

      public int getY()
      Get the y-coordinate of this ship's bow (which is always the north/upper end of a vertically oriented ship, or the west/left end of a horizontally oriented ship).
      Returns:
      The y-coordinate of this ship's bow.
    • isOrientedHorizontally

      public boolean isOrientedHorizontally()
      Get whether this ship is oriented horizontally (left-right) or vertically (up-down) on the board.
      Returns:
      True if this ship is oriented horizontally, or false if it is oriented vertically.
    • getHitsSoFar

      public int getHitsSoFar()
      How many times has this ship been hit so far?
      Returns:
      The number of hits struck on this ship.
    • isSunk

      public boolean isSunk()
      Determine whether this ship has been sunk in the game.
      Returns:
      True if this ship has been sunk, or false if it is still afloat.
    • occupies

      public boolean occupies(int x, int y)
      Determine if this ship occupies the specified location.
      Parameters:
      x - The x-coordinate of the location to check.
      y - The y-coordinate of the location to check.
      Returns:
      True if part of this ship lies in the specified cell, or false otherwise.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object