Class Ship
java.lang.Object
Ship
- Direct Known Subclasses:
Battleship
,Cruiser
,Destroyer
,EmptySea
,Submarine
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
Modifier and TypeFieldDescriptionprotected 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
-
Method Summary
Modifier and TypeMethodDescriptionint
int
int
abstract String
boolean
boolean
isSunk()
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".toString()
Returns a single character String to use in the Ocean's print method.
-
Field Details
-
bowRow
protected int bowRowThe row (0 to 9) which contains the bow (front) of the ship. -
bowColumn
protected int bowColumnThe column (0 to 9) which contains the bow (front) of the ship. -
length
protected int lengthThe number of tiles occupied by the ship. Empty sea locations have a length of 1. -
horizontal
protected boolean horizontaltrue
if the ship occupies a single row,false
otherwise. -
hit
protected boolean[] hithit 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
- Returns:
- the String representing the type of this ship.
-
okToPlaceShipAt
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 shipcolumn
- the candidate column to place the shiphorizontal
- whether or not to have the ship facing to the leftocean
- 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
Puts the Ship in the Ocean. This will give values to thebowRow
,bowColumn
, andhorizontal
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 shipcolumn
- the column to place the shiphorizontal
- whether or not to have the ship facing to the leftocean
- 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 shotcolumn
- the column of the shot- Returns:
- true if this ship hasn't been sunk and a part of this ship
occupies the given
row
andcolumn
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
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.
-