class Hanoi::Jane::Formatters::Matrix

Attributes

bit_offset[R]
digits[R]
stacks[RW]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 8
def initialize
  @digits = '0'
  @stacks = [[]]

  @bit_offset = 24
  @bit_side = :right
  @valid_digits = Config.instance.config.digits

  yield self if block_given?

  populate
end
shim(size) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 103
def Matrix.shim size
  ((5 - (size + 1)) / 2).round
end

Public Instance Methods

digit(value) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 84
def digit value
  @column = @bit_offset
  @row = 0
  if @bit_side == :right
    @column += 2
    @row = 4
  end

  insert value, @row, @column
end
digits=(digits) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 21
def digits= digits
  if digits.chars.reject { |d| @valid_digits.keys.include? d }.length > 0
    raise MatrixException.new '%s is not a valid value for digits' % digits
  end

  if digits.length > 5
    raise MatrixException.new '%s is longer than 5 chars' % digits
  end

  @digits = digits
end
draw_digits() click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 69
def draw_digits
  @digits.chars.each do |bit|
    digit bit
    @bit_side = realign @bit_side
  end
end
draw_disc(disc, height = 0, offset = 0) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 39
def draw_disc disc, height = 0, offset = 0
  if disc
    (disc + 1).times do |i|
      self[6 - height][i + offset + (Matrix.shim disc)] = 1
    end
  end
end
draw_stack(stack, offset = 0) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 47
def draw_stack stack, offset = 0
  height = 0
  stack.each do |disc|
    draw_disc disc, height, offset
    height += 1
  end
end
draw_stacks() click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 61
def draw_stacks
  offset = 0
  @stacks.each do |stack|
    draw_stack stack, offset
    offset += 8
  end
end
insert(value, row = 0, column = 0) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 95
def insert value, row = 0, column = 0
  3.times do |i|
    3.times do |j|
      self[row + i][column + j] = @valid_digits[value][i][j]
    end
  end
end
populate() click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 55
def populate
  wipe
  draw_stacks
  draw_digits
end
realign(side) click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 76
def realign side
  if side == :right
    @bit_offset += 8
    return :left
  end
  :right
end
wipe() click to toggle source
# File lib/hanoi/jane/formatters/matrix.rb, line 33
def wipe
  7.times do |i|
    self[i] = [0] * 45
  end
end