1. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## Battleship game

Hey, I am having a difficult time figuring out how to implement something where when the player places his ships, there is a check to see if it can fit into the grid such as if a 5 piece is already played and a 4 piece wants to be placed but the 5 piece is in the way. How do I report that they need to choose a different location? This is the code I have so far:

Java Code:
```System.out.print("Player two, Where would you like your Aircraft Carrier(5) to start(Enter x and y coordinate): ");
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
i = i - 1;
j = j - 1;
if (direction == 'H' || direction == 'h'){
for (int count = 0; count < (5); count++){
player2board[i][j] = 'A';
player2board[i][j++]= 'A';
}

}	else {
for (int count = 0; count < (5); count++){
player2board[i][j] = 'A';
player2board[i++][j]= 'A';

}

}
showPlayer2board(player2board);

System.out.print("Player two, where would you like your Battleship(4) to start(Enter x and y coordinate): ");
i = i - 1;
j = j - 1;
while (player2board[i][j] == 'A'){
System.out.println("Spot already used choose a new start location: ");
i = i - 1;
j = j - 1;
}
System.out.print("Player two, Would you like it to be placed Horizontal('H') or Vertical('V')? ");
if (direction == 'H'){
for (int count = 0; count < (4); count++){
player2board[i][j] = 'B';
player2board[i][j++]= 'B';
}

}	else {
for (int count = 0; count < (4); count++){
player2board[i][j] = 'B';
player2board[i++][j]= 'B';

}

}
showPlayer2board(player2board);

System.out.print("Player 2, Where would you like your Submarine(3) to start(Enter x and y coordinate): ");
i = i - 1;
j = j - 1;
while (player2board[i][j] == 'A' || player2board[i][j] == 'B'){
System.out.println("Spot already used choose a new start location: ");
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
if (direction == 'H'){
for (int count = 0; count < (3); count++){
player2board[i][j] = 'S';
player2board[i][j++]= 'S';
}

}	else {
for (int count = 0; count < (3); count++){
player2board[i][j] = 'S';
player2board[i++][j]= 'S';

}

}
showPlayer2board(player2board);

System.out.print("Player two, where would you like your Destroyer(3) to start(Enter x and y coordinate): ");
i = i - 1;
j = j - 1;

while (player2board[i][j] == 'A' || player2board[i][j] == 'B' || player2board[i][j] == 'S'){
System.out.println("Spot already used choose a new start location: ");
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
if (direction == 'H'){
for (int count = 0; count < (3); count++){
player2board[i][j] = 'D';
player2board[i][j++]= 'D';
}

}	else {
for (int count = 0; count < (3); count++){
player2board[i][j] = 'D';
player2board[i++][j]= 'D';

}

}
showPlayer2board(player2board);

System.out.print("Player 2, Where would you like your Patrol Boat(2) to start(Enter x and y coordinate): ");
i = i - 1;
j = j - 1;
while (player2board[i][j] == 'A' || player2board[i][j] == 'B' || player2board[i][j] == 'S' || player2board[i][j] == 'D'){
System.out.println("Spot already used choose a new start location: ");
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
if (direction == 'H'){
for (int count = 0; count < (2); count++){
player2board[i][j] = 'P';
player2board[i][j++]= 'P';
}

}	else {
for (int count = 0; count < (2); count++){
player2board[i][j] = 'P';
player2board[i++][j]= 'P';

}

}
showPlayer2board(player2board);
}```
Last edited by afarag; 04-13-2014 at 03:13 AM.

2. ## Re: Battleship game

[code]
[/code]
to get highlighting and preserve formatting.

3. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
4

## Re: Battleship game

Hi,

first I want to tell you some ways to restructure your code so you can modify it more easily:

- If you have (more or less) the same code in multiple places, then you should always move that code into a separate function.
- Try to keep functions small.
- Try that a function is doing one thing ... (which is also given in the function name)

Now we could concentrate on your code:

- If you have code that might run multiple times, you should use a loop as construct. So you could have something like
Java Code:
```do {
// Get User Input
// Check Locations
// Set Ship if check was successfull
} while (<ShipNotPlaced>);```
- If you want to check the place of the ship, you have to check all points where the ship will be and not just the first one. (So you could build up a loop as you did with placing the ship.)

- If you want to check, if a cell is empty: Just check if the cell is not as it was initialized. That way you only have one comparison instead of multiple comparisons.

- you might want to get rid of "magic stuff". RIght now you use characters like A, B, C for different ships. One common way to code different stati somethig could have is an enumeration. So you could build an enumerations of field content which could then be "empty", "carrier", ... If you do this, the code will be easier to read, too, because you get somethinge like if (player2board[x][y] != FieldContent.empty).

With kind regards,

4. Member
Join Date
Apr 2014
Posts
4
Rep Power
0

## Re: Battleship game

Thank you! This really helped, ill try to keep in mind the previous stuff said as well so its less of a pain to repeat stuff, still in intro to comp sic :)