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
print('=========================================================================') print("However, please choose how many cheeses you want on your stool: ") how_many_cheeses = input() if how_many_cheeses == 'Finish': print('Thanks for playing! ') sys.exit('You have chosen to exit the program. Bye!') while how_many_cheeses.isdigit() == False or int(how_many_cheeses) <= 0: print("Please enter a numeral value that is greater than 0") how_many_cheeses = input() try: how_many_cheeses = int(how_many_cheeses) except: print('Bad input, try again!') how_many_cheeses = int(input()) c1 = ConsoleController(how_many_cheeses,4) m5 = TOAHModel(c1.number_of_stools) m5.fill_first_stool(c1.number_of_cheeses) c1.play_loop() print('======================================================================') print('Below are the moves you made:') print(m5.get_move_seq()) x = m5.get_move_seq() print("Total moves made: "+str(x.length())) print("Play again soon!") print('======================================================================')
def tour_of_four_stools(model: TOAHModel, delay_btw_moves: float = 0.5, console_animate: bool = False) -> None: """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 """ def move_three_stools(n: int, source: int, intermediate: int, destination: int) -> None: """ Print moves to get n cheeses from source to destination, possibly using intermediate """ if n > 1: move_three_stools(n - 1, source, destination, intermediate) move_three_stools(1, source, intermediate, destination) move_three_stools(n - 1, intermediate, source, destination) else: model.move(source, destination) def move_four_stools(model: TOAHModel, n: int, source: int, intermediate1: int, intermediate2: int, destination: int) -> None: """ Print moves to get n cheeses from source to destination, possibly using intermediates """ def M(n) -> int: """ return the minimum moves of moving n Cheeses on four stools >>> M(4) 9 """ if n == 1: return 1 else: moves = [] for i in range(1, n): moves.append(2 * M(n - i) + 2**i - 1) global mo mo = moves.copy() return min(moves) def find_i(n) -> int: """ return the index of a in the global variable mo >>> find_i(9) 1 """ return mo.index(n) def find(n): """ return the index of M(n) in the global variable mo + 1 since the index has a range from 1 to n(exclusive) >>>find(9) 3 """ return find_i(M(n)) + 1 if n > 1: move_four_stools(model, n - find(n), source, intermediate2, destination, intermediate1) move_three_stools(find(n), source, intermediate2, destination) move_four_stools(model, n - find(n), intermediate1, source, intermediate2, destination) else: model.move(source, destination) move_four_stools(model, NUM_CHEESES, 0, 1, 2, 3) model2 = TOAHModel(4) model2.fill_first_stool(NUM_CHEESES) print(model2) for moves in model.get_move_seq()._moves: if console_animate: time.sleep(delay_btw_moves) model2.move(moves[0], moves[1]) print(model2)