lib/hanoi/jane/towers.rb in hanoi-jane-0.2.3 vs lib/hanoi/jane/towers.rb in hanoi-jane-0.2.5
- old
+ new
@@ -1,25 +1,33 @@
module Hanoi
module Jane
class Towers
include Enumerable
- attr_reader :total, :stacks, :discs
- attr_accessor :fancy
+ attr_reader :total, :stacks, :discs, :old_stacks, :disc, :source, :sink
+ attr_accessor :fancy, :animated, :animation
def initialize discs
@discs = discs
@total = 0
@base = 2
@stacks = [(0...discs).to_a.reverse, [], []]
@fancy = false
+ @animated = false
end
def move
+ @old_stacks = @stacks.clone.map { |s| s.clone }
+
diff
- find_disc
- @stacks[find_stack].push @stacks[@source].pop
+ @source = find_disc
+ @sink = find_stack
+ @stacks[@sink].push @stacks[@source].pop
+
+ if @animated
+ @animation = Animation.new self
+ end
end
def solved
rebased.chars.all? { |digit| digit.to_i == @base - 1 }
end
@@ -27,19 +35,23 @@
def matrix
Formatters::Matrix.new self
end
def console
- (Formatters::Console.new self).to_s
+ (Formatters::Console.new @discs, @stacks, @fancy).to_s
end
def inspect
{
stacks: @stacks,
moves: @total,
binary: rebased,
- moved: @disc
+ moved: {
+ disc: @disc,
+ from: @source,
+ to: @sink
+ }
}
end
def each
yield self if @total == 0
@@ -70,14 +82,14 @@
private
def find_disc
@stacks.each_with_index do |stack, index|
- @source = index if stack.index @disc
+ return index if stack.index @disc
end
end
- def find_stack #disc, source, stacks
+ def find_stack
# if the next stack is empty, move there
if @stacks[(@source + 1) % 3] == []
return (@source + 1) % 3
end