def play_loop(self): """ Play Console-based game. @param ConsoleController self: @rtype: None TODO: -Start by giving instructions about how to enter moves (which is up to you). Be sure to provide some way of exiting the game, and indicate that in the instructions. -Use python's built-in function input() to read a potential move from the user/player. You should print an error message if the input does not meet the specifications given in your instruction or if it denotes an invalid move (e.g. moving a cheese onto a smaller cheese). You can print error messages from this method and/or from ConsoleController.move; it's up to you. -After each valid move, use the method TOAHModel.__str__ that we've provided to print a representation of the current state of the game. """ show_instruction() won_message = False toah = TOAHModel(self.number_of_stools) model = toah.get_move_seq(). \ generate_toah_model(self.number_of_stools, self.number_of_cheeses) print(model) movement = input("Input your move: ") while movement != "end": try: result = extract_input(movement) move(model, result[0], result[1]) except IllegalMoveError as message: print(message) except ValueError: print('This is a invalid input') except IndexError: print('This is a invalid input') except TypeError: print('This is a invalid input') else: print(model) if len(model.get_stool()[-1]) == self.number_of_cheeses: movement = "end" won_message = True else: movement = input("Input your move: ") display_won_message(won_message)
class ConsoleController: """ Controller for text console. """ def __init__(self, num_of_cheeses, num_of_stools): """ Initialize a new ConsoleController self. @param ConsoleController self: @param int number_of_cheeses: @param int number_of_stools: @rtype: None """ self.toah_model = TOAHModel(num_of_stools) self.toah_model.fill_first_stool(num_of_cheeses) def process_input(self): """ Process Command. @param ConsoleController self: @rtype: bool/None """ command = input('Please enter the index of stools for next ' 'step ! ex: 0, 2: ').split(',') try: if len(command) == 1: if command[0].strip().lower() == 'help': print('######', 'help : Help Command ', '######') print('######', 'exit : Program Exit ', '######') print('######', 'quit : End Game ', '######') print('######', 'n,n : Input number of stools', '######') elif command[0].strip().lower() == 'quit': print('######', '### End Game ###', '######') return False elif command[0].strip().lower() == 'exit': print('######', '### Program Exit ###', '######') return False else: raise IndexError else: src_stool = int(command[0]) dst_stool = int(command[1]) move(self.toah_model, src_stool, dst_stool) except ValueError: raise except IndexError: raise except IllegalMoveError: raise def play_loop(self): """ Play Console-based game. @param ConsoleController self: @rtype: None TODO: -Start by giving instructions about how to enter moves (which is up to you). Be sure to provide some way of exiting the game, and indicate that in the instructions. -Use python's built-in function input() to read a potential move from the user/player. You should print an error message if the input does not meet the specifications given in your instruction or if it denotes an invalid move (e.g. moving a cheese onto a smaller cheese). You can print error messages from this method and/or from ConsoleController.move; it's up to you. -After each valid move, use the method TOAHModel.__str__ that we've provided to print a representation of the current state of the game. """ print('##########', '########################', '##########') print('##########', ' Start Game ', '##########') print('##########', '########################', '##########', '\n') print(self.toah_model, '\n') while True: try: if self.process_input() is False: break except ValueError: print('Not a number or Number is Zero! ex> 0,1 ') continue except IndexError: print('Number is src_stool, dst_stool! ex> 0,1 ') continue else: print(self.toah_model) print('Number of moves :', \ self.toah_model.get_move_seq().length(), '\n')