class CellularGenerator
Public Class Methods
new(width=nil, height=nil, opts=Hash.new)
click to toggle source
Calls superclass method
Map::new
# File lib/delve/generator/cellular.rb, line 5 def initialize(width=nil, height=nil, opts=Hash.new) super width, height @options = { born: [5, 6, 7, 8], survive: [4, 5, 6, 7, 8], topology: :eight } set_options opts @dirs = directions @options[:topology] @map = fill 0 end
Public Instance Methods
generate() { |i, j, new_map[j]| ... }
click to toggle source
# File lib/delve/generator/cellular.rb, line 35 def generate new_map = fill 0 randomize 0.45 born = @options[:born] survive = @options[:survive] (0..@height-1).each do |j| width_step = 1 width_start = 0 if @options[:topology] == 6 width_step = 2 width_start = j%2 end width_start.step(@width-1, width_step) do |i| curr = @map[i][j] ncount = get_neighbours i, j if curr > 0 and survive.index(ncount) != nil new_map[i][j] = 1 elsif curr <= 0 and born.index(ncount) != nil new_map[i][j] = 1 end yield i, j, new_map[i][j] end end @map = new_map end
randomize(probablility)
click to toggle source
# File lib/delve/generator/cellular.rb, line 19 def randomize(probablility) (0..@width-1).each do |i| (0..@height-1).each do |j| @map[i][j] = rand < probablility ? 1 : 0 end end end
set(x, y, value)
click to toggle source
# File lib/delve/generator/cellular.rb, line 31 def set(x, y, value) @map[x][y] = value end
set_options(opts)
click to toggle source
# File lib/delve/generator/cellular.rb, line 27 def set_options(opts) opts.keys.each { |key| @options[key] = opts[key] } end
Private Instance Methods
directions(v)
click to toggle source
# File lib/delve/generator/cellular.rb, line 80 def directions(v) dirs = { :four => [ [ 0, -1], [ 1, 0], [ 0, 1], [-1, 0] ], :eight => [ [ 0, -1], [ 1, -1], [ 1, 0], [ 1, 1], [ 0, 1], [-1, 1], [-1, 0], [-1, -1] ], :six => [ [-1, -1], [ 1, -1], [ 2, 0], [ 1, 1], [-1, 1], [-2, 0] ] } dirs[v] end
get_neighbours(cx, cy)
click to toggle source
# File lib/delve/generator/cellular.rb, line 67 def get_neighbours cx, cy result = 0 @dirs.each do |dir| x = cx + dir[0] y = cy + dir[1] next if x < 0 or x >= (@width - 1) or y < 0 or y >= (@height - 1) result += @map[x][y] == 1 ? 1 :0 end result end