lib/hanoi/jane/towers.rb in hanoi-jane-0.1.4 vs lib/hanoi/jane/towers.rb in hanoi-jane-0.2.0

- old
+ new

@@ -24,10 +24,23 @@ 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 + end + def inspect { stacks: @stacks, moves: @total, binary: rebased, @@ -96,9 +109,40 @@ 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