DEAD = 1 ALIVE = 2 function createObserverMap (cells, legend) Observer { subject = cells, attributes = cells.attribute, legends = {legend}, type = "map" } end ---- function createMap (life) createObserverMap(life.cells, life.legend) life.cells:notify(0) end ------------------ function createTimer (life) life.timer = Timer { Event{action = function() life.cells:synchronize() changes(life) -- life.cells:notify() end} } end function count_neighbors(cell) count = 0 forEachNeighbor(cell, function(cell, neighbor) if neighbor.past.color == ALIVE then count = count + 1 end end) return count end ------------------- function changes (life) forEachCell(life.cells, function(cell) neighbors = count_neighbors(cell) if neighbors < 2 then cell.color = DEAD elseif neighbors > 3 then cell.color = DEAD elseif neighbors == 3 and cell.past.color == DEAD then cell.color = ALIVE end end) end function init (life) forEachCell(life.cells, function(cell) cell.color = math.random(2) end) life.cells:createNeighborhood{ strategy = "moore", self = false } end ----------------- function run (life, time) life.timer:execute (time) end ----------- function CellularAutomatalife(life) init (life) -- create Plot createMap (life) -- timer createTimer (life) return life end --------- life = CellularAutomatalife { cells = CellularSpace{ xdim = 50, ydim = 50, attribute = {"color"} }, legend = Legend { grouping = "uniquevalue", colorBar = { {value = DEAD, color = "white"}, {value = ALIVE, color = "black"} } } } ----- run (life, 400)