class ConsoleController: """ Controller for text console. """ def __init__(self, number_of_cheeses, number_of_stools): """ Initialize a new ConsoleController self. @param ConsoleController self: @param int number_of_cheeses: @param int number_of_stools: @rtype: None """ self.number_of_cheeses = number_of_cheeses self.number_of_stools = number_of_stools self.model = TOAHModel(self.number_of_stools) self.model.fill_first_stool(self.number_of_cheeses) 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("Instructions: Entering '0 2' means move the top cheese from the\ first stool to the third stool. \n Enter 'e' to exit.") print(str(self.model)) n = 1 input_ = input("Enter Move {}: ".format(n)) while input_ != 'e': split_ = input_.strip(" ").split(" ") if len(split_) != 2 or split_[0].isalpha() or split_[1].isalpha(): print(IllegalMoveError("Please enter correct values!")) print(str(self.model)) input_ = input("Enter Move {}: ".format(n)) else: origin = int(split_[0]) dest = int(split_[1]) if origin > (self.model.get_number_of_stools() - 1) or origin <\ 0: print(IllegalMoveError("Origin stool does not exist!")) print(str(self.model)) input_ = input("Enter Move {}: ".format(n)) elif dest > (self.model.get_number_of_stools() - 1) or dest < 0: print(IllegalMoveError("Destination stool does not exist!")) print(str(self.model)) input_ = input("Enter Move {}: ".format(n)) elif len(self.model.get_stools()[origin]) == 0: print(IllegalMoveError( "Cannot move to cheese from an empty stool")) print(str(self.model)) input_ = input("Enter Move {}: ".format(n)) elif len(self.model.get_stools()[dest]) != 0 and \ self.model.get_stools()[dest][-1].size < \ self.model.get_stools()[origin][-1].size: print(IllegalMoveError( "Cannot place a bigger cheese on top of a smaller " "cheese!")) print(str(self.model)) input_ = input("Enter Move {}: ".format(n)) else: move(self.model, origin, dest) print(str(self.model)) n += 1 input_ = input("Enter Move {}: ".format(n))
class ConsoleController: """ Controller for text console. """ def __init__(self, number_of_cheeses, number_of_stools): """ Initialize a new ConsoleController self. @param ConsoleController self: the ConsoleController itself @param int number_of_cheeses: the number of cheeses @param int number_of_stools: the number of stools @rtype: None """ self.model = TOAHModel(number_of_stools) self.model.fill_first_stool(number_of_cheeses) def play_loop(self): """ Play Console-based game. @param ConsoleController self: the ConsoleController itself @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 spe instruction or if it denotes an invalid move (e.g. moving a cheese ontcifications given in youro 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(""" --------Welcome to the Game of Hanoi-------- Type two integers like x y (two integers x and y seperated by a space) to move a cheese from xth stool to yth stool. If the input move is invalid, an error message will be sent until you type a valid move. NO.{} represent the number of successful moves you have made. Type quit to exit the game. """) move_order = input('Please order a move or type quit to exit the game:') n = self.model.get_number_of_stools() while move_order != 'quit': try: from_stool = int(move_order.split()[0]) - 1 dest_stool = int(move_order.split()[1]) - 1 if len(move_order.split()) != 2: print("Hey, dude! Please follow the format!") elif from_stool == -1 or dest_stool == -1: print("Hey, dude! We don't have that stool!") else: move(self.model, from_stool, dest_stool) except IndexError: if len(move_order.split()) != 2: print("Hey, dude! Please follow the format!") elif from_stool + 1 > n or dest_stool + 1 > n: print("Hey, dude! We don't have that stool") else: print("Hey, dude! Please follow the format!") except IllegalMoveError: if from_stool == dest_stool: print("Hey, dude! Origin and destination stools must " "be distinct!") elif self.model.get_top_cheese(from_stool) is None: print("Hey, dude! Can't find a cheese from an empty stool!") elif self.model.get_top_cheese(from_stool).size > self.model\ .get_top_cheese(dest_stool).size: print('Hey, dude! A big cheese can not be on a small one!') except ValueError: print("Hey, dude! Don't type other characters!") print(self.model) move_order = input('NO.{} Give me a move:'. format(self.model.number_of_moves()))
class ConsoleController: """ Controller for text console. """ def __init__(self, number_of_cheeses, number_of_stools): """ Initialize a new ConsoleController self. @param ConsoleController self: @param int number_of_cheeses: @param int number_of_stools: @rtype: None """ if isinstance(number_of_cheeses, int) and isinstance( number_of_stools, int): self.number_of_cheeses = number_of_cheeses self.number_of_stools = number_of_stools self.model = TOAHModel(self.number_of_stools) self.model.fill_first_stool(self.number_of_cheeses) self.complete = self.model._stools[0][:] else: print("Please enter a NUMBER of cheeses and a NUMBER of stools") 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. """ flag = True player_input = '' orginial_stool = 0 destination_stool = 0 print( "\nThis is how you play.\n\nEnter from which stool you want to move to and its destination\nas an ordered pair, i.e. (cheese stool location, stool I want to move to), (0,1) means \nI want to move the top cheese on stool 1 to top of stool 2.\nAnd you can only stack smaller cheese on larger cheese: enter quit anytime to exit the game \nother wise we will keep playing\n" ) print() print("here is the model of the game") print(self.model) while flag: if self.complete == self.model._stools[self.number_of_stools - 1]: print("\ncongratulations you won!!!\n") print("You completed the puzzel in " + str(self.model.number_of_moves()) + " moves") flag = False else: player_input = input( "please enter the ordered pair of your movement: ") if player_input.lower() == 'quit': print('goodbye') flag = False else: r = player_input.split(',') if player_input[0] != '(' or player_input[-1] != ')': print( '\nPlease enter the NUMBER of your source and destination stool as an ordered pair!\n' ) else: try: orginial_stool = int(r[0][1:]) destination_stool = int(r[1][:-1]) except ValueError: print( '\nPlease enter the NUMBER of your source and destination stool\n' ) else: if orginial_stool < self.model.get_number_of_stools( ) and destination_stool < self.model.get_number_of_stools( ): move(self.model, orginial_stool, destination_stool) print(self.model) else: print( '\nPlease enter an appropriate stool and/or cheese\n' )