Untitled
unknown
java
3 years ago
5.8 kB
2
Indexable
public static void Battle() { Game.player1Turn(); Game.player2Turn(); GameBoard.printGuessing(); System.out.println(); System.out.println("Player 1 ships: " + BattleshipGame.player1Ships + " | Player 2 ships: " + BattleshipGame.player2Ships); System.out.println(); } public static void player1Turn() { System.out.println("\nIts Player 1's turn to attack!"); int x = -1, y = -1; do { Scanner sc = new Scanner(System.in); System.out.print("Enter X coordinate: "); x = sc.nextInt(); System.out.print("Enter Y coordinate: "); y = sc.nextInt(); if ((x >= 0 && x < 10) && (y >= 0 && y < 10)) //valid guess { if (BattleshipGame.grid[x][y] == "x") //if computer ship is already there; computer loses ship { System.out.println("You have hit one of the ships!"); BattleshipGame.grid[x][y] = "!"; // exclamation mark means a hit --BattleshipGame.player2Ships; } else if (BattleshipGame.grid[x][y] == " ") { System.out.println("Sorry, you missed"); BattleshipGame.grid[x][y] = "-"; } } else if ((x < 0 || x >= 10) || (y < 0 || y >= 10)) //invalid guess { System.out.println("You can't place ships outside the 10x10 grid"); } if (BattleshipGame.grid[x][y] == "C" && BattleshipGame.grid[x][y] == "C " && BattleshipGame.grid[x][y] == " C ") { System.out.println("You have sunken the Carrier"); } if (BattleshipGame.grid[x][y] == "b" && BattleshipGame.grid[x][y] == "b " && BattleshipGame.grid[x][y] == " b ") { System.out.println("You have sunken the Battleship"); } if (BattleshipGame.grid[x][y] == "c" && BattleshipGame.grid[x][y] == "c " && BattleshipGame.grid[x][y] == " c ") { System.out.println("You have sunken the Cruiser"); } if (BattleshipGame.grid[x][y] == "s" && BattleshipGame.grid[x][y] == "s " && BattleshipGame.grid[x][y] == " s ") { System.out.println("You have sunken the Submarine"); } if (BattleshipGame.grid[x][y] == "d" && BattleshipGame.grid[x][y] == " d ") { System.out.println("You have sunken the Destroyer"); } } while ((x < 0 || x >= 10) || (y < 0 || y >= 10)); //keep re-prompting till valid guess } public static void player2Turn() { System.out.println("\nIts Player 2's turn to attack!"); int x = -1, y = -1; do { Scanner input = new Scanner(System.in); System.out.print("Enter X coordinate: "); x = input.nextInt(); System.out.print("Enter Y coordinate: "); y = input.nextInt(); if ((x >= 0 && x < 10) && (y >= 0 && y < 10)) //valid guess { if (BattleshipGame.grid[x][y] == "C" || BattleshipGame.grid[x][y] == "C " || BattleshipGame.grid[x][y] == " C " || BattleshipGame.grid[x][y] == "b" || BattleshipGame.grid[x][y] == "b " || BattleshipGame.grid[x][y] == " b " || BattleshipGame.grid[x][y] == "c" || BattleshipGame.grid[x][y] == "c " || BattleshipGame.grid[x][y] == " c " || BattleshipGame.grid[x][y] == "s" || BattleshipGame.grid[x][y] == "s " || BattleshipGame.grid[x][y] == " s " || BattleshipGame.grid[x][y] == " d " || BattleshipGame.grid[x][y] == "d") //if computer ship is already there; computer loses ship { System.out.println("You have hit one of the ships!"); BattleshipGame.grid[x][y] = "!"; // exclamation mark means a hit --BattleshipGame.player2Ships; } else if (BattleshipGame.grid[x][y] == "- ") { System.out.println("Sorry, you missed"); BattleshipGame.grid[x][y] = "- "; } } else if ((x < 0 || x >= 10) || (y < 0 || y >= 10)) //invalid guess { System.out.println("You can't place ships outside the 10x10 grid"); } if (BattleshipGame.grid[x][y] == "C" && BattleshipGame.grid[x][y] == "C " && BattleshipGame.grid[x][y] == " C ") { System.out.println("You have sunken the Carrier"); } if (BattleshipGame.grid[x][y] == "b" && BattleshipGame.grid[x][y] == "b " && BattleshipGame.grid[x][y] == " b ") { System.out.println("You have sunken the Battleship"); } if (BattleshipGame.grid[x][y] == "c" && BattleshipGame.grid[x][y] == "c " && BattleshipGame.grid[x][y] == " c ") { System.out.println("You have sunken the Cruiser"); } if (BattleshipGame.grid[x][y] == "s" && BattleshipGame.grid[x][y] == "s " && BattleshipGame.grid[x][y] == " s ") { System.out.println("You have sunken the Submarine"); } if (BattleshipGame.grid[x][y] == "d" && BattleshipGame.grid[x][y] == " d ") { System.out.println("You have sunken the Destroyer"); } } while ((x < 0 || x >= 10) || (y < 0 || y >= 10)); //keep re-prompting till valid guess } public static void gameOver() { System.out.println("Player 1 ships: " + BattleshipGame.player1Ships + " | Player 2 ships: " + BattleshipGame.player2Ships); if (BattleshipGame.player1Ships > 0 && BattleshipGame.player2Ships <= 0) { System.out.println("Hooray! You won the battle :)"); } else { System.out.println("Sorry, you lost the battle"); } System.out.println(); }
Editor is loading...