lib/hanoi/jane/towers.rb in hanoi-jane-0.2.0 vs lib/hanoi/jane/towers.rb in hanoi-jane-0.2.1
- old
+ new
@@ -1,11 +1,11 @@
module Hanoi
module Jane
class Towers
include Enumerable
- attr_reader :total, :stacks
+ attr_reader :total, :stacks, :discs
def initialize discs
@discs = discs
@total = 0
@base = 2
@@ -25,20 +25,11 @@
def matrix
Matrix.new self
end
def console
- s = ''
- x = self.stacks.clone
- y = x.map { |s| s.clone }
-
- (Towers.rotate y.map { |s| (Towers.pad s, @discs).reverse }).each do |stack|
- s += stack.map { |s| Towers.make_disc s, (Towers.scale @discs) }.join ' '
- s += "\n"
- end
-
- s
+ (Console.new self).to_s
end
def inspect
{
stacks: @stacks,
@@ -109,40 +100,9 @@
end
end
def Towers.rebase value, base, width
'%0*d' % [width, value.to_s(base)]
- end
-
- def Towers.pad array, length
- until array.length == length
- array.push nil
- end
-
- array.reverse
- end
-
- def Towers.make_disc width, space, char = 'o'
- unless width
- return ' ' * space
- end
-
- count = Towers.scale width
- padding = (space - count) / 2
-
- '%s%s%s' % [
- ' ' * padding,
- char * count,
- ' ' * padding
- ]
- end
-
- def Towers.scale size
- (size * 2) + 1
- end
-
- def Towers.rotate stacks
- stacks.map { |s| s.clone }.transpose.reverse
end
end
end
end