Sunday, April 13, 2014

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): ");
int i = IO.readInt();
int j = IO.readInt();
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
char direction = IO.readChar();
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 = IO.readInt();
j = IO.readInt();
i = i - 1;
j = j - 1;
while (player2board[i][j] == 'A'){
System.out.println("Spot already used choose a new start location: ");
i = IO.readInt();
j = IO.readInt();
i = i - 1;
j = j - 1;
}
System.out.print("Player two, Would you like it to be placed Horizontal('H') or Vertical('V')? ");
direction = IO.readChar();
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 = IO.readInt();
j = IO.readInt();
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 = IO.readInt();
j = IO.readInt();
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
direction = IO.readChar();
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 = IO.readInt();
j = IO.readInt();
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 = IO.readInt();
j = IO.readInt();
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
direction = IO.readChar();
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 = IO.readInt();
j = IO.readInt();
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 = IO.readInt();
j = IO.readInt();
i = i - 1;
j = j - 1;
}
System.out.print("Would you like it to be placed Horizontal('H') or Vertical('V')? ");
direction = IO.readChar();
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);
}




































































































































































































































































No comments:

Post a Comment