In this project, you will be required to write a simple one sided battleship game. Your program will start by specifying a board size, and then reading an input file with ships placements. From there you will have a chance to fire missiles to try to sink the ships. You will also be able to view the board, view the ship placement (cheat mode) and get the game statistics.
You may get help from your instructor(s) and the teaching assistants. Anything else is not allowed and is subject to the penalties listed in the DCS Policy on Academic Dishonesty. This includes but is not limited to:
We certainly do not expect there to be absolutely no communication between the students of this class; we know that people tend to learn very well that way. However, we expect that you will limit your discussion of this project to determining the specification of the problem. If you have any doubt if what you would like to do might violate our rules, please see your lecture instructor for clarification.
Don't forget that the CS tutoring center is available to you.
Battleship is a popular game played on a board representing a rectangular portion of the sea. It is broken up into a grid of square cells, each one having a unique pair of coordinates. Battleships are placed on the board in a straight horizontal or vertical line. The opponent does not know where the battleships are; he or she fires missiles into the sea, specifying coordinates, and is only told if it was a hit or a miss. The player who fired the missile uses this information to guess the locations of the battleships. A battleship is sunk when all of its sections have been hit by a missile. The goal is to sink all the battleships.
Usually it is a two-player game, each one having his/her own board. You will create a one-player game, where the program places battleships on a board and the user is the opponent trying to sink them.
Your program must be able to create a new board, place the ships as specified in a file, be able to fire missiles at the user-specified the locations, and keep track of the game's state. The ships cannot be placed off the board or on top of each other. Game statistics must be kept throughout the game, including keeping track of the number of hits and misses, the total number of missiles fired, and the number of ships sunk.
The game shall be started as follows:
java Battleship
N filename
The value N represents the dimensions of the board. Since a board is always square, there are N columns and N rows. (For a board of size N, the legal row and column coordinates are 0 to N-1.)
Because single letters will be used to label the rows and columns of the board, the maximum size allowed will be 26. For example, with N=10, the range of letters will be A-J.
The filename is the name of a file whose first line contains the number of ships, followed by one line each for each ship position, in the following format:
startRow startColumn endRow endColumn
startRow and startCol are each single letters which specify
the starting position of the ship.
endRow and endCol are each single letters which specify
the ending position of the ship.
For this project, you are guaranteed that the orientation of the ships will be either horizontal or vertical (no diagonals).
You will also be guaranteed that the file will contain no more than 26 ships, for output purposes.
An example input file can be found here.
If a board size of 10 has been specified and the input file contains the following:
2 C C C E D E H E
then the initial board setup would be as follows:
A B C D E F G H I J A - - - - - - - - - - B - - - - - - - - - - C - - A A A - - - - - D - - - - B - - - - - E - - - - B - - - - - F - - - - B - - - - - G - - - - B - - - - - H - - - - B - - - - - I - - - - - - - - - - J - - - - - - - - - -
(More on output format later.)
If any errors occur in the data used by the game upon startup, an error message must be printed on standard error and the program halted. Here is a table of possible error situations and the messages displayed as a consequence.
error |
message to be displayed (all terminate with a new line) |
| Illegal number of arguments* | Usage: java Battleship N config-file |
| 1st command line argument is not an integer* | Usage: java Battleship N config-file |
| 1st command line argument < 5* | Board must be at least 5 by 5. |
| 1st command line argument > 26* | Board must be at most 26 by 26. |
| File named by second argument cannot be opened. | Cannot open file filename. |
| Specifications of ships cause them to overlap. | Overlapping or out-of-bounds ships in file
filename. |
| Specifications of ships extend beyond the boundaries of the board. | Overlapping or out-of-bounds ships in file
filename. |
The first thing that happens after the program is started is that the board
layout is displayed as if the user had typed "board". The
user will then be prompted for a command by displaying ">
" on a new line.
The commands are as follows:
| Command | Description |
| board |
Print out the placement of the water and missiles on the board.
Water must be marked with a hyphen: Each previous fired missile destination must be marked with either an X or an O signifying a hit or a miss, respectively. This printout must not show the placement of the ships. For the precise format of the output, see the Output Format section later in this document. Note that you must allow for a single letter for the row and column, separated by a single space. |
| ships |
Print out the placement of the ships on the board.
Water must be marked with a hyphen:
Each ship must be represented
by a letter corresponding to its position in the input file. For example,
the first ship should be marked as This printout must not show the placement of the missiles. For the precise format of the output, see the Output Format section later in this document. Note that you must allow for a single letter for the row and column, separated by a single space. |
| fire row col | Fire a missile at the square at the given row and column.
Both coordinates will be given as single letters separated by a single space.
The status of the missile must
be changed depending on if it is a hit or a miss at the current spot on the
board. If there is a missile already there, the user will get the error message
If the coordinates are out of range, the user will get the error message
If the missile hits a part of the sea, the user will get the message
" If the missile hits a part of a ship, the user will get the message
" If the missile hits a part of a ship, causing it to sink, the user will get the message
" If the wrong number of arguments is provided, or the row and columns
specified are not letters in the valid range,
the user will get the error message " Afterwards the program will behave as if "board" had been entered by the user.
If, after a "fire" command, all the battleships are sunk,
the game will display, on a separate line:
After a win has been determined, the program will behave as if the commands "stats" and "quit" have been entered. |
| stats | Print the number of missiles fired, the number of hits,
the number of misses, the hit ratio
percentage, and the number of ships sunk. When computing the percentage for the hit ratio, perform the
operation as: |
| help |
Displays the following usage message:
|
| quit | Exit the program. |
| A blank command, or a command with just whitespace, should quietly cause a re-prompting. | |
| garbage | If any other command is entered, the user will get the error message
"Illegal command." on standard output. |
When the board is printed, it will be printed as a grid. Rows are horizontal, increasing in coordinates downward, and columns are vertical, increasing in coordinates to the right. Column letters are displayed above the first row starting with "A" as the first column and incrementing thereafter. Row letters are displayed to the left of the first column, starting with "A" as the first column and incrementing thereafter. Un-fired-upon water is indicated with a hyphen. Water that has been fired upon is indicated with the letter "O". Un-fired-upon ship locations are indicated by their position in the input file, "A" for the first, "B" for second, and so on. Ship locations that have been hit by a missile are marked with an "X". All items displayed are placed in a 2-space field, left-justified. rev1.7 - For the last column, you should not print an extra space after it.
An example output with correct spacing can be found here.
You are required to use three technologies when designing this project: inheritance, exceptions, and file input.
1. [Required] You shall use several different classes to represent the current
contents of a cell on the board. For example, the board can be be composed
of abstract Cell references, which is then populated with
instances of concerete subclasses such as
Water, ShipSection (OK or damaged),
or MissedMissile.
2. [Optional] Use several different classes to represent the actions of the six user commands. Populate a container with associations between instances of these new classes and the string name of the first word in each command listed in Figure 2. When the user types something in, a command is looked up and executed.
Should an error occur that cannot be handled at the point of detection in
the code, an exception should be raised and caught somewhere where the program
can gracefully handle it or terminate. For example, you will likely want
to use exceptions to deal with file input errors. You may define your own
exception classes. Use of System.exit( int ) for any purpose
is forbidden in this project.
As you follow the sample run below, assume that the file input-2.2 contains the following data:
5 D H H H C B F B C D C F H D I D F C F D |
Any text that the user types in is shown in this way.
%java Battleship 10 input-2.2 A B C D E F G H I J A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - - - - - - - - F - - - - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - > ships A B C D E F G H I J A - - - - - - - - - - B - - - - - - - - - - C - B - C C C - - - - D - B - - - - - A - - E - B - - - - - A - - F - B E E - - - A - - G - - - - - - - A - - H - - - D - - - A - - I - - - D - - - - - - J - - - - - - - - - - > fire Illegal coordinates. > fire A Z Illegal coordinates. > fire A A Miss! A B C D E F G H I J A O - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - - - - - - - - F - - - - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - > fire F C Hit! A B C D E F G H I J A O - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - - - - - - - - F - - X - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - > fire F D Hit! Sunk! A B C D E F G H I J A O - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - - - - - - - - F - - X X - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - > ships A B C D E F G H I J A - - - - - - - - - - B - - - - - - - - - - C - B - C C C - - - - D - B - - - - - A - - E - B - - - - - A - - F - B E E - - - A - - G - - - - - - - A - - H - - - D - - - A - - I - - - D - - - - - - J - - - - - - - - - - > stats Number of missiles fired: 3 Number of hits: 2 Number of misses: 1 Hit ratio: 66.66666667% Number of ships sunk: 1 > help Possible commands: board - displays the user's board ships - displays the placement of the ships fire r c - fires a missile at the cell at [r,c] stats - prints out the game statistics quit - exits the game > abacab Illegal command. > > quit
In order to meet minimum submission qualifications you must submit the following:
boardboard" command and displays the
board with the water cells.
quit" command and terminates the program.
When you have these classes working to the standard set by your professor, submit all your source code using the following command:
try 232-grd project1-min Battleship.java ...
The ellipsis ("...") means that you may submit as many other java files as your design requires.
When you are satisfied and confident that your project meets final submission standards, submit all your source code using the following command:
try 232-grd project1 Battleship.java ...
$Id: writeup.html,v 1.7 2007/01/08 16:57:56 vcss232 Exp vcss232 $