def move_tower(num_disks, towers, source, target, middle): ''' Function is move_tower parameters: num_disks (int), towers (dictionary), source, target and middle tower names (strings) returns: drawing of the steps taken to move the disks from source tower to target tower does: using recursion moves n - 1 disks from source tower to a middle (middle man) tower, then moves the nth disk from the source tower to the target tower, and lastly moves n - 1 disks from the middle tower to the target tower. ''' # base case that terminates the recursion and starts the visualization if num_disks == 1: hanoi_viz.move_disk(source, target, towers) else: # moves n - 1 disks to the middle tower using the target tower # as a helper move_tower(num_disks - 1, towers, source, middle, target) # moves the biggest block over to the target tower move_tower(1, towers, source, target, middle) # moves n - 1 disks to the target tower using the source tower # as a helper move_tower(num_disks - 1, towers, middle, target, source)
def move_tower(num_disks, orig, dest, temp, towers): if num_disks == 0: move_disk(orig, dest, towers) else: move_tower(num_disks - 1, orig, temp, dest, towers) move_disk(orig, dest, towers) move_tower(num_disks - 1, temp, dest, orig, towers)
def move_tower(source, target, middle, towers, num_disks): # if statement for if the number of disks is greater than 0, than to move from source to target # this will continue until disks on the source and target are empty and the middle tower is filled if num_disks >= 0: move_tower(source, middle, target, towers, num_disks - 1) # if greater than 0 to move from source to middle move_disk( source, target, towers ) # utilizes the move disk function to move the disks between towers move_tower(middle, target, source, towers, num_disks - 1) # moves disk from middle to target
def move_tower(disk_num, source, target, aux, TOWERS): '''function move towers parameters: number of disks, names of source, target, temp towers, along with list of Simpsons characters returns: nothing does: checks if the number of disks is greater than or equal to one and moves n - 1 disks from source tower Homer to temp home Bart. We call move_disk from hanoi_viz to move disks from source to target using dictionary key towers. The final move towers is to move the n - 1 disks from the temp tower of Bart to our target tower of Marge ''' if disk_num >= 1: move_tower(disk_num - 1, source, aux, target, TOWERS) hanoi_viz.move_disk(source, target, TOWERS) move_tower(disk_num - 1, aux, target, source, TOWERS)
def move_tower(num_disks, source, target, helper, towers): """ Parameters: The number of disks, integer. The source, target, and helper towers, all as strings. Dictionary of towers, which keeps track of which rings are on which tower Does: Recursively solves the hanoi puzzle: calls the move_disk function from hanoi_viz.py to move each disk, and calls itself to manipulate which tower is the source, helper, and target for each step. The rings must be moved one at a time, and each ring may only be on top of larger rings and below smaller ones. We only care about moving disks that exist, so the base case here is implicit - when there are no disks left, we don't need to move any disks! Returns: Nothing """ if num_disks > 0: move_tower(num_disks - 1, source, helper, target, towers) hanoi_viz.move_disk(source, target, towers) move_tower(num_disks - 1, helper, target, source, towers)