Sha256: b572bf6ef36e5322c08d5d34525f9d116ef775854151909666cae1df054e1dc6

Contents?: true

Size: 959 Bytes

Versions: 3

Compression:

Stored size: 959 Bytes

Contents

module Hanoi
  module Jane
    class Dropper < Array
      def initialize stack, item, full_drop = false
        stack.map { |i| self.push i }
        @item = item
        @full_drop = full_drop
      end

      def drop
        pos = Dropper.position self, @item
        self[pos] = @item

        unless pos >= self.length - 1
          self[pos + 1] = nil
        end
      end

      def dropped
        if @full_drop
          return self[(Dropper.position self, @item)] || (Dropper.position self, @item) < 0
        end
        
        (
          self[(Dropper.position self, @item) - 1] ||
          self[(Dropper.position self, @item)]
        ) || (Dropper.position self, @item) == 0
      end

      def each
        until dropped
          drop
          yield self
        end
      end

      def Dropper.position stack, item
        pos = stack.index item
        return stack.length - 1 unless pos
        pos - 1
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanoi-jane-0.4.0 lib/hanoi/jane/animation/dropper.rb
hanoi-jane-0.3.4 lib/hanoi/jane/animation/dropper.rb
hanoi-jane-0.3.3 lib/hanoi/jane/animation/dropper.rb