Sha256: 1d85ec06ad7855d8b9b5327d9b94e4c9b598f0a180199f85e7036500b0898f7d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Hanoi
  module Jane
    class Console
      attr_reader :content

      def initialize towers
        @stacks = towers.stacks
        @discs = towers.discs
        @content = ''
        x = @stacks.clone
        y = x.map { |s| s.clone }

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

      def to_s
        @content
      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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanoi-jane-0.2.1 lib/hanoi/jane/console.rb