Sha256: f96dd389f1b288c20674a8f9ac06852917396226c50978f493ce7b679ea76829

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Hanoi
  module Jane
    class AnimatedTowers
      include Enumerable

      attr_accessor :towers, :discs, :height

      def initialize
        yield self if block_given?

        if @discs > @height
          raise HanoiException.new 'number_of_discs (%d) > height (%d)' % [@height, @discs]
        end

        @towers = @towers.new @discs
      end

      def each
        until @towers.solved
          stacks = PaddedStacks.new @towers.stacks, @height
          value = @towers.rebased
          @towers.move
          yield Frame.new stacks, value, :key

          @anim = Animation.new do |a|
            a.stacks = stacks
            a.disc = @towers.disc
            a.from = @towers.from
            a.to = @towers.to
            a.height = @height
          end

          @anim.each do |frame|
            yield Frame.new frame.stacks, @towers.rebased, :tween
          end
        end

        yield Frame.new (PaddedStacks.new @towers.stacks, @height), @towers.rebased, :key
      end
    end

    class Frame
      attr_reader :stacks, :type, :value

      def initialize stacks, value, type
        @stacks = stacks
        @value = value
        @type = type
      end
    end

    class HanoiException < Exception
      attr_reader :text

      def initialize text
        @text = text
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanoi-jane-0.3.0 lib/hanoi/jane/towers/animated_towers.rb