Sha256: c7c3a66386d16699ecefb4b08771c6672bd2be5eef1a2395a1e728d1cb72c83c
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
module Hanoi module Jane class Animation include Enumerable attr_reader :stacks def initialize towers @towers = towers @height = towers.discs @stacks = Animation.pad towers.old_stacks, @height @disc = towers.disc @source = towers.source @destination = towers.sink @fancy = towers.fancy @lift = true @drop = false @done = false end def animate @drop = true unless @lift if @lift stack = @stacks[@source] @position = stack.index @disc stack[@position] = nil if @position + 1 >= @height @lift = false else stack[@position + 1] = @disc end @stacks[@source] = stack end if @drop stack = @stacks[@destination] @position = -1 unless @position stack[@position] = @disc stack[(stack.index @disc) + 1] = nil @position -= 1 @stacks[@destination] = stack.take @height if stack[@position] || @position == 0 @drop = false @done = true end end end def to_s (Formatters::Console.new @height, @stacks, @fancy).to_s end def matrix m = Formatters::Matrix.new @towers m.stacks = @stacks m end def each until @done animate yield self end end def Animation.pad stacks, height stacks.map { |stack| stack + Array.new(height - stack.length) } end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
hanoi-jane-0.2.5 | lib/hanoi/jane/animation.rb |