Sha256: f8394a5d98c6ca80fe151de788b5c397c1dd2f3bd4ac88843cbe9e60bd7a09bf

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Hanoi
  module Jane
    module Formatters
      class Console
        def initialize towers
          @discs = towers.discs
          @stacks = towers.stacks.clone.map { |s| s.clone }
        end

        def to_s
          s = ''
          (Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each do |stack|
            s += stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join ' '
            s += "\n"
          end

          s
        end

        def Console.pad array, length
          until array.length == length
            array.push nil
          end

          array.reverse
        end

        def Console.make_disc width, space, char = 'o'
          unless width
            return ' ' * space
          end

          count = Console.scale width
          padding = (space - count) / 2

          '%s%s%s' % [
            ' ' * padding,
            char * count,
            ' ' * padding
          ]
        end

        def Console.scale size
          (size * 2) + 1
        end

        def Console.rotate stacks
          stacks.map { |s| s.clone }.transpose.reverse
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanoi-jane-0.2.2 lib/hanoi/jane/formatters/console.rb