Class Battleship
java.lang.Object
Battleship
public class Battleship
extends java.lang.Object
A console version of the
classic Battleship
game. The board has 10 rows and 10 columns. The rows are named A-J, the
columns are named 1-10. Each position is named by its row and column (e.g.,
A1 is the upper-left corner of the board).
-
Field Summary
Fields Modifier and Type Field Description static int
BOARD_SIZE
The size of the game board is 10 x 10.static java.lang.String
SPACE_HIT
Marker indicating a space on the board has been fired upon and the result was a hit.static java.lang.String
SPACE_MISS
Marker indicating a space on the board has been fired upon and the result was a miss.static java.lang.String
SPACE_OPEN
Marker indicating a space on the board is open (i.e., has not been fired upon). -
Constructor Summary
Constructors Constructor Description Battleship(int startingAmmunition)
Constructs a Battleship game that is ready to play. -
Method Summary
Modifier and Type Method Description void
fire(java.lang.String location)
Fires a torpedo at the given location as long as there is ammunition remaining.int
getAmmunition()
Returns the amount of ammunition remaining.boolean
hasWon()
Determines whether or not the player has won.boolean
keepPlaying()
Returns whether or not game play should continue.void
printBoard(boolean reveal)
Prints the board.void
printWelcome()
Prints the game's welcome message and instructions.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
BOARD_SIZE
public static final int BOARD_SIZEThe size of the game board is 10 x 10.- See Also:
- Constant Field Values
-
SPACE_OPEN
public static final java.lang.String SPACE_OPENMarker indicating a space on the board is open (i.e., has not been fired upon). The value is a space (" ").- See Also:
- Constant Field Values
-
SPACE_MISS
public static final java.lang.String SPACE_MISSMarker indicating a space on the board has been fired upon and the result was a miss. The value is a hyphen ("-").- See Also:
- Constant Field Values
-
SPACE_HIT
public static final java.lang.String SPACE_HITMarker indicating a space on the board has been fired upon and the result was a hit. The value is a pound sign ("#").- See Also:
- Constant Field Values
-
-
Constructor Details
-
Battleship
public Battleship(int startingAmmunition)Constructs a Battleship game that is ready to play.- Parameters:
startingAmmunition
- the number of torpedos to start the game with
-
-
Method Details
-
fire
public void fire(java.lang.String location)Fires a torpedo at the given location as long as there is ammunition remaining. The results of the shot will be printed to the console.- Parameters:
location
- the location to fire upon, values are expected to follow the format[LETTER][NUMBER]
-
getAmmunition
public int getAmmunition()Returns the amount of ammunition remaining.- Returns:
- int
-
hasWon
public boolean hasWon()Determines whether or not the player has won.- Returns:
- true if the player has won, false otherwise
-
keepPlaying
public boolean keepPlaying()Returns whether or not game play should continue. The game ends when either of the following criteria are met:- All enemy ships have been sunk (a win)
- The player has no more ammunition (a loss)
- Returns:
- true if the game should continue, false otherwise
-
printBoard
public void printBoard(boolean reveal)Prints the board. Each space will either be displayed with one of theSPACE_
constants defined in this class or the first letter of the ship hidden at the location, ifreveal
is set to true. An upper case letter indicates a hit, a lower case letter indicates a miss.- Parameters:
reveal
- if true, then the ships' locations are displayed
-
printWelcome
public void printWelcome()Prints the game's welcome message and instructions.
-