Package student.micro.battleship
Class Ship
java.lang.Object
student.micro.battleship.Ship
Represents a single ship in a player's fleet in the classic board game
"Battleship".
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
How many times has this ship been hit so far?Get the type of this ship.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).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).int
hashCode()
boolean
Get whether this ship is oriented horizontally (left-right) or vertically (up-down) on the board.boolean
isSunk()
Determine whether this ship has been sunk in the game.boolean
occupies
(int x, int y) Determine if this ship occupies the specified location.toString()
-
Constructor Details
-
Ship
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
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
-
equals
-
hashCode
public int hashCode()
-