Computer Science I

MC140.01
Spring 2001
Assignment 7
due Friday 30 March 2001

The goal of this assignment is to practise passing arrays to functions.
This assignment is worth 2% of your semester grade.

Battleship, part I.
This assignment, along with the subsequent assignments, will involve writing a program to play the game of Battleship.

Battleship is played on a 10 X 10 square grid. Each player places 5 ships on his grid. Each ship occupies between 2 and 5 positions (or "squares") on the grid, as follows:
carrier: 5 squares
battleship: 4 squares
submarine: 3 squares
cruiser: 3 squares
destroyer: 2 squares

The columns of the grid are numbered from 0 to 9; the rows of the grid are lettered from A to J (as illustrated in part 3 below).

Players take turns trying to hit and sink each ship. On a player's turn, he calls out a position in the grid, identified by its row letter and column number. For example, "A1" is the square in the upper left corner of the grid.

After calling out a position, the opponent reports that the call is either a "hit" or a "miss". If it is a hit, the opponent must identify which ship was hit.

A ship is sunk when all the positions that ship occupies have been hit.

A player wins when he sinks all his opponent's ships.

1. (2 points) Create the grid.
Create a 10 x 10 character array called grid. Declare this as a local variable in your main() function.

2. (5 points) Initialize the grid.
Write a function called initGrid() which is called from the main() function. The initGrid() function should take the grid as an argument and fill it with blanks (the space character: ' ').

3. (8 points) Print the grid.
Write a function called printGrid() which is called from the main() function. The printGrid() function should take the grid as an argument and print out the grid. The rows and columns should be labeled and the each entry should be boxed as follows:

   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---+---+---+---+---+---+---+---+---+---+---+
 A |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 B |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 C |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 D |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 E |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 F |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 G |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 H |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 I |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+
 J |   |   |   |   |   |   |   |   |   |   |
---+---+---+---+---+---+---+---+---+---+---+

4. (3 points) Write the main().
Write the main() function, which should include the declaration from part 1 above. Then it should call initGrid() and printGrid(). When executed, the progam should merely output the grid, as illustrated in part 3 above.

5. (2 points) Make sure you do the following:

6. Submit the assignment in the usual way (i.e., follow the submission instructions from assignment #1). Also, please bring a hardcopy to class, and please staple loose sheets together!