[docs]classPlayerLogic:""" Manages the logic for the players in the Mastermind game. Args: game (GameState): The state of the game. """def__init__(self,game_state:GameParameter)->None:self.game_state=game_state@propertydefGAME_MODE(self)->str:returnself.game_state.GAME_MODE
[docs]definitialize_players(self)->None:""" Initializes the players based on the game mode. """game_mode_mapping={"HvH":(HumanCodeCracker,HumanCodeSetter),"HvAI":(HumanCodeCracker,AICodeSetter),"AIvH":(AICodeCracker,HumanCodeSetter),"AIvAI":(AICodeCracker,ExternalCodeSetter),}ifself.GAME_MODEingame_mode_mapping:self.PLAYER_CRACKER,self.PLAYER_SETTER=(cls(self)forclsingame_mode_mapping[self.GAME_MODE])
[docs]defprocess_player_guessing(self)->Optional[str]:""" Processes the player's guessing logic. Returns: Optional[str]: A command from the player, if any. """whileself.game_state.win_statusisNone:guess=self.PLAYER_CRACKER.obtain_guess()# Process commandsifguessin{"q","d"}:# quit or discardreturnguessifguess=="u":# undoself._undo_logic()continueifguess=="r":# redoself._redo_logic()continuefeedback=self.PLAYER_SETTER.get_feedback(guess)# Process commandiffeedbackin{"q","d"}:# quit or discardreturnfeedbackiffeedback=="u":# undocontinue# since guess haven't been submitted, skip = undoprint(f"Feedback: {feedback}")self.submit_guess(guess,feedback)self.game_state.check_and_update_win_status()
def_undo_logic(self)->None:""" Handles the undo logic. """self.PLAYER_CRACKER.undo()self.PLAYER_SETTER.undo()self.game_state._board.remove_last()def_redo_logic(self)->None:""" Handles the redo logic. """guess=self.PLAYER_CRACKER.redo()feedback=self.PLAYER_SETTER.redo()self.submit_guess(guess,feedback)
[docs]defsubmit_guess(self,guess:Tuple[int,...],feedback:Tuple[int,...])->None:""" Submits a guess and its corresponding feedback to the game board. Args: guess (Tuple[int, ...]): The guess to be submitted. feedback (Tuple[int, ...]): The feedback for the guess. Raises: NotImplementedError: If the game has ended. """ifself.game_state.win_statusisnotNone:raiseNotImplementedError("Cannot make guess after game has ended.")self.PLAYER_CRACKER.clear_undo()self.PLAYER_SETTER.clear_undo()self.game_state._board.add_guess(guess,feedback)