Sha256: c61566d27c8517d0e2e0a98dd4f1666753883a508570dd29a62c197258377ab2

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module Hanoi
  module Jane
    module Formatters
      class Console
        CHARS = {
          space: ' ',
          disc: 'o',
          pole: '|',
          vert_divider: '|',
          horiz_divider: '-'
        }

        FANCY_CHARS = {
          space: ' ',
          disc: '🎾',
          pole: '💈',
          vert_divider: '🔺',
          horiz_divider: '🔻'
        }

        def initialize towers
          @discs = towers.discs
          @stacks = towers.stacks.clone.map { |s| s.clone }
          @@chars = towers.fancy ? FANCY_CHARS : CHARS
        end

        def to_s
          s = ''
          joiner = @@chars[:space]

          (Console.rotate @stacks.map { |s| (Console.pad s, @discs).reverse }).each_with_index do |stack, i|
            joiner = @@chars[:vert_divider] if i == @discs

            s += "%s%s%s\n" % [
              joiner,
              stack.map { |s| Console.make_disc s, (Console.scale @discs) }.join(joiner),
              joiner
            ]

          end

          s += "%s\n" % [@@chars[:horiz_divider] * (4 + (Console.scale @discs) * 3)]
        end

        def Console.pad array, length
          Array.new(length + 1 - array.length) + array.reverse
        end

        def Console.make_disc width, space
          char = @@chars[:disc]
          unless width
            width = 0
            char = @@chars[:pole]
          end

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

          '%s%s%s' % [
            @@chars[:space] * padding,
            char * count,
            @@chars[:space] * padding
          ]
        end

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

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

        def Console.fancify number
          fancy_digits = '012'
          number.chars.map { |d| fancy_digits[d] }.join ' '
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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