Skip to content

Instantly share code, notes, and snippets.

Created July 3, 2015 08:59
Show Gist options
  • Save anonymous/a5eccb570ea51ca5e081 to your computer and use it in GitHub Desktop.
Save anonymous/a5eccb570ea51ca5e081 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# The individual pegs for the Towers of Hanoi game
# class Peg
# attr_reader :rings
# def initialize(rings)
# @rings = rings
# end
# def last
# @rings.last
# end
# def empty
# @rings.empty
# end
# def pop
# @rings.pop
# end
# def push(ring)
# @rings.push ring
# end
# end
# The actual game that puts the pegs together and interacts with them
class TowerGame
attr_reader :pegs
def initialize(rings = 3)
@win = [*(1..rings)].reverse
@left = @win_seq
@middle = []
@right = []
@pegs = { 'left' => @left, 'middle' => @middle, 'right' => @right }
end
# def move(from, to)
# if to.empty? || from.last < to.last
# puts "Moving #{from.last} from #{from} to #{to}"
# to.push from.pop
# else
# puts 'Invalid move'
# end
# end
def move
@left.push @right.pop
end
def show
puts "Left Peg: #{@left}\n"\
"Middle Peg: #{@middle}\n"\
"Right Peg: #{@right}\n"
end
def win?
@right == @win
end
end
game1 = TowerGame.new(7)
game1.show
# game1.move(game1.pegs['left'], game1.pegs['right'])
game1.move
game1.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment