def tour_of_four_stools(model, delay_btw_moves=0.5, animate=False): """ Move a tower of cheeses from the first stool in model to the fourth. @type model: TOAHModel TOAHModel with tower of cheese on first stool and three empty stools @type delay_btw_moves: float time delay between moves if console_animate is True @type animate: bool animate the tour or not """ mock_model = TOAHModel(model.get_number_of_stools()) mock_model.fill_first_stool(model.get_number_of_cheeses()) number_cheeses = model.get_number_of_cheeses() four_stools_move(model, number_cheeses, 0, 1, 3) i = 0 move_seq = model.get_move_seq() move_times = move_seq.length() while animate: time.sleep(delay_btw_moves) ori = move_seq.get_move(i)[0] des = move_seq.get_move(i)[1] mock_model.move(ori, des) print(mock_model) i = i + 1 if i == move_times: animate = False
def play_loop(self): """ Play Console-based game. @param ConsoleController self: @rtype: None """ # moves should be in the form of two int, like 0 1 # there is a whitespace between them # where 0 is the index of source stool # and 1 is the index of destination stool # type 'want to exit' to exit the game m = TOAHModel(self.number_of_stools) m.fill_first_stool(self.number_of_cheeses) move_ = input('please enter your move') while move_ != 'want to exit': try: move(m, int(move_[0]), int(move_[2])) print(m) move_ = input('please enter your move') except IllegalMoveError as e: print(e) move_ = input('please enter your move') except IndexError: print('Invalid input!') move_ = input('please enter your move') except ValueError: print('invalid input!') move_ = input('please enter your move')
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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(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. """ ex = False print("ok hit e to exit and m to move") while ex == False: I = input("so whatcha gonna do ") try: if I == "e": print("GG") ex = True print(self.model) elif I == "m": o = int(input("which cheese block you movin? ")) if o not in range(len(self.model.stools)): raise FalseMoveError() d = int(input("where you movin it to? ")) if d not in range(len(self.model.stools)): raise FalseMoveError() move(self.model, o, d) print(self.model) else: raise FalseMoveError() except FalseMoveError: print("Not a valid input") except ValueError: print("That's not a number")
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_stools = number_of_stools self.number_of_cheeses = number_of_cheeses self.toah = TOAHModel(number_of_stools) self.toah.fill_first_stool(number_of_cheeses) self.instructions = \ ('The objective of the game is to move the given stack of cheeses \n' 'to the right most stool with the least amount of moves.\n' 'To move a block of cheese from stool X to stool Y \n' 'enter stool numbers in the format x,y\n' "Enter 'Info' if you wish to read this message again\n" "Enter 'Quit' to exit the game") def play_loop(self): """ Play Console-based game. @param ConsoleController self: @rtype: None """ print(self.instructions) command = input("Enter a move or type Quit to exit: ") while command != 'Quit': c = command.strip().split(',') try: if command == 'Info': print(self.instructions) elif len(c) != 2 or not (c[0].isnumeric() and c[1].isnumeric()): raise IllegalMoveError elif not 0 < int(c[0]) <= int(self.number_of_stools): print('Error ' + c[0] + ' is not within the range\n') elif not 0 < int(c[1]) <= int(self.number_of_stools): print('Error ' + c[1] + ' is not within the range\n') elif not self.toah.get_top_cheese(int(c[0]) - 1): print('stool ' + str(int(c[0]) - 1) + ' has No cheese!') else: move(self.toah, int(c[0]) - 1, int(c[1]) - 1) print(self.toah) except IllegalMoveError: print( 'Incorrect input, input must be positve #,#, Info or Quit\n' ) command = input("Enter a move or type Quit to exit: ") print("\nYou have successfully quit the game!")
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.tmodel = TOAHModel(self.number_of_stools) self.finished = False self.counter = 1 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. """ self.tmodel.fill_first_stool(self.number_of_cheeses) print(self.tmodel) while not self.finished: x = input('Which stool index do you want to move a cheese from?') if x == 'end': self.finished = True return None y = input('Which stool index do you want to move the cheese to?') if y == 'end': self.finished = True return None try: move(self.tmodel, int(x), int(y)) print(self.tmodel) except ValueError: print('That is an invalid move. Please try again') except IllegalMoveError: print('That is an invalid move. Please try again')
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._model = TOAHModel(number_of_stools) self._num_cheeses = number_of_cheeses self._num_stools = number_of_stools self._model.fill_first_stool(num_cheeses) #Fill 1st stool to start game 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. """ while not self._model.is_done(): start = input("Enter start stool: ") dest = input("Enter dest stool: ") if start == '' or dest == '': break else: start = int(start) - 1 dest = int(dest) - 1 # Now make the move. try: move(self._model, start, dest) except IllegalMoveError: print("Invalid Move!") print("Exiting the game now...\n")
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. """ is_int = False toah_game = TOAHModel(self.number_of_stools) toah_game.fill_first_stool(self.number_of_cheeses) print(toah_game) print("Welcome to the Tour of Anne Hoy Game.") print( "Enter a non-integer value such as a string(a) or a float(13.5) to quit the game." ) print('') run = True while run: try: cheese_origin = int( input("Choose a stool number to move the cheese from: ")) cheese_dest = int( input("Choose the stool you want to move the cheese to: ")) toah_game.move(cheese_origin, cheese_dest) print(toah_game) except IllegalMoveError: print("Invalid move. Please reenter.") print("") except IndexError: print("Invalid stool number. Please reenter.") print("") except ValueError: print("") print("Game Ended. Thank you for playing.") run = False
class ConsoleController: """ Controller for text console. """ model = None 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 """ #pass 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: @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. """ line = 0 while(1): line += 1 print("\nStage "+ str(line) + "\n***************************************************\n") print(self.model.__str__()) print("") cmd = int(input("1.move 2.quit : ")) if(cmd == 1): ori = int(input("Origin: ")) dest = int(input("Destination: ")) move(self.model,ori,dest) elif(cmd == 2): break else: print("\033[31;1mWrong input \033[0m") print("Bye~")
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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(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. """ while True: play = input('Please enter move: ') if play == "q": return False result = play.split() from_ = int(result[0]) to_ = int(result[1]) try: if to_ > len(self.model._stools): raise IllegalMoveError("Stool does not exist") else: self.model.move(from_, to_) except IllegalMoveError as IE: print(IE) pass print(self.model)
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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(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('Instruction: "1 3" means move top cheese from the first stool' 'to the third stool \nEnter "end" to exit.') print(self.model) count = 1 move_ = input('Enter move {}: '.format(count)) while move_ != 'end': try: move_list = move_.split() move(self.model, int(move_list[0]) - 1, int(move_list[1]) - 1) except IndexError: print('Invalid input!') except ValueError: print('Invalid input!') except IllegalMoveError as e: print(e) else: count += 1 print(self.model) move_ = input('Enter move {}: '.format(count))
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. """ game = TOAHModel(self.number_of_stools) game.fill_first_stool(self.number_of_cheeses) print("~~~~~~~~~~WELCOME TO FAIYAZ AND RASU'S GAME~~~~~~~~~~\n") print("~~~~~~~~~~OBJECTIVES~~~~~~~~~~") print( "To Move the entire stack of cheese from one stool to another stool\n" ) print("~~~~~~~~~~INSTRUCTIONS~~~~~~~~~~") print("1. Move cheese from one stool to another one at a time \n" "2. Can only move the top most cheese from a stool \n" "3. Can't place a larger cheese over a smaller cheese \n" "4. To exit the game, enter 'exit'") print('START \n \n ') print(game) start = 'Open' while start != 'exit': origin = int( input( "Enter the stool from which you want to move the cheese. " + "Stools range from 0 to " + str(self.number_of_stools - 1) + ': ')) final = int( input("Now enter the stool you want to " "move the slice of cheese to. Stools range from 0 to " + str(self.number_of_stools - 1) + '\n')) try: move(game, origin, final) except IllegalMoveError as error: print("!!!!!!You caused an error!!!!!!!") print(error) print(game) start = input("type 'exit' to exit")
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("Instruction: Player enter the start stool and destination stool" " at every time. \n" "Be ware you should enter the number of stool. " "i.e.'1' means first stool. Player can\n" "quit this game by enter 'E' when a messauge pop out.\n" "Be careful, " "You can't put big cheese over smaller ones! Good luck!\n" "==============================================================" "===================\n ") model = TOAHModel(self.number_of_stools) model.fill_first_stool(self.number_of_cheeses) ext = "" print(model) while ext is not "E": try: origin = int(input("Please enter the origin of movement---> ")) dest = int( input("Please enter the destination of " "movement---> ")) model.move(origin - 1, dest - 1) except IllegalMoveError as e: print(e) except ValueError: print("Please read instruction carefully," " and enter a positive integer.") finally: print(model) ext = input("Type E to exit game or any other to keep playing" "---> ") print("Game over")
def animate_hanoi(reference_model, delay): """ animate the movements in the console window one by one; delay each movement by the given <delay> @type reference_model: TOAHModel @type delay: float @rtype: None """ toah = TOAHModel(reference_model.get_number_of_stools()) toah.fill_first_stool(reference_model.get_number_of_cheeses()) for i in range(reference_model.number_of_moves()): movement = reference_model.get_move_seq().get_move(i) toah.move(movement[0], movement[1]) time.sleep(delay) print(toah)
def tour_of_four_stools(model, delay_btw_moves=0.5, animate=False): """Move a tower of cheeses from the first stool in model to the fourth. @type model: TOAHModel TOAHModel with tower of cheese on first stool and three empty stools @type delay_btw_moves: float time delay between moves if console_animate is True @type animate: bool animate the tour or not """ move_cheeses(model, model.get_number_of_cheeses(), 0, 1, 2, 3) if animate: new_model = TOAHModel(4) new_model.fill_first_stool(model.get_number_of_cheeses()) for i in range(model.number_of_moves()): new_model.move(model.get_move_seq().get_move(i)[0], model.get_move_seq().get_move(i)[1]) print(new_model) time.sleep(delay_btw_moves)
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. """ m = TOAHModel(stool) m.fill_first_stool(cheese) print(m) cond_check = True while cond_check: move_lst = [] user_input = input('give me instruction') # check condition check_end(user_input) if cond_check: user_input = user_input.split(',') try: move_lst.append(int(user_input[0])) move_lst.append(int(user_input[1])) move(m, move_lst[0], move_lst[1]) except Exception as a: print("input should be in the format int,int ", a) finally: print(m) if len(m.get_stools_lst()[-1]) == cheese: print("you win")
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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(number_of_cheeses) self.number_of_stools = number_of_stools def play_loop(self): """ Play Console-based game. @param ConsoleController self: @rtype: None """ print("Instructions: To move a cheese from the first stool to " "the second, input '0 to 1' \n Enter 'ex' to exit") move_count = 1 print(self.model) move_in = input('Enter Move ' + str(move_count) + ':') while move_in != 'ex': move_in = move_in.split() if len(move_in) == 3 and move_in[1] == 'to' and \ (move_in[0].isnumeric() and move_in[2].isnumeric()): try: self.model.move(int(move_in[0]), int(move_in[-1])) move_count += 1 except IllegalMoveError as e: print(e) else: print("Invalid Input Syntax") print(self.model) move_in = input('Enter Move ' + str(move_count) + ':')
def tour_of_four_stools(model, delay_btw_moves=0.5, animate=False): """Move a tower of cheeses from the first stool in model to the fourth. @type model: TOAHModel TOAHModel with tower of cheese on first stool and three empty stools @type delay_btw_moves: float time delay between moves if CONSOLE_ANIMATE is True @type animate: bool animate the tour or not """ move_four_stools(model, model.get_number_of_cheeses(), [0, 3, 1, 2]) if animate: animate_model = TOAHModel(4) animate_model.fill_first_stool(model.get_number_of_cheeses()) move_seq = model.get_move_seq() print(animate_model) for i in range(move_seq.length()): (src_stool, dst_stool) = move_seq.get_move(i) time.sleep(delay_btw_moves) animate_model.move(src_stool, dst_stool) print(animate_model)
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')
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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(number_of_cheeses) #the complete model of the model self.model_complete = TOAHModel(number_of_stools) self.model_complete._stools[number_of_stools - 1] = self.model._stools[0][0:] 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. % press q to quit - starting the game: after - """ #printing out the instruction for game print('***___________INSTRUCTION___________***\n\ 1. type "q" to exit the game at any stage \n 2.\ the format of which you want to enter your move is as follows\ \n >>> starting stool, target stool \n \ this will move the upper most cheese on the starting stool to\ top of the target stool') #initializing the user input user_input = '' #initializing whether the game has finished or not has_solved = False while user_input != 'q' and not has_solved: print(self.model) user_input = input('Please enter the moving order: ') if user_input != 'q': try: positions = user_input.split(',') start_pos = int(positions[0]) dest_pos = int(positions[1]) move(self.model, start_pos, dest_pos) except: print('Illegal Move, please enter the move again') finally: if self.model == self.model_complete: print(self.model) print('Congrats!! you have solved the game') has_solved = True print('The Game is Over.\nThanks for playing the game.')
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.cheese_model = TOAHModel(number_of_stools) self.cheese_model.fill_first_stool(number_of_cheeses) self.number_of_cheese = number_of_cheeses self.number_of_stools = number_of_stools print("Welcome to the game. ") print("You will be asked to input the number of cheeses and "\ "stools that you would like to play with") print("Make sure your inputs are a single int of how many cheeses "\ "or stools you would like.") print("If you would like to stop playing, simply type 'quit'. ") 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. """ while True: answer_first_stool = input("Enter the first stool: ") answer_last_stool = input("Enter the last stool: ") if answer_first_stool == "quit" or answer_last_stool == "quit": break else: new_answer_last_stool = int(answer_last_stool) new_answer_first_stool = int(answer_first_stool) if (new_answer_last_stool < self.number_of_stools) and \ (new_answer_first_stool < self.number_of_stools): move(self.cheese_model, new_answer_first_stool, new_answer_last_stool) else: raise IllegalMoveError( "Cannot place larger cheese on top of smaller") print(self.cheese_model)
class ConsoleController: """ Controller for text console. ==Attribute== @param TOAHModel model: a list version of the TOAHModel """ 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.model = TOAHModel(number_of_stools) self.model.fill_first_stool(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("> To move a stack, first enter which tower number it's " "currently on followed by a space and then the tower number you " "want to move the stack to. \n \n" "> The first tower is tower number 1 and each tower number" " after that goes up by one (ie. the second tower is tower" " number 2). \n \n" "> To exit the game simply type 'exit' (as an isolated word" ") in the input command. \n \n" "> The objective of the game is to move all pieces from the" " initial tower, to the last tower. \n \n" "> The game's rules are: \n \n" "1. You can never move a bigger piece on a smaller piece. \n" "2. You can only move one piece at a time \n" "3. All disks must only be moved to viable towers \n") print(self.model) move_input = input("Enter a move or command: ").split() while 'exit' not in move_input: if len(move_input) == 2 and move_input[0].isdigit() and\ move_input[1].isdigit(): try: source = int(move_input[0]) - 1 destination = int(move_input[1]) - 1 self.model.move(source, destination) print(self.model) except IllegalMoveError as error: print(error) move_input = input("Enter a move or command: ").split() else: move_input = \ input("Please enter a legal move or command: ").split()
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: @param int number_of_cheeses: @param int 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: @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. """ inp = 0 while inp != 'end': inp = input(""" - specify the origin and destination of the cheese in the following format: "orig dest" - in that order with a space in between, be sure both are ints - ensure that you do not place bigger or equal cheese on smaller cheese - to exit the game type "end" """) try: if inp != 'end': #orig, dest = inp.split(' ')[0], inp.split(' ')[1] orig, dest = int(inp.split(' ')[0]), int(inp.split(' ')[1]) if len(self.model._stools[dest] ) == 0: # you forgot this again move(self.model, orig, dest) print(str(self.model)) # elif self.model.get_top_cheese(orig) < self.model.get_top_cheese(dest): elif (self.model.get_top_cheese(orig).size < self.model.get_top_cheese(dest).size): move(self.model, orig, dest) print(str(self.model)) else: print('smaller cheese please') except IndexError: print( 'specify the origin and destination of the cheese in the following format: "orig dest"' ) else: 'GAME OVER'
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 """ self._tm = TOAHModel(number_of_stools) self._tm.fill_first_stool(number_of_cheeses) self._game_over = False def game_over(self): """Update _game_over to True. Print Game Over statement. >>> cc = ConsoleController(4,5) >>> cc.game_over() 'Thanks for Playing :)' """ self._game_over = True print("Thanks for playing :)") 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("Good Luck!", ":)") # While the user hasn't chosen to while self._game_over == False: # Outputs string representation of puzzle after move is executed print('Current Arrangement:\n') print(self._tm) exit = input( "To keep playing, press enter. Otherwise, type \'exit\' then hit enter. " ) if exit == "exit": return self.game_over() prev_stool = input( "Which stool would you like to move a cheese block from? ") new_stool = input("Where would you like to move it? ") try: move(self._tm, int(prev_stool) - 1, int(new_stool) - 1) except ValueError: string = "\nPlease input a positive integer" stars = "*" * len(string) print(stars, string) print(stars) except IllegalMoveError: string = "\nYou can't do that...\n" stars = "*" * len(string) print(stars, string) print(stars) if self._tm.mission_accomplished(): print(self._tm) print("Well Done!") return
print("move final " + str(m) + " took " + str(a)) count += a #count += move_cheeses(model, n-m, 0, 1, 2, animate) #count += move_cheeses(model, m, 0, 1, 3, animate) #count += move_cheeses(model, n-m, 2, 0, 3, animate) return count if __name__ == '__main__': #mod = TOAHModel(4) #mod.fill_first_stool(5) #print(tour_of_four_stools(mod,animate=True)) #move_cheeses(mod, 8, 0, 1, 2) num_cheeses = 100 delay_between_moves = 0.5 console_animate = False # DO NOT MODIFY THE CODE BELOW. four_stools = TOAHModel(4) four_stools.fill_first_stool(number_of_cheeses=num_cheeses) tour_of_four_stools(four_stools, console_animate, delay_between_moves) print(four_stools.number_of_moves()) # Leave files below to see what python_ta checks. # File tour_pyta.txt must be in same folder import python_ta python_ta.check_all(config="tour_pyta.txt")
# 2. Then use predefined steps to move the 2 cheeses from stool 0 to stool 3 # 3. Then move all the cheeses that were left on stool 2 from step 1 to # stool 3 using stool 0 and stool 1 as helper stools else: solve1(cheese - 2, model, original, helper1, destination, helper2) model.move(original, helper1) print(model) time.sleep(delay_between_moves) model.move(original, destination) print(model) time.sleep(delay_between_moves) model.move(helper1, destination) print(model) time.sleep(delay_between_moves) solve1(cheese - 2, model, helper2, original, helper1, destination) if __name__ == '__main__': num_cheeses = 16 delay_between_moves = 0.5 console_animate = False # DO NOT MODIFY THE CODE BELOW. four_stools = TOAHModel(4) four_stools.fill_first_stool(num_cheeses) tour_of_four_stools(four_stools, animate=console_animate, delay_btw_moves=delay_between_moves) print(four_stools.number_of_moves())
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. """ def exception_handler(origin, dest, model): """ Handles the exceptions @type origin: int @type dest: int @type model: TOAHModel @rtype: None|str """ try: move(model, origin, dest) except IllegalMoveError: print("That's invalid. Choose other stools. \n") else: pass print("=" * 10 + "WELCOME TO THE TOUR OF ANNE HOY GAME" + "=" * 10) print("\n\n\nInstructions:\n-To exit the game, type '-1' when" "asked for the moves\n-Each stool is numbered, starting" "from 1") print("---Rules---") print("*Enter all information in numeral numbers") print("**The goal of this game is to stack all the cheeses onto the" " last stool") print( "***It is only possible to add cheeses on top of a larger cheese") print("****Only can move existing cheese on a stool") print("*****to EXIT type in -1 as destination") print("") model = TOAHModel(self.number_of_stools) model.fill_first_stool(self.number_of_cheeses) print(model.__str__()) print('') print("Begin") print("") origin = 0 dest = 0 while origin != -1 and dest != -1: origin = int(input("Move from stool: ")) if origin == -1: break dest = int(input("To stool: ")) if dest == -1: break while ((origin > model.num_of_stools - 1) or (origin < 0) or (dest > model.num_of_stools - 1) or (dest < 0) or (len(model._stools[origin]) == 0) or (len(model._stools[dest]) > 0 and (model.get_top_cheese(origin).size > model.get_top_cheese(dest).size))): exception_handler(origin, dest, model) origin = int(input("Move from stool: ")) dest = int(input("To stool: ")) move(model, origin, dest) print(model.__str__())
class TestTOAH(unittest.TestCase): """Unit tests for Assignment 1.""" def setUp(self): self.toah = TOAHModel(4) self.optimals = [ 0, 1, 3, 5, 9, 13, 17, 25, 33, 41, 49, 65, 81, 97, 113, 129, 161, 193, 225, 257, 289, 321, 385, 449, 513, 577, 641, 705, 769, 897, 1025, 1153, 1281, 1409, 1537, 1665, 1793, 2049, 2305, 2561, 2817, 3073, 3329, 3585, 3841, 4097, 4609, 5121, 5633 ] def test_all_cheeses(self): for i in range(0, len(self.optimals)): self.toah = TOAHModel(4) self.toah.fill_first_stool(i) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[i]) def test_0_cheeses(self): self.toah.fill_first_stool(0) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[0]) def test_1_cheeses(self): self.toah.fill_first_stool(1) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[1]) def test_2_cheeses(self): self.toah.fill_first_stool(2) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[2]) def test_3_cheeses(self): self.toah.fill_first_stool(3) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[3]) def test_4_cheeses(self): self.toah.fill_first_stool(4) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[4]) def test_5_cheeses(self): self.toah.fill_first_stool(5) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[5]) def test_6_cheeses(self): self.toah.fill_first_stool(6) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[6]) def test_7_cheeses(self): self.toah.fill_first_stool(7) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[7]) def test_8_cheeses(self): self.toah.fill_first_stool(8) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[8]) def test_9_cheeses(self): self.toah.fill_first_stool(9) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[9]) def test_10_cheeses(self): self.toah.fill_first_stool(10) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[10]) def test_11_cheeses(self): self.toah.fill_first_stool(11) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[11]) def test_12_cheeses(self): self.toah.fill_first_stool(12) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[12]) def test_13_cheeses(self): self.toah.fill_first_stool(13) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[13]) def test_14_cheeses(self): self.toah.fill_first_stool(14) tour.tour_of_four_stools(self.toah) self.assertEqual(self.toah.number_of_moves(), self.optimals[14])
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(number_of_stools) self._model.fill_first_stool(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 ---Type initial stool and the destination to move " "the topmost cheese in the order - ''start stool(int)'," "'destination stool(int)''. And, to exit the function type 'e'" ". The initial stool's index is 0 and the fourth stool's index " "is 3" "") pot_move = input("enter move: ") while pot_move != 'e': try: check_comma(pot_move[1]) self._model.check_move(int(pot_move[0]), int(pot_move[2])) move(self._model, int(pot_move[0]), int(pot_move[2])) print(self._model) pot_move = input("enter move: ") except IndexError as ie: print(ie) print("index should be in range.") pot_move = input("enter move: ") except SyntaxError as ilme: print(ilme) print("Invalid syntax.") pot_move = input("enter move: ") except IllegalMoveError as ilme: print(ilme) print("enter again.") pot_move = input("enter move: ") except ValueError as ve: print(ve) print("enter again.") pot_move = input("enter move: ")