// used by player_guess() to determine if player's guess is valid public static boolean test_player_guess(String guess) { String alpha1 = guess.substring(0, 1); int numb1 = 0; try { numb1 = Integer.parseInt(guess.substring(1)); } catch (Exception ex) { out.println("Invalid coordinate!"); return false; } int alpha2 = 300; int numb2 = 300; for (int i = 1; i < grid_size; i++) { if (alpha1.toLowerCase().equals(AI_grid.get_grid()[i][0].toLowerCase())) { alpha2 = i; } if (numb1 == Integer.parseInt(AI_grid.get_grid()[0][i])) { numb2 = i; } } if (alpha2 == 300 || numb2 == 300) { out.println("Invalid input. Please try again"); return false; } return true; }
/* asks the player for the position of all ships, it adds each ship to the playerShips arraylist and adds the ships to the player grid. */ public static void player_setup() { Player_grid.print_grid(); String input, ship_name; String type = "player"; for (int i = 0; i < num_of_ships; i++) { ship_name = fleet_list.get(i); set_ship_size(ship_name); out.println("Please give a coordinate to hide your " + ship_name + " : (ex. 'A1')"); out.println(ship_name + " size: " + ship_size); input = sc.next(); get_coord(input); out.println("Which direction would you like your " + ship_name + " to face?:"); out.print( "Orientation Key:" + "\n" + "0 - up" + "\n" + "1 - right" + "\n" + "2 - down" + "\n" + "3 - left" + "\n"); try { orientation = sc.nextInt(); } catch (InputMismatchException ex) { out.println("Invalid position or invalid input for orientation"); } set_ship_orientation(y, x, orientation, ship_size); try { if (is_location_free(x, y, ship_size, Player_grid)) { player_ships.add( new Battleship( ship_size, ship_name, x, y, width, height)); // add ship to player ships Player_grid.add_ship( x, y, width, height, ship_size, type, ship_name); // prints ship to grid } else { out.println("Invalid Location. Try again"); i--; } } catch (Exception e) { out.println("Invalid Location. Try again"); i--; } Player_grid.print_grid(); } reset_joint_variables(); }
// used by run_game() to check if there are any ships alive on the grid public static boolean are_ships_alive(BattleshipGrid grid) { for (int j = 0; j < grid_size; j++) { for (int k = 0; k < grid_size; k++) { if (grid.get_symbol(k, j).equals(grid.ship_body_symbol)) { return true; } } } return false; }
// selects a guess from player public static void player_guess() { String guess; out.println("Enter a guess: (ex. G1)"); // check if player gives a valid guess (e.g. "E5") do { guess = sc.next(); } while (!test_player_guess(guess)); // if player guess is repeated, ask for another guess while (is_player_guess_repeated(guess)) { out.println("You have already guess that. Try again."); do { guess = sc.next(); } while (!test_player_guess(guess)); } out.println("player guess: " + guess); // convert player guess to array value get_coord(guess); // if string value at position guessed equals ship_body_symbol, set it to hit_symbol if (AI_grid.get_symbol(x, y).equals(AI_grid.ship_body_symbol)) { out.println("HIT!"); AI_grid.set_symbol(x, y, AI_grid.hit_symbol); AI_grid_for_player.set_symbol(x, y, AI_grid_for_player.hit_symbol); } else { out.println("Miss..."); AI_grid.set_symbol(x, y, AI_grid.miss_symbol); AI_grid_for_player.set_symbol(x, y, AI_grid_for_player.miss_symbol); } set_ship_destroyed(AI_ships, AI_grid); AI_grid_for_player.print_grid(); }
// checks if chosen position is available on the grid by comparing the symbols of the positions // with the water symbol public static boolean is_location_free(int x, int y, int ship_size, BattleshipGrid grid) { for (int i = 0; i < ship_size; i++) { if (!grid.get_symbol(x, y).equals(grid.water_symbol)) { return false; } if (width == 1) { y++; } else { x++; } } return true; }
// method checks if a ship has been destroyed by by comparing the number of hit symbols of a ship // to its size public static void set_ship_destroyed(ArrayList<Battleship> ship, BattleshipGrid grid) { int count = 0; for (Battleship s : ship) { for (String a : s.get_coords()) { get_coord(a); if (grid.get_symbol(x, y).equals(grid.hit_symbol)) { count++; } } if (count == s.get_size()) { s.set_destroyed(true); out.println(s.get_name() + " destroyed!"); } count = 0; } }
// used by player_guess() to determine if guess is repeated public static boolean is_player_guess_repeated(String guess) { get_coord(guess); return (!AI_grid.get_symbol(x, y).equals(AI_grid.water_symbol) && !AI_grid.get_symbol(x, y).equals(AI_grid.ship_body_symbol)); }