[docs]classMainUI:"""Class to handle the user menu interface."""def__new__(cls):ifnothasattr(cls,"instance"):cls.instance=super(MainUI,cls).__new__(cls)returncls.instance
[docs]defmain_menu(self)->bool:""" Display the main menu and handle user input. Return whether the user want to exit. """choice=MainMenu().get_option()ifchoice=="Start New Game":self.new_game_menu()returnTrueelifchoice=="Load Saved Game":self.saved_game_menu()returnTrueelifchoice=="Game History":GameHistoryMenu().display()returnTrueelifchoice=="Save and Exit":returnFalse# terminate the loop
[docs]defnew_game_menu(self)->bool:"""Display the new game menu and handle user input."""choice=NewGameMenu().get_option()ifchoice=="You vs Someone Else":GameController.start_new_game("HvH")returnTrueelifchoice=="You vs AI":GameController.start_new_game("HvAI")returnTrueelifchoice=="AI vs You":# GameController.start_new_game("AIvH")print("This feature is not implemented yet.")returnTrueelifchoice=="Solve External Game":# GameController.start_new_game("AIvAI")print("This feature is not implemented yet.")returnTrueelifchoice=="Return to Main Menu":returnFalse# terminate the loopelse:raiseAssertionError("Unexpected invalid choice.")
[docs]defsaved_game_menu(self):"""Display the saved game menu and handle user input."""choice=ResumeGameMenu().get_option()ifchoice==0:returnFalse# return to main menugame_index=list_continuable_games_index(retrieve_stored_games())GameController.resume_game(game_index[choice-1])# -1 since first option is 1returnTrue
[docs]defrun(self):"""Run the game."""print("Welcome to Mastermind!")whileself.main_menu():pass# keep calling self.main_menu() until it return Falseprint("Thank you for playing!")userdata._save_data()