Sha256: fbd8692a249cdfd9add0c64a3bf2f3b58a3845e2a9612d37f922648395b9576b

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Hanoi
  module Jane
    class Matrix < Array
      def initialize towers
        @stacks = towers.stacks
        @digits = towers.rebased

        7.times do
          self.push [0] * 45
        end

        offset = 0
        @stacks.each do |stack|
          count = 0
          stack.each do |disc|
            shim = ((5 - (disc + 1)) / 2).round
            (disc + 1).times do |i|
              self[6 - count][i + offset + shim] = 1
            end
            count += 1
          end
          offset += 8
        end

        @bit_offset = 24
        @bit_side = :right
        @digits.chars.each do |bit|
          digit bit

          if @bit_side == :right
            @bit_side = :left
            @bit_offset += 8
          else
            @bit_side = :right
          end
        end
      end

      def digit value
        digits = {
          0 => [
            [1, 1, 1],
            [1, 0, 1],
            [1, 1, 1]
          ],

          1 => [
            [0, 1, 0],
            [0, 1, 0],
            [0, 1, 0]
          ],

          2 => [
            [1, 1, 0],
            [0, 1, 0],
            [0, 1, 1]
          ]
        }

        @column = @bit_offset
        @row = 0
        if @bit_side == :right
          @column += 2
          @row = 4
        end

        insert digits[value.to_i]

      end

      def insert grid
        3.times do |i|
          3.times do |j|
            self[@row + i][@column + j] = grid[i][j]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanoi-jane-0.1.0 lib/hanoi/jane/matrix.rb