def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float=0.5, console_animate: bool=False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to animate the tour in the console delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ the_four_stools(model, model.number_of_cheeses(), 0, 1, 2, 3) # stool starts at 0 and ends at 3 if console_animate == True: x = model.get_move_seq() z = x.length() y = model.number_of_cheeses() model = TOAHModel(4) model.fill_first_stool(y) index = 0 time.sleep(delay_btw_moves) print(model) while index < z: time.sleep(delay_btw_moves) m = x.get_move(index) model.move(m[0],m[1]) time.sleep(delay_btw_moves) print(model) time.sleep(delay_btw_moves) index += 1
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float = 0.5, console_animate: bool = False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to use ConsoleController to animate the tour delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ #This first part is to make the right MoveSequence cheese_num = model.number_of_cheeses() moves = MoveSequence([]) four_stool_solution(moves, cheese_num, 0, 1, 2, 3) #Then we apply it to our existing model, animating with delay if #requested. if console_animate: for move in moves._moves: print(model) model.move(move[0], move[1]) time.sleep(delay_btw_moves) print(model) else: for move in moves._moves: model.move(move[0], move[1])
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float = 0.5, console_animate: bool = False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to animate the tour in the console delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ n = model.number_of_cheeses() list_of_moves = moves_list(n, 0, 1, 2, 3) if console_animate: for move in list_of_moves: TOAHModel.move(model, move[0], move[1]) print(model) print('Moving top cheese from {} to {}'.format(move[0], move[1])) time.sleep(delay_btw_moves) else: for move in list_of_moves: TOAHModel.move(model, move[0], move[1]) print('Moving top cheese from {} to {}'.format(move[0], move[1]))
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float = 0.5, console_animate: bool = False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to use ConsoleController to animate the tour delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ # Call helper functions to move the cheese # If console_animate is true, print each step if (console_animate is True): # Pass true in the parameter to print four_stool_hanoi(model, model.number_of_cheeses(), 0, 1, 2, 3, True) # Otherwise don't print it else: four_stool_hanoi(model, model.number_of_cheeses(), 0, 1, 2, 3, False)
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float=0.5, console_animate: bool=False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to animate the tour in the console delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ four_hanoi(model.number_of_cheeses(), 0, 1, 2, 3, console_animate, delay_btw_moves)
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float = 0.5, console_animate: bool = False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to animate the tour in the console delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ four_hanoi(model.number_of_cheeses(), 0, 1, 2, 3, console_animate, delay_btw_moves)
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float=0.5, console_animate: bool=False,): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to use ConsoleController to animate the tour delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ # store the number of cheeses in n, because it's shorter and easier n = model.number_of_cheeses() # create an empty list to store the moves (in tuple form) moves = [] # call the helper function so the solution can be stored in moves helper_four_stools(moves, n, 0, 1, 2, 3) # if user wants to animate if console_animate is True: # grab a movie one by one and apply it to the given model # until the moves list is empty while len(moves) != 0: # temp is basically the first move in the list # it's a tuple temp = moves[0] # delete that move from the original moves list because we # already saved it and we won't need it again moves = moves[1:] # since temp is a tuple, temp[0] is the original stool and # temp[1] is the destination stool: temp = (temp[0], temp[1]) model.move(temp[0], temp[1]) # if user wants a delay between moves, use sleep to pause time.sleep(delay_btw_moves) # print a visual representation of the model being changed print(model) # if user does not any animation, only the model is changed else: # exact same thing but this time there is no delay between moves while len(moves) != 0: # get origin stool temp = moves[0] # get destination stool moves = moves[1:] # change the model model.move(temp[0], temp[1])
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float=0.5, console_animate: bool=False): """Move a tower of cheeses from the first stool in model to the fourth. model - a TOAHModel with a tower of cheese on the first stool and three other empty stools console_animate - whether to use ConsoleController to animate the tour delay_btw_moves - time delay between moves in seconds IF console_animate == True no effect if console_animate == False """ src = 0 spare_1 = 1 spare_2 = 2 dest = 3 num_cheeses = model.number_of_cheeses() rec_four_stools(model, num_cheeses, src, spare_1, spare_2, dest, delay_btw_moves, console_animate)