CMSC 201
Programming Project Two
Naval Battle
Out: Wednesday 10/03/07
Due: Before Midnight, Wednesday 10/17/07
The design document for this project,
design2.txt ,
is due: Before Midnight, Wednesday 10/10/07
|
The Objective
The objective of this assignment is to give you practice with project and
function design. It will also give you an opportunity to work with a
two-dimensional array and passing that array to functions for manipulation.
The Background
This project is loosely based on the game Battleship. Those of you who are
already familiar with this game should note that the project description does
not follow the same rules as the game. Numerous changes have been made to
make a similar game that is much easier to code than the original would be.
The board for this game forms a grid that is 10 rows high by 10 columns wide,
that simulates an area of the ocean. Within this area of the ocean are 5
ships, placed there by the computer, but the player cannot see where they are.
The objective of this game is for the player to sink all 5 of the ships within
a specific number of turns, in our case 40 turns.
For each turn, the player will fire a torpedo into a specific location,
indicated by the coordinates of the grid. The computer will then inform the
player whether the torpedo hit a target or missed, by placing an O on the
board if the shot was a miss or by placing an X on the board if the shot was
a hit. A single hit is never enough to sink a ship. The larger the ship, the
more hits it can withstand before it sinks. If a ship is 3 units long then it
spans three blocks on the grid and would require 3 torpedo hits, one in each
of the grid squares occupied by the ship, in order to sink it.
If you would like to practice playing a similar game so that you understand it,
I have found a version called Armada on the web that will allow you to play
against the computer.
Practice Playing Armada
The Task
Design and code a project that will allow you to play Naval Battle against the
computer.
The Specifications
- There are 5 different kinds of ships and you are to hide one of each
kind in the ocean at a given location. Each kind of ship has a
specific length. Here is a list of the ships, the character used to
indicate a ship of that type in a single position, their length and
their bow and stern locations. The bow is the front of the boat and
the stern is the back of the boat.
Ship Type | Character | Length | Bow Row | Bow Col | Stern Row
| Stern Col |
Aircraft Carrier | A | 5 | 0 | 5 | 4 | 5 | |
Battleship | B | 4 | 7 | 6 | 7 | 9 | |
Cruiser | C | 2 | 1 | 8 | 1 | 9 | |
Destroyer | D | 3 | 4 | 1 | 4 | 3 | |
Submarine | S | 3 | 7 | 0 | 9 | 0 | |
- The board must be implemented as a two-dimensional array of chars
- You must use the character O to indicate a miss and the character X to
indicate a hit. The characters shown in the table are to be used to
indicate the positions of the ships in the array, although they are
not displayed to the player. So the array originally contains:
0 1 2 3 4 5 6 7 8 9
-----------------------------------------
0 | | | | | | A | | | | |
-----------------------------------------
1 | | | | | | A | | | C | C |
-----------------------------------------
2 | | | | | | A | | | | |
-----------------------------------------
3 | | | | | | A | | | | |
-----------------------------------------
4 | | D | D | D | | A | | | | |
-----------------------------------------
5 | | | | | | | | | | |
-----------------------------------------
6 | | | | | | | | | | |
-----------------------------------------
7 | S | | | | | | B | B | B | B |
-----------------------------------------
8 | S | | | | | | | | | |
-----------------------------------------
9 | S | | | | | | | | | |
-----------------------------------------
but the player is shown:
0 1 2 3 4 5 6 7 8 9
-----------------------------------------
0 | | | | | | | | | | |
-----------------------------------------
1 | | | | | | | | | | |
-----------------------------------------
2 | | | | | | | | | | |
-----------------------------------------
3 | | | | | | | | | | |
-----------------------------------------
4 | | | | | | | | | | |
-----------------------------------------
5 | | | | | | | | | | |
-----------------------------------------
6 | | | | | | | | | | |
-----------------------------------------
7 | | | | | | | | | | |
-----------------------------------------
8 | | | | | | | | | | |
-----------------------------------------
9 | | | | | | | | | | |
-----------------------------------------
- To begin play you must display an empty board, tell the player how many
turns he has remaining and prompt the player for his choice of row and
column. Your program will then determine whether it was a hit or a
miss, replace the character in the array in that position with either
an X or an O and display the board again.
- If the choice was a hit, you must determine whether you have actually
sunk the ship with that hit. If the ship sunk, you should display a
message saying which ship was sunk.
- If the player manages to sink all of the ships within 40 turns, the
player wins. If not, you should display a message saying that the
player lost and show the contents of the array, so that he can see where
ships or parts of ships still exist.
- See the sample run below for specifics. There are actually two runs.
In one the player wins and in the other he loses.
Further Specifications
- You are required to use an array (or arrays) that are exactly 10 x 10.
- You must have two different functions that display the array. One will
display the array during game play. The other will display the array at
game's end if the player loses. Two functions are necessary for this,
since what is displayed is different for each of these cases.
- If the user fires the torpedo into the same position twice, you are
required to mention that he has already chosen that position. Let him
make another choice. Don't count that mistake as a turn. (See sample
run for clarification, if necessary)
Suggestions
- You should realize that only one array is necessary for this project,
however, if you choose to use two, that's okay.
- It is easy to initialize the array to hold all spaces and then change
the appropriate positions to hold letters representing the ships.
If you do so, you could write a function that takes the array, its sizes,
the bow row & col, stern row & col, and the character used to represent
each ship and let that function actually place those characters into the
array.
Sample Run
This is the sample run.
Although your output need not be identical to that in the output file,
all information (including a greeting and instructions that are not shown
in the output) must be present.
Please be aware that this may NOT be a complete test of the program.
Submitting the Program
You are to use separate compilation for this project, so you will be
submitting a minimum of three files.
Your C source code file that
contains main() MUST be called proj2.c. I would expect
that you would also have files called battle.c and battle.h, but you
may choose to have additional .c and .h files.
To submit your project, type the following at the Unix prompt.
Note that the project name starts with uppercase 'P'.
submit cs201 Proj2 proj2.c battle.c battle.h (and possibly
other files, separated by spaces)
To verify that your project was submitted, you can execute the
following command at the Unix prompt. It will show all files that
you submitted in a format similar to the Unix 'ls' command.
submitls cs201 Proj2