Created
October 1, 2013 22:11
-
-
Save samueldowens/6786003 to your computer and use it in GitHub Desktop.
Tower of Hanoi: Brute Force Method
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 | |
#rules: | |
# arrays will be the pegs | |
# each array goes from top on left to bottom on right | |
# win/stop when c = [1,2,3,4] | |
#pieces cannot go to a space where a lower number is farther into the array. | |
def move_disc | |
a = [1,2,3,4] | |
b = [0,0,0,0] | |
c = [0,0,0,0] | |
turn = 0 | |
while c != [1,2,3,4] do | |
turn += 1 | |
if a == [1,2,3,4] && b == [0,0,0,0] && c == [0,0,0,0] | |
a = [0,2,3,4] | |
b = [0,0,0,1] | |
c = [0,0,0,0] | |
elsif a == [0,2,3,4] && b == [0,0,0,1] && c == [0,0,0,0] | |
a = [0,0,3,4] | |
b = [0,0,0,1] | |
c = [0,0,0,2] | |
elsif a == [0,0,3,4] && b == [0,0,0,1] && c == [0,0,0,2] | |
a = [0,0,3,4] | |
b = [0,0,0,0] | |
c = [0,0,1,2] | |
elsif a == [0,0,3,4] && b == [0,0,0,0] && c == [0,0,1,2] | |
a = [0,0,0,4] | |
b = [0,0,0,3] | |
c = [0,0,1,2] | |
elsif a == [0,0,0,4] && b == [0,0,0,3] && c == [0,0,1,2] | |
a = [0,0,1,4] | |
b = [0,0,0,3] | |
c = [0,0,0,2] | |
elsif a == [0,0,1,4] && b == [0,0,0,3] && c == [0,0,0,2] | |
a = [0,0,1,4] | |
b = [0,0,2,3] | |
c = [0,0,0,0] | |
elsif a == [0,0,1,4] && b == [0,0,2,3] && c == [0,0,0,0] | |
a = [0,0,0,4] | |
b = [0,1,2,3] | |
c = [0,0,0,0] | |
elsif a == [0,0,0,4] && b == [0,1,2,3] && c == [0,0,0,0] | |
a = [0,0,0,0] | |
b = [0,1,2,3] | |
c = [0,0,0,4] | |
elsif a == [0,0,0,0] && b == [0,1,2,3] && c == [0,0,0,4] | |
a = [0,0,0,0] | |
b = [0,0,2,3] | |
c = [0,0,1,4] | |
elsif a == [0,0,0,0] && b == [0,0,2,3] && c == [0,0,1,4] | |
a = [0,0,0,2] | |
b = [0,0,0,3] | |
c = [0,0,1,4] | |
elsif a == [0,0,0,2] && b == [0,0,0,3] && c == [0,0,1,4] | |
a = [0,0,1,2] | |
b = [0,0,0,3] | |
c = [0,0,0,4] | |
elsif a == [0,0,1,2] && b == [0,0,0,3] && c == [0,0,0,4] | |
a = [0,0,1,2] | |
b = [0,0,0,0] | |
c = [0,0,3,4] | |
elsif a == [0,0,1,2] && b == [0,0,0,0] && c == [0,0,3,4] | |
a = [0,0,0,2] | |
b = [0,0,0,1] | |
c = [0,0,3,4] | |
elsif a == [0,0,0,2] && b == [0,0,0,1] && c == [0,0,3,4] | |
a = [0,0,0,0] | |
b = [0,0,0,1] | |
c = [0,2,3,4] | |
elsif a == [0,0,0,0] && b == [0,0,0,1] && c == [0,2,3,4] | |
a = [0,0,0,0] | |
b = [0,0,0,0] | |
c = [1,2,3,4] | |
else | |
puts "Something broke." | |
end | |
puts "After #{turn} moves the pegs look like..." | |
p a | |
p b | |
p c | |
end | |
end | |
move_disc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment