module NLife::Helper

miscellaneous functions for state/surround gereration

Public Instance Methods

golly_to_array(string) click to toggle source
# File lib/nlife/helper.rb, line 33
def golly_to_array(string)
  match = %r{^B([0-8]*)\/S([0-8]*)$}.match(string)
  raise ArgumentError 'wrong input format' unless match
  birth = (0..8).map { |i| match[1].include? i.to_s }
  survival = Array.new(9, false)
  match[2].split(//).map(&:to_i).each { |i| survival[i] = true }
  return birth, survival
end
rule_from_arrays(birth, survival) click to toggle source

constructs rules from two arrays: birth, survival densities

# File lib/nlife/helper.rb, line 21
def rule_from_arrays(birth, survival)
  unless birth.size == survival.size
    fail ArgumentError 'params sizes not match'
  end
  max_density = birth.size
  proc do |state, density|
    fail ArgumentError 'density too high' if density > max_density
    result = (state == 0 ? birth : survival)[density.round]
    result ? 1.0 : 0.0
  end
end
rule_from_golly(string) click to toggle source

constructs rules from string, describing birth/survival densities

# File lib/nlife/helper.rb, line 43
def rule_from_golly(string)
  rule_from_arrays(*golly_to_array(string))
end
surround_from_block(rows, cols) { |row + rows / 2 % rows - rows / 2, (col + cols / 2) % cols - cols / 2| ... } click to toggle source

surround is calculated for rectangle centered at (0, 0)

# File lib/nlife/helper.rb, line 9
def surround_from_block(rows, cols)
  result = NArray.float(cols, rows)
  result.shape[1].times do |row|
    result.shape[0].times do |col|
      result[col, row] = yield((row + rows / 2) % rows - rows / 2,
                               (col + cols / 2) % cols - cols / 2)
    end
  end
  result
end