Hello... I'm not entirley new to Java but I have some problems with an assignment that I'm doing. I hope that you guys can help me with a little bit.
We are asked to write a battleship game. We are asked to enter the location of four ships: a BattleShip, Destroyer, Cruiser & Tug. Theres 2 boards for the game.. A challenger board and a program board.
Once the ships have been placed the game boards should be shown as follows. Notice the challenger's ship locations are shown, but the locations of the program's ships are hidden and it is the user's task to try and sink them. (While the program is shooting back.). The user should be prompted for the co-ordinate of the bow (top leftmost square) of the ship and the direction which can be vertical "V" or horizontal "H". For example:
The game should be something like this:
Challenger Program
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0~ ~ ~ D D D D ~ ~ ~ 0~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 1~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 2~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3~ ~ ~ ~ B ~ ~ C C C 3~ ~ ~ ~ ~ ~ ~ ~ ~ ~
4~ ~ ~ ~ B ~ ~ ~ ~ ~ 4~ ~ ~ ~ ~ ~ ~ ~ ~ ~
5~ ~ ~ ~ B ~ ~ ~ ~ ~ 5~ ~ ~ ~ ~ ~ ~ ~ ~ ~
6~ ~ ~ ~ B ~ ~ ~ ~ ~ 6~ ~ ~ ~ ~ ~ ~ ~ ~ ~
7~ ~ ~ ~ B ~ ~ ~ ~ ~ 7~ ~ ~ ~ ~ ~ ~ ~ ~ ~
8~ T T ~ ~ ~ ~ ~ ~ ~ 8~ ~ ~ ~ ~ ~ ~ ~ ~ ~
9~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 9~ ~ ~ ~ ~ ~ ~ ~ ~ ~
----------------------------------------------------------
Anyway...
I've done this so far... but the code below shows only one board. I need to
show to boards.. one for the program and one for the challenger.
And I need to know how to enter the vertical/horizontal co-ordinates.. what code shoud I add.
The code is aliitle bit messy.. but any help would be really appreciated.
Thanks alot in advance.
-------------------------------------------------------------
public class BattleshipGame {
//This class represents the game board and the rules of the game
private char[][] board;
private int numberOfMoves; //keeps track of the number of moves
public final static int MOVE_MISS = 0;
public final static int MOVE_HIT = 1;
public final static int MOVE_INVALID = 2;
public final static String[] ResultMessages = {"Miss", "Hit!", "Invalid Move"};
public BattleshipGame() {
board = new char[10][10];
initializeBoard();
placeShip(4, true, 'B'); // battleship
placeShip(3, false, 'D'); // destroyer
placeShip(2, true, 'C'); // cruiser
placeShip(1, false, 'T'); // tug
numberOfMoves = 0;
}
public void initializeBoard() {
// This sets all the squares to be waves
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[i].length; ++j) {
board[i][j] = '~';
}
}
}
public void placeShip(int shipSize, boolean xOriented, char symbol) {
// Places a ship of size shipSize,
// either along the x or y axis (no diagonal ships)
int startx = 0, starty = 0;
int x, y;
if (xOriented) {
x = 1;
y = 0;
} else {
x = 0;
y = 1;
}
boolean goodSpot = false;
while (!goodSpot) {
// Choose a random spot to put the end of the ship
startx = (int) (Math.random() * (board.length - shipSize*x));
starty = (int) (Math.random() * (board[0].length - shipSize*y));
goodSpot = true;
// Verify that nothing else is already placed in any of the squares you need
for (int i=0; i < shipSize; ++i) {
if (board[startx+(i*x)][starty+(i*y)] != '~') {
goodSpot = false;
}
}
}
for (int i=0; i < shipSize; ++i) {
board[startx+(i*x)][starty+(i*y)] = symbol;
}
}
public void printGameBoard(boolean showShipPositions) {
//This method displays the game board
//if showShipPositions is true the positions of
//the ships are also shown
String output = "Moves: " + numberOfMoves + "\n\n";
System.out.println(output);
output = " ";
for (int i = 0; i < board.length; ++i) {
output = output + i + " ";
}
System.out.println(output);
for (int i = 0; i < board.length; ++i) {
output = "" + i + " ";
for (int j = 0; j < board[i].length; ++j) {
if (board[i][j] == '.' || board[i][j] == '*') {
output += board[i][j] + " ";
}
else if(showShipPositions){
//show the positions of the ships
output += board[i][j] + " ";
}
else{
output += '~' + " ";
}
}
output += "\n";
System.out.println(output);
}
}
public int move(int x, int y) {
numberOfMoves++;
// check to see if the coordinates are out of bounds
if (x >= board.length || x < 0 ||
y >= board[0].length || y < 0) {
return MOVE_INVALID;
}
// see if it's a miss
if (board[x][y] == '~' || board[x][y] == '.') {
board[x][y] = '.';
return MOVE_MISS;
}
else {
//it must be a hit
board[x][y] = '*';
return MOVE_HIT;
}
}
public boolean isWon() {
//determine if the game is won yet
boolean won = true;
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[i].length; ++j) {
if (board[i][j] != '~' && board[i][j] != '*' && board[i][j] != '.') {
won = false;
}
}
}
return won;
}
}