Created
July 3, 2015 08:59
-
-
Save anonymous/a5eccb570ea51ca5e081 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
#!/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