|
| 1 | +import json |
| 2 | +import random |
| 3 | +from board import Board |
| 4 | +from player import HumanPlayer, AIPlayer |
| 5 | +from ship import Ship |
| 6 | + |
| 7 | +class Game: |
| 8 | + def __init__(self): |
| 9 | + opponent_choice = input("Do you want to play against AI or another player? (AI/player): ") |
| 10 | + if opponent_choice.lower() == "ai": |
| 11 | + self.ai_player = AIPlayer("AI") |
| 12 | + self.human_player = HumanPlayer() |
| 13 | + self.board1 = Board() |
| 14 | + self.board2 = Board() |
| 15 | + self.board1.setup_ships() |
| 16 | + self.ai_setup_ships() |
| 17 | + else: |
| 18 | + self.human_player = HumanPlayer() |
| 19 | + self.second_player = HumanPlayer() |
| 20 | + self.board1 = Board() |
| 21 | + self.board2 = Board() |
| 22 | + self.board1.setup_ships() |
| 23 | + self.board2.setup_ships() |
| 24 | + |
| 25 | + self.current_player = self.human_player |
| 26 | + self.is_game_over = False |
| 27 | + self.shots_taken = set() |
| 28 | + |
| 29 | + def ai_setup_ships(self): |
| 30 | + ships_to_place = [Ship(1) for _ in range(4)] + [Ship(2) for _ in range(3)] + [Ship(3) for _ in range(2)] + [Ship(4) for _ in range(1)] |
| 31 | + |
| 32 | + for ship in ships_to_place: |
| 33 | + placed = False |
| 34 | + while not placed: |
| 35 | + try: |
| 36 | + start_row = random.randint(0, self.board2.size - 1) |
| 37 | + start_col = random.randint(0, self.board2.size - 1) |
| 38 | + orientation = random.choice(['horizontal', 'vertical']) |
| 39 | + self.board2.place_ship(ship, start_row, start_col, orientation) |
| 40 | + placed = True |
| 41 | + except ValueError: |
| 42 | + continue |
| 43 | + |
| 44 | + def shoot(self, row, col): |
| 45 | + if (row, col) in self.shots_taken: |
| 46 | + print("You have already shot at this position!") |
| 47 | + return False |
| 48 | + self.shots_taken.add((row, col)) |
| 49 | + |
| 50 | + target_board = self.board2 if self.current_player == self.human_player else self.board1 |
| 51 | + |
| 52 | + print(f"Player {self.current_player.name} is shooting at ({row}, {col})") |
| 53 | + hit = target_board.receive_shot(row, col) |
| 54 | + if hit: |
| 55 | + print("Hit!") |
| 56 | + else: |
| 57 | + print("Miss!") |
| 58 | + |
| 59 | + self.check_game_over() |
| 60 | + return hit |
| 61 | + |
| 62 | + def check_game_over(self): |
| 63 | + if self.board2.check_all_ships_sunk(): |
| 64 | + self.is_game_over = True |
| 65 | + print(f"{self.current_player.name} wins!") |
| 66 | + self.ask_to_play_again() |
| 67 | + |
| 68 | + def ask_to_play_again(self): |
| 69 | + choice = input("Want to play again? (yes/no): ") |
| 70 | + if choice.lower() == 'yes': |
| 71 | + self.reset_game() |
| 72 | + else: |
| 73 | + print("Thanks for playing!") |
| 74 | + |
| 75 | + def reset_game(self): |
| 76 | + self.board1 = Board() |
| 77 | + self.board2 = Board() |
| 78 | + self.current_player = self.human_player |
| 79 | + self.is_game_over = False |
| 80 | + |
| 81 | + opponent_choice = input("Do you want to play against AI or another player? (AI/player): ") |
| 82 | + if opponent_choice.lower() == "ai": |
| 83 | + self.ai_player = AIPlayer("AI") |
| 84 | + self.board1.setup_ships() |
| 85 | + self.ai_setup_ships() |
| 86 | + else: |
| 87 | + self.second_player = HumanPlayer() |
| 88 | + self.board1.setup_ships() |
| 89 | + self.board2.setup_ships() |
| 90 | + |
| 91 | + def save_game(self, filename): |
| 92 | + game_state = { |
| 93 | + 'board1': self.board1.grid, |
| 94 | + 'board2': self.board2.grid, |
| 95 | + 'current_player': self.current_player.name, |
| 96 | + 'is_game_over': self.is_game_over |
| 97 | + } |
| 98 | + with open(filename, 'w') as f: |
| 99 | + json.dump(game_state, f) |
| 100 | + |
| 101 | + def load_game(self, filename): |
| 102 | + with open(filename, 'r') as f: |
| 103 | + game_state = json.load(f) |
| 104 | + self.board1.grid = game_state['board1'] |
| 105 | + self.board2.grid = game_state['board2'] |
| 106 | + self.current_player = self.human_player if game_state['current_player'] == self.human_player.name else self.ai_player |
| 107 | + self.is_game_over = game_state['is_game_over'] |
| 108 | + |
| 109 | + def display_boards(self): |
| 110 | + print("Your Board:") |
| 111 | + self.board1.display(reveal=True) |
| 112 | + print("\nOpponent's Board (Hidden view):") |
| 113 | + self.board2.display() |
| 114 | + |
| 115 | + def play(self): |
| 116 | + while not self.is_game_over: |
| 117 | + print(f"Current player: {self.current_player.name}") |
| 118 | + self.display_boards() |
| 119 | + |
| 120 | + if self.current_player.is_ai: |
| 121 | + row, col = self.current_player.get_move() |
| 122 | + print(f"AI chose coordinates: ({row}, {col})") |
| 123 | + else: |
| 124 | + command = input("Enter 'save' to save or 'load' to load the game, or make a move (row col): ") |
| 125 | + if command.lower() == 'save': |
| 126 | + self.save_game('game_save.json') |
| 127 | + print("Game saved.") |
| 128 | + continue |
| 129 | + elif command.lower() == 'load': |
| 130 | + self.load_game('game_save.json') |
| 131 | + print("Game loaded.") |
| 132 | + continue |
| 133 | + |
| 134 | + try: |
| 135 | + row, col = map(int, command.split()) |
| 136 | + except ValueError: |
| 137 | + print("Invalid input. Please enter coordinates in 'row col' format.") |
| 138 | + continue |
| 139 | + |
| 140 | + hit = self.shoot(row, col) |
| 141 | + if not hit: |
| 142 | + self.current_player = self.ai_player if self.current_player == self.human_player else self.human_player |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + game = Game() |
| 146 | + game.play() |
0 commit comments