rendered paste body
public class battleShip {
static PlayerBoard[] playerBoard = {new PlayerBoard(10, 10, 1), new PlayerBoard(10, 10, 2)};
static final int emptySpace = 0, shipSpace = 1, missileMiss = 2, missileHit = 3;
static final int aircraft_carrier_length = 5, battleship_length = 4, submarine_length = 3, destroyer_length = 2;
static int turn = 0;
static int[] playerHits = {0, 0};
public static void main(String[] Args) {
System.out.println("Player 1, this is your board.\n");
playerBoard[0].printBoard();
placeShips(playerBoard[0], 1);
System.out.println("Player 2, this is your board.\n");
playerBoard[1].printBoard();
placeShips(playerBoard[1], 2);
int x, y, player, opponent;
while (true) { //get this working
player = turn % 2;
opponent = ((turn + 1) % 2);
x = -1; y = -1;
System.out.println("Player " + (player + 1) + ", fire your missiles.");
while (!playerBoard[opponent].spaceIsValid(x, y) || !playerBoard[opponent].spaceIsUnfired(x, y)) {
System.out.print("X coordinate: ");
x = Keyboard.readInt();
System.out.print("Y coordinate: ");
y = playerBoard[opponent].gameBoardColumn(x).length - 1 - Keyboard.readInt();
if(!playerBoard[opponent].spaceIsValid(x, y)) {
System.out.println("Please aim your missile onto the board.");
x = -1; y = -1;
}
if (playerBoard[opponent].spaceIsValid(x, y) && !playerBoard[opponent].spaceIsUnfired(x, y)) {
System.out.println("Please fire your missile into a new location.");
x = -1; y = -1;
}
}
fireMissiles(x, y);
if (playerHits[player] == 14) {
System.out.println("Player " + player + " you win!");
return;
}
turn++;
}
}
public static void fireMissiles(int x_pos, int y_pos) {
int player = (turn % 2), opponent = ((turn + 1) % 2);
if (playerBoard[opponent].spaceIsUnoccupied(x_pos, y_pos)) {
playerBoard[player].setHiddenBoardSpace(missileMiss, x_pos, y_pos);
playerBoard[opponent].setGameBoardSpace(missileMiss, x_pos, y_pos);
System.out.println("Miss!");
return;
}
playerBoard[player].setHiddenBoardSpace(missileHit, x_pos, y_pos);
playerBoard[opponent].setGameBoardSpace(missileHit, x_pos, y_pos);
playerHits[player]++;
System.out.println("Direct hit!");
}
public static void placeShips(PlayerBoard board, int player) {
board.placeShip("Aircraft Carrier", aircraft_carrier_length);
board.printBoard();
board.placeShip("Battleship", battleship_length);
board.printBoard();
board.placeShip("Submarine", submarine_length);
board.printBoard();
board.placeShip("Destroyer", destroyer_length);
board.printBoard();
System.out.println("Player " + player + ", you have placed all of your ships.\n");
}
}