Project 1 - Battleship Game

Initial Submission Due: Friday, December 19th, 2008

Final Project Due: Sunday, January 18th, 2009
Final Project Late Submission: Monday, January 19th, 2009

Overview

You will write a simple battleship game for this project. The program will expect a command line with a board size and an input file containing ship information. Your program will start by creating a board of the specified size reading the input file, and placing the ships on the board. From there the player will have a chance to fire missiles to try to sink the ships. The player will also be able to get help, view the board, view ship placement (a cheat mode), get game statistics and quit the game.

Objectives

Grading

The weights for this project are as follows:

  1. Initial Submission: 30%
  2. Final Submission: 70%
  3. Final Submission Late: -10%

Getting Help

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.

Program Requirements

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:

The ships cannot be placed off the board or on top of each other. Game statistics must be kept throughout the game, including tracking the number of hits and misses, the total number of missiles fired, and the number of ships sunk.

Command Line

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.)

The minimum board size is 5 by 5. The maximum size allowed will be 26 because single letters will label the rows and columns of the board. For example, with N=10, the range of letters will be A-J.

Input Format

The filename is the name of a file whose first line contains the number of ships, followed by one line 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.

The example file link, input, shows sample input file content. Note that spacing may vary between the entries. This whitespace may be any combination of space characters and tab characters.

Example Using a Valid Ships File

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.)

Example Using an Invalid Ships File

If a board size of 10 has been specified and the file invalid-file contains the following:

5
D H H H
C B F B
D C F C
H D I D
F C F D

Then the ships overlap, and the program must not continue. See the table of errors for what should happen.

If the program receives errors in the input data, the program must print an error message to standard error and gracefully end. Below is a table of possible error situations and the messages displayed as a consequence.

Fig. 1: Error Situations and Messages
*required for initial submission

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.

When the program starts, it displays the board as if the user had typed "board". The program then prompts the user will for a command by displaying "> " on a new line. The commands are in the table below.

Fig. 2: User Command Input Semantics
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 printed board 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 A, the second ship as B, and so on.

This printed board 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, display the message
"Coordinates previously fired upon." on standard output.

If the coordinates are out of range, display the message
"Illegal coordinates." on standard output.

If the missile hits a part of the sea, display the message "Miss!" on standard output.

If the missile hits a part of a ship, display the message "Hit!" on standard output.

If the missile hits a part of a ship, causing it to sink, display the message " Sunk!" on standard output; this output is in addition to reporting a hit.

If the wrong number of arguments is provided, or the row and columns specified are not letters in the valid range, display the message "Illegal coordinates."

Afterwards the program will behave as if "board" had been entered by the user.

If, after a "fire" command, all the battleships are sunk, display on a separate line: "You win!"

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:
percentage = 100.0 * numberOfHits / totalMissilesFired

Here is an example output for the command:

Number of missiles fired: 1
Number of hits: 1
Number of misses: 0
Hit ratio: 100.0%
Number of ships sunk: 1
help Displays the following usage message:

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

quit Exit the program.
  (blank: all white space or a newline with no prior characters) Reprint the prompt.
garbage (i.e. neither a command nor blank) Display the message "Illegal command." on standard output.

Output Format

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, right-justified.

The example file link, output, shows the required, correct spacing and layout for the board display.

Project Design

You are required to use these technologies in this project: inheritance, exceptions, and file input.

Uses of Inheritance

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 composed of abstract Cell references, and the program populates the board with instances of concrete subclasses such as Water, ShipSection (OK or damaged), or MissedMissile.

2. [Optional] Use several different classes to represent the actions of the user commands. Create a collection containing 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, the program code looks up the command and executes the command associated with the entered text.

Use of Exceptions

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.

Sample Run

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

Note: the above file is valid. If the third ship's coordinates were transposed, as in invalid-file, then the file would be invalid because the ships overlap.

Any text that the user types in is shown in a font of this color and weight.


%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


Project Submission

Initial Submission

In order to meet initial submission qualifications you must submit the following:

When you have these classes working to the standard set by your professor, submit all your source code using the following command:

try grd-232 project1-min Battleship.java ...

The ellipsis ("...") means that you may submit as many other java files as your design requires.

Final Submission

Implement all the game logic as described above. When you are satisfied and confident that your project meets final submission standards, submit all your source code using the following command:

try grd-232 project1 Battleship.java ...

$Id: writeup.html,v 1.4 2008/12/12 17:59:18 vcss232 Exp $