diff --git a/lib/hanoi/jane/cli.rb b/lib/hanoi/jane/cli.rb index e045994..9932600 100644 --- a/lib/hanoi/jane/cli.rb +++ b/lib/hanoi/jane/cli.rb @@ -30,6 +30,39 @@ def phat sleep interval end end + + desc 'console', 'Solve the towers on the console' + option :discs, type: :numeric, default: 3 + option :constrained, type: :boolean, default: true + option :interval, type: :numeric, default: 0.5 + option :height, type: :numeric, default: 2 + + def console + at = AnimatedTowers.new do |a| + a.towers = options[:constrained] ? ConstrainedTowers : Towers + a.discs = options[:discs] + a.height = options[:discs] + options[:height] + end + + at.each do |frame| + system('clear') + + c = Formatters::Console.new do |c| + c.stacks = frame.stacks + end + + puts frame.value + puts c + + interval = options[:interval] + if frame.type == :tween + interval = interval * 0.1 + end + sleep interval + end + + puts '%d moves to solve for %d discs' % [at.towers.total, at.discs] + end end end end diff --git a/lib/hanoi/jane/formatters/console.rb b/lib/hanoi/jane/formatters/console.rb index 1023ecc..f12d891 100644 --- a/lib/hanoi/jane/formatters/console.rb +++ b/lib/hanoi/jane/formatters/console.rb @@ -11,7 +11,7 @@ def initialize end def to_s - require "pry" ; binding.pry + (Console.assemble stacks).map { |r| r.join '' }.join "\n" end def Console.assemble stacks