Class: CellularGenerator
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(width = nil, height = nil, opts = Hash.new) ⇒ CellularGenerator
constructor
A new instance of CellularGenerator.
- #randomize(probablility) ⇒ Object
- #set(x, y, value) ⇒ Object
- #set_options(opts) ⇒ Object
Methods inherited from Map
Constructor Details
#initialize(width = nil, height = nil, opts = Hash.new) ⇒ CellularGenerator
Returns a new instance of CellularGenerator.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 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 } opts @dirs = directions @options[:topology] @map = fill 0 end |
Instance Method Details
#generate ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
19 20 21 22 23 24 25 |
# 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) ⇒ Object
31 32 33 |
# File 'lib/delve/generator/cellular.rb', line 31 def set(x, y, value) @map[x][y] = value end |
#set_options(opts) ⇒ Object
27 28 29 |
# File 'lib/delve/generator/cellular.rb', line 27 def (opts) opts.keys.each { |key| @options[key] = opts[key] } end |