|
| 1 | +from mastermind.core.models.game import Game |
| 2 | +from mastermind.core.models.game_state import get_winner |
| 3 | +from mastermind.core.services.gameboard_service import GameboardService |
| 4 | + |
| 5 | + |
| 6 | +class GameEndedException(Exception): |
| 7 | + pass |
| 8 | + |
| 9 | + |
| 10 | +class GameNotStartedException(Exception): |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +class GameService: |
| 15 | + def __init__(self, game: Game) -> None: |
| 16 | + """Initializes a new game service with the given game. |
| 17 | +
|
| 18 | + Args: |
| 19 | + game (Game): The game to be managed. |
| 20 | + """ |
| 21 | + |
| 22 | + self._game_board = game.game_board |
| 23 | + self._game_configuration = game.game_configuration |
| 24 | + self._game_entities = game.game_entities |
| 25 | + self._game_state = game.game_state |
| 26 | + self._gameboard_service = GameboardService(self._game_board) |
| 27 | + |
| 28 | + def add_round(self, guess: tuple[int, ...], feedback: tuple[int, int]) -> None: |
| 29 | + """Adds a new game round with the player's guess and corresponding feedback. |
| 30 | +
|
| 31 | + Appends the round to game rounds and clears the undo stack to prevent branching. |
| 32 | + This method should be called from the GameController, not directly by the player. |
| 33 | +
|
| 34 | + Args: |
| 35 | + guess (tuple[int, ...]): A tuple representing the player's current guess. |
| 36 | + feedback (tuple[int, int]): A tuple containing the number of correct and misplaced pegs. |
| 37 | +
|
| 38 | + Examples: |
| 39 | + >>> game = create_new_game(GameConfiguration(NUMBER_OF_COLORS=3, NUMBER_OF_DOTS=4, ATTEMPTS_ALLOWED=5, GAME_MODE=GameMode.PVP)) |
| 40 | + >>> service = GameService(game) |
| 41 | + >>> service.add_round((1, 2, 3, 4), (1, 0)) |
| 42 | + >>> service.game_rounds |
| 43 | + [GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(1, 0))] |
| 44 | +
|
| 45 | + Raises: |
| 46 | + GameEndedException: When trying to add a round to a game that has ended. |
| 47 | + """ |
| 48 | + |
| 49 | + if self._game_state.game_over: |
| 50 | + raise GameEndedException("Cannot add round to game that has ended.") |
| 51 | + |
| 52 | + self._gameboard_service.add_round(guess, feedback) |
| 53 | + self._game_state.game_started = True |
| 54 | + self._game_state.winner = get_winner( |
| 55 | + num_attempts=len(self._game_board), |
| 56 | + max_attempts=self._game_configuration.ATTEMPTS_ALLOWED, |
| 57 | + last_feedback=self._gameboard_service.game_rounds[-1].FEEDBACK, |
| 58 | + number_of_dots=self._game_configuration.NUMBER_OF_DOTS, |
| 59 | + ) |
| 60 | + |
| 61 | + def undo(self) -> None: |
| 62 | + """Undo the most recent game round. |
| 63 | +
|
| 64 | + Examples: |
| 65 | + >>> game = create_new_game(GameConfiguration(NUMBER_OF_COLORS=3, NUMBER_OF_DOTS=4, ATTEMPTS_ALLOWED=5, GAME_MODE=GameMode.PVP)) |
| 66 | + >>> service = GameService(game) |
| 67 | + >>> service.add_round((1, 2, 3, 4), (1, 0)) |
| 68 | + >>> game.game_board.game_rounds |
| 69 | + [GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(1, 0))] |
| 70 | + >>> service.undo() |
| 71 | + >>> game.game_board.game_rounds |
| 72 | + [] |
| 73 | +
|
| 74 | + Raises: |
| 75 | + GameNotStartedException: When trying to undo game rounds before the game has started. |
| 76 | + GameEndedException: When trying to undo game rounds after the game has ended. |
| 77 | + """ |
| 78 | + |
| 79 | + if not self._game_state.game_started: |
| 80 | + raise GameNotStartedException( |
| 81 | + "Cannot undo game rounds before game has started." |
| 82 | + ) |
| 83 | + |
| 84 | + if not self._game_state.game_over: |
| 85 | + raise GameEndedException("Cannot undo game rounds after game has ended.") |
| 86 | + |
| 87 | + self._gameboard_service.undo() |
| 88 | + |
| 89 | + def redo(self) -> None: |
| 90 | + """Restores the most recently undone game round. |
| 91 | +
|
| 92 | + Examples: |
| 93 | + >>> game = create_new_game(GameConfiguration(NUMBER_OF_COLORS=3, NUMBER_OF_DOTS=4, ATTEMPTS_ALLOWED=5, GAME_MODE=GameMode.PVP)) |
| 94 | + >>> service = GameService(game) |
| 95 | + >>> service.add_round((1, 2, 3, 4), (1, 0)) |
| 96 | + >>> service.undo() |
| 97 | + >>> service.redo() |
| 98 | + >>> service.game_rounds |
| 99 | + [GameRound(GUESS=(1, 2, 3, 4), FEEDBACK=(1, 0))] |
| 100 | +
|
| 101 | + Raises: |
| 102 | + GameNotStartedException: When trying to redo game rounds before the game has started. |
| 103 | + """ |
| 104 | + |
| 105 | + if not self._game_state.game_started: |
| 106 | + raise GameNotStartedException( |
| 107 | + "Cannot redo game rounds before game has started." |
| 108 | + ) |
| 109 | + |
| 110 | + self._gameboard_service.redo() |
0 commit comments