Created
October 11, 2013 15:19
-
-
Save TrevMcKendrick/6936543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tower_of_hanoi.rb | |
a = [1,2] | |
b = [] | |
c = [] | |
# Move the disks from one peg to another following the rules of Hanoi. | |
# | |
# number_of_disks - the total number of disks | |
# from - the starting peg | |
# to - the ending (goal) peg | |
# via - the remaining peg (b in this case) | |
# | |
def move_disk(number_of_disks,from,to,via) | |
if from == [] && via == [] | |
return to | |
else | |
if move_count.odd? == true | |
if from.length.odd? == true | |
to << 1 | |
else | |
via << 1 | |
end | |
end | |
to << number_of_disks if from == [number_of_disks] && to == [] | |
end | |
move_disk(number_of_disks,from,to,via) | |
to | |
end | |
puts move_disk(2, a, c, b).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment