Skip to content

Instantly share code, notes, and snippets.

@pat-whitrock
Created March 16, 2014 20:45
Show Gist options
  • Save pat-whitrock/9589672 to your computer and use it in GitHub Desktop.
Save pat-whitrock/9589672 to your computer and use it in GitHub Desktop.
class Peg
attr_reader :name
attr_accessor :values
def initialize(name)
@name = name
@values = []
end
def shift
self.values.shift
end
def unshift(value)
self.values.unshift(value)
end
end
a = Peg.new("a")
b = Peg.new("b")
c = Peg.new("c")
a.values = [1,2,3,4]
def moveTower(disc_num, source, destination, spare)
if disc_num == 1
temp = source.shift
puts "Moving #{temp} from #{source.name} to #{destination.name}"
destination.unshift(temp)
else
moveTower(disc_num-1, source, spare, destination)
temp = source.shift
puts "Moving #{temp} from #{source.name} to #{destination.name}"
destination.unshift(temp)
moveTower(disc_num-1, spare, destination, source)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment