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:

* * * * * D * S

* C C C * D * S

C * * * * D * *

C * * S * D * *

C * * S * * S S

 

Some suggestions to make it into a level 4:

 

Normal style issues apply:

 

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!