Hanoi Solver - Recursion

Hello There!

I was wondering, if it would be a fun little exercise to include a Hanoi solver into the Hanoi project for the recursion topic.
I was inspired by a video on recursion by Computerphile: https://www.youtube.com/watch?v=8lhxIOAfDss
And thought maybe it would be a fun exercise to decide whether or not you want to play the game yourself or let it be solved for you.
My code of the solution of the project provided for the Hanoi-game would be as followed:

n_solve = []
if input("Would you like to get this solved for you?(y/n)") == "y":
  solver = True
if solver:
  solving(num_disks,stacks[0],stacks[1],stacks[2])
  printing()
  print("\n\nThe game was completed in {0} moves, and the optimal number of moves is {1}".format(len(n_solve),num_optimal_moves))
else:
  playing_the_game()

this would be the menu or something. playing_the_game would be the game itself as the project itself, while solving() the solver is. solving() is defined above as this:

def printing():
    print("\n\n\n...Current Stacks...")
    for i in stacks:
      i.print_items()

def move(f,t):
  print("Moving from {} Stack to {} Stack!".format(f.name,t.name))
  disk = f.pop()
  t.push(disk)
  n_solve.append(1)

def solving(number_of_disks,from_stack,help_stack,target_stack):
  #print(num)
  if number_of_disks == 0:
    return 
  solving(number_of_disks-1,from_stack,target_stack,help_stack)
  printing()
  move(from_stack,target_stack)
  solving(number_of_disks-1,help_stack,from_stack,target_stack)

just a fun little idea to extend the project exercise (https://www.codecademy.com/paths/computer-science/tracks/linear-data-structures/modules/cspath-stacks/projects/towers-of-hanoi) from the Computer Science course. :slight_smile:
Cheers
Aleks

2 Likes