diff options
Diffstat (limited to 'life.lua')
-rw-r--r-- | life.lua | 49 |
1 files changed, 22 insertions, 27 deletions
@@ -52,18 +52,11 @@ function gol.helpers.split_once (s, sep) end -- Modified stripper from Roberto to support trimming other chars than whitespace -function gol.helpers.strip(str, char) - local space = S(" \t\v\n") - - if char then - char = P(char) - else - char = space - end - - local nochar = 1 - char - - local stripper = char^0 * C((char^0 * nochar^1)^0) +function gol.helpers.strip(str, chars) + local chars = chars or " \t\v\n" + chars = S(chars) + local nochars = 1 - chars + local stripper = chars^0 * C((chars^0 * nochars^1)^0) return match(stripper,str) or "" end @@ -104,17 +97,16 @@ function gol.parse_rule (raw_rule) return { birth = b, stay = s } end -function gol.apply_rule (cell, cnt, rule, fade) +function gol.apply_rule (cell, cnt, rule, fade, keep) --local live, dead = "1", "0" --local new = dead local new = 0 - local live = P("1") - local dead = 1 - live + local live = "1" local stay = rule.stay local birth = rule.birth -- checking if cnt matches the numbers from the conditions list - if live:match(cell) then + if cell == live then for _, cond in ipairs(stay) do if cnt == cond then new = "1" @@ -131,12 +123,18 @@ function gol.apply_rule (cell, cnt, rule, fade) end if fade then - if not live:match(new) then + if not (live == new) then local add = tonumber (cell) - if add and add < 9 and add ~= 0 then - add = add + 1 - else - add = 0 + if not add then -- == "D" + add = "D" + else + if add and add < 9 and add ~= 0 then + add = add + 1 + elseif keep and add and add == 9 then -- marking dead cells once alive + add = "D" + else + add = 0 + end end new = tostring(add) end @@ -170,10 +168,7 @@ end function gol.next_line (rows, rule) local new = "" - --local dead = "0" - --local live = "1" - local live = P("1") - local dead = 1 - live + local live = "1" local fade = gol.setup.current.fade or gol.setup.fade @@ -222,13 +217,13 @@ function gol.next_line (rows, rule) -- counting live cells in the environment local cnt = 0 for _, chr in pairs(env) do - if live:match(chr) then + if chr == live then cnt = cnt + 1 end end -- adding new cell according to ruleset - new = new .. gol.apply_rule(current, cnt, rule, fade) + new = new .. gol.apply_rule(current, cnt, rule, fade, true) n = n + 1 until n > max |