summaryrefslogtreecommitdiff
path: root/life.lua
diff options
context:
space:
mode:
Diffstat (limited to 'life.lua')
-rw-r--r--life.lua43
1 files changed, 33 insertions, 10 deletions
diff --git a/life.lua b/life.lua
index 6d5a2cb..3961830 100644
--- a/life.lua
+++ b/life.lua
@@ -18,6 +18,10 @@
gol = {}
gol.helpers = {}
+gol.setup = gol.setup or {}
+gol.setup.current = gol.setup.current or {}
+
+gol.setup.fade = gol.setup.fade or false
local C, Ct, P, R, S, match = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.match
@@ -100,29 +104,44 @@ function gol.parse_rule (raw_rule)
return { birth = b, stay = s }
end
-function gol.apply_rule (cell, cnt, rule)
- local live, dead = "1", "0"
- local new = dead
+function gol.apply_rule (cell, cnt, rule, fade)
+ --local live, dead = "1", "0"
+ --local new = dead
+ local new = 0
+ local live = P("1")
+ local dead = 1 - live
local stay = rule.stay
local birth = rule.birth
-- checking if cnt matches the numbers from the conditions list
- if cell == live then
+ if live:match(cell) then
for _, cond in ipairs(stay) do
if cnt == cond then
- new = live
+ new = "1"
break
end
end
else -- ==dead
for _, cond in ipairs(birth) do
if cnt == cond then
- new = live
+ new = "1"
break
end
end
end
+ if fade then
+ if not live:match(new) then
+ local add = tonumber (cell)
+ if add and add < 9 and add ~= 0 then
+ add = add + 1
+ else
+ add = 0
+ end
+ new = tostring(add)
+ end
+ end
+
return new
end
@@ -151,8 +170,12 @@ end
function gol.next_line (rows, rule)
local new = ""
- local dead = "0"
- local live = "1"
+ --local dead = "0"
+ --local live = "1"
+ local live = P("1")
+ local dead = 1 - live
+
+ local fade = gol.setup.current.fade or gol.setup.fade
local n = 1
local max = string.len(rows[2])
@@ -199,13 +222,13 @@ function gol.next_line (rows, rule)
-- counting live cells in the environment
local cnt = 0
for _, chr in pairs(env) do
- if chr == live then
+ if live:match(chr) then
cnt = cnt + 1
end
end
-- adding new cell according to ruleset
- new = new .. gol.apply_rule(current, cnt, rule)
+ new = new .. gol.apply_rule(current, cnt, rule, fade)
n = n + 1
until n > max