1. Member
Join Date
Nov 2012
Posts
1
Rep Power
0

## Battleship program

Write a program that allows a user to play a pre-determined number of text-based games of Battleship.

Assume that Battleship will be played on one of the following five board sizes: 5 by 5, 6 by 6, 7 by 7, 8 by 8 or 9 by 9. In the provided Driver above, two games of Battleship will be played, each on a 5 by 5 board. However, make sure that your program works for the other dimensions as well.

The following ships must be placed on the board in random locations such that they appear in either contiguous horizontal or vertical squares and do not overlap: an aircraft carrier of length 5, a battleship of length 4, a submarine of length 3 and a patrol boat of length 2.

When a user plays a game of battleship, she will be prompted for the 1-based row and column of her next guess. The game will keep track of how many guesses it takes the user to guess where each component of each ship is located.

The Driver program was included:

public class Driver
{
public static void main (String [] args)
{
Battleship game = new Battleship(5); // play on a 5 by 5 board

System.out.println("Battleship!");
System.out.println("-----------\n");

{
game.resetBoard();

System.out.println("Revealed board (for verification purposes):\n");
game.printBoard(true); // print contents of all squares

while (!game.over())
{
game.printBoard(false); // only print known squares
game.makeGuess();
}

game.printStatistics();
}

System.out.println("Goodbye. Thanks for playing.");
}
}

As well as a square class
public class Square
{
private boolean known; // has the user already guessed this square?
private int contents; // the contents of the square (empty, patrol boat, etc.)

/**
*
* @return whether the user has guessed the square
*/

public boolean getKnown()
{
return known;
}

/**
* Writer method
*
* @param value indicates whether the user has guessed the square
*/

public void setKnown(boolean value)
{
known = value;
}

/**
*
* @return an integer indicating the contents of the square (e.g. empty, submarine, etc.)
*/

public int getContents()
{
return contents;
}

/**
* Writer method.
*
* @param value indicates the contents of the square (e.g. empty, battleship, etc.)
*/

public void setContents(int value)
{
contents = value;
}
}

I need to make the Battleship class to make the program run. Help!

Also, I have included a transcript of what it should look like:

+-+-+-+-+-+
1 | | | | | |
+-+-+-+-+-+
2 | | | | | |
+-+-+-+-+-+
3 | | | | | |
+-+-+-+-+-+
4 | | | | | |
+-+-+-+-+-+
5 | | | | | |
+-+-+-+-+-+
1 2 3 4 5

Enter a row to guess > 1
Enter a column to guess > 2
Hit! You just sunk part of the Submarine

+-+-+-+-+-+
1 | |3| | | |
+-+-+-+-+-+
2 | | | | | |
+-+-+-+-+-+
3 | | | | | |
+-+-+-+-+-+
4 | | | | | |
+-+-+-+-+-+
5 | | | | | |
+-+-+-+-+-+
1 2 3 4 5

Enter a row to guess > 2
Enter a column to guess > 2
Hit! You just sunk part of the Submarine

Thanks!

2. Senior Member
Join Date
Nov 2012
Posts
105
Rep Power
0

## Re: Battleship program

Well, we can't just give you the code, but we can help you with any errors or problems ect. also I would suggest if you're going for flexibility in the program, you could just as easily make the Battleship class like:

Java Code:
`Battleship game = new Battleship(5, 5); // play on a 5 by 5 board`
That way you can change either dimension. Now to start off, the class should have an array to store this data, and have all the functions required to make the board and whatever else you need it to do. Or at least I'm assuming what the class is for. Of course, there will probably be people who give better advice than me xD

EDIT: I forgot, you also need [ CODE] [ /CODE](no spaces) tags to put around your code, to make it look all neat.
Last edited by Darkzombies; 11-28-2012 at 02:56 AM.