Assignment
- Battleship
You
are going to code a game of Battleship. This two files you will need are
battleship.java and ships.txt. Our game will have three submarines (S), two
cruisers (C), and one destroyer (D) hidden on the grid. The grid will always be
8X5.
The ocean/grid is made of an object.
You
may not change this:
public class
square
{
public char inhere;
/* Note - possible values of inhere are:
* = nothing is in that square
S = submarine is in that square
C = cruiser is in that square
D = destroyer is in that square
*/
public boolean visible;
/* If the user has guessed that square, it becomes visible
This means the value 'inhere' will be printed to the screen
*/
}
The actual ocean is declared like this.
You
may not change this:
square ocean
[] [] = new square [5] [8];
int sizex =
5;
int sizey =
8;
Steps:
Make
a new method in your program read the file into the array of objects to give
the ocean its proper values.
public static void
readocean (square ocean[][], int sizex, int sizey);
The
file ships.txt on the I:// makes up a grid like the following:
* * * * * D * S
* C C C * D * S
C * * * * D * *
C * * S * D * *
C * * S * * S S
Make
a new method in your program that allows the user to make one turn.
They
will pick an x and a y co-ordinate (like in x’s and o’s).
That
square needs to be set to visible.
The
ocean needs to be reprinted.
public static void
oneturn(square ocean[][], int sizex, int sizey)
Make
a new method that decides when the game is over. (No squares will have their
visible variables set to false).
public static Boolean
gameover (square ocean[][], int sizex, int sizey);
Make
a loop in void main that goes until (a) the user is sick of the game and
wants to quit or (b) the game is over – use your gameover method.
Some
suggestions to make it into a level 4:
Use
applets.
Keep
a score – the number of guesses the user has had so far
Get
the user’s name and insert it into all kinds of places.
Give
really clear instructions. For example, how many crusisers are remaining?
How many submarines?
Tell
the user when a battleship was sunk (since they are more than one square,
this is hard).
Normal
style issues apply:
Pre
and post conditions
Comments
Title
comments (name, date, title)
Tabbing
and white space
An example
run of the game
-- WELCOME
TO BATTLESHIP --
What is your
name? Grace Murray Hopper
Welcome Grace Murray Hopper, this game is designed to test if you have
what it takes to be an admiral in the navy.
If you find
all of the ships:
- in under
20 tries, your rank will be admiral.
- in under
25 tries, your rank will be captain.
- in under
30 tries, your rank will be ensign.
- in over 30
tries, you won’t be admitted to serve in the navy!
Grace Murray
Hopper, are you ready to begin? (y,n) y
--- The
Ocean ---
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
Enter the x
co-ordinate (0-4): 1
Enter the y
co-ordinate (0-7): 1
Grace Murray
Hopper, You have a hit!
You have
taken 1 turn. You have 15 out of 16 ships remaining.
--- The
Ocean ---
~~~~~~~~
~C~~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
Enter the x
co-ordinate (0-4): 0
Enter the y
co-ordinate (0-7): 0
Grace Murray
Hopper, You missed!
You have
taken 2 turns. You have 15 out of 16 ships remaining.
and so on!