[docs]classGameController:"""Communication between MainUI and the gameboard."""
[docs]@classmethoddefvalidate_input(cls,prompt:str,validator:Validator)->Any:"""Get user input and validate it."""whileTrue:try:user_input=input("\n"+prompt)returnvalidator().validate_value(user_input)exceptValidationErrorase:print(f"Invalid input. {e}")
[docs]@classmethoddefget_game_parameters(cls)->tuple[int,int,int]:"""Get the number of colors, number of dots, and number of rounds from user."""num_of_colors=cls.validate_input("Enter the number of colors (2-10): ",NumberOfColors)num_of_dots=cls.validate_input("Enter the number of dots (2-10): ",NumberOfDots)max_attempts=cls.validate_input("Enter the maximum number of attempts: ",MaximumAttempts)returnnum_of_colors,num_of_dots,max_attempts
[docs]@classmethoddefstart_new_game(cls,game_mode:str)->None:"""Start a new game."""ifgame_modenotin["HvH","HvAI","AIvH","AIvAI"]:raiseAssertionError("Unexpected invalid game mode.")parameters=cls.get_game_parameters()# get user inputgame=Game(*parameters,game_mode)# create a new gameexit_state=game.start_game()# start the game and retrieve exit stateifexit_state!="d":# "d" is discard, do not saveGameHistoryManager.save_game(game)# save the game
[docs]@classmethoddefresume_game(cls,game_index:int)->None:"""Resume a saved game."""# Retrieve gamegame=userdata.saved_games[game_index]["game"]# Resume game and retrieve exit stateexit_state=game.resume_game()# Update saved games if not game discardedifexit_state=="d":# or delete it if discardeduserdata.saved_games.pop(game_index)else:userdata.saved_games[game_index]=GameHistoryManager().generate_meta_data(game)