Class: Hanoi::Jane::Lifter

Inherits:
Array
  • Object
show all
Defined in:
lib/hanoi/jane/animation/lifter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack) ⇒ Lifter

Returns a new instance of Lifter.



6
7
8
9
10
# File 'lib/hanoi/jane/animation/lifter.rb', line 6

def initialize stack
  stack.map { |i| self.push i }

  @lifted = false
end

Instance Attribute Details

#liftedObject (readonly)

Returns the value of attribute lifted.



4
5
6
# File 'lib/hanoi/jane/animation/lifter.rb', line 4

def lifted
  @lifted
end

Class Method Details

.position(stack) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hanoi/jane/animation/lifter.rb', line 33

def Lifter.position stack
  pos = nil
  stack.reverse.each_with_index do |item, index|
    if item
      pos = index
      break
    end
  end

  stack.length - 1 - pos
end

Instance Method Details

#eachObject



26
27
28
29
30
31
# File 'lib/hanoi/jane/animation/lifter.rb', line 26

def each
  until @lifted
    lift
    yield self
  end
end

#liftObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hanoi/jane/animation/lifter.rb', line 12

def lift
  start = Lifter.position self

  item = self[start]
  self[start] = nil

  next_pos = start + 1
  if next_pos >= self.length
    @lifted = true
  else
    self[next_pos] = item
  end
end