Class: Lifelike::LifelikeCellularAutomaton::World

Inherits:
Object
  • Object
show all
Defined in:
lib/lifelike/lifelike_cellular_automaton/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cell_grid) ⇒ World

Returns a new instance of World.



5
6
7
# File 'lib/lifelike/lifelike_cellular_automaton/world.rb', line 5

def initialize(cell_grid)
  @cell_grid = cell_grid
end

Instance Attribute Details

#cell_gridObject (readonly)

Returns the value of attribute cell_grid.



4
5
6
# File 'lib/lifelike/lifelike_cellular_automaton/world.rb', line 4

def cell_grid
  @cell_grid
end

Instance Method Details

#tick(generations) ⇒ Object



9
10
11
12
13
# File 'lib/lifelike/lifelike_cellular_automaton/world.rb', line 9

def tick(generations)
  (1..generations).reduce(self) do |world, _|
    world.tick_once
  end
end

#tick_onceObject



15
16
17
18
19
20
21
# File 'lib/lifelike/lifelike_cellular_automaton/world.rb', line 15

def tick_once
  self.class.new(
    @cell_grid.map_with_neighbors do |cell, neighbors|
      cell.tick(neighbors)
    end
  )
end