From: MaximumGeeker Date: Sun, 21 Feb 2021 03:42:53 +0000 (+0000) Subject: Adding small game of life example in Lua. X-Git-Url: https://git.josue.xyz/?a=commitdiff_plain;h=7e79c19d7c19173d09e260aa42fe5aeb0f5ae9ef;p=langlearn%2F.git Adding small game of life example in Lua. --- diff --git a/game_of_life/Lua/MaximumGeeker/life.lua b/game_of_life/Lua/MaximumGeeker/life.lua new file mode 100644 index 0000000..a61c8c2 --- /dev/null +++ b/game_of_life/Lua/MaximumGeeker/life.lua @@ -0,0 +1,61 @@ + +math.randomseed( os.time() ) +math.random() +math.random() +math.random() + +local w, h = 12, 12 + +local xmt = { __index = function() return 0 end } +local x = setmetatable({}, xmt) +local mt = { __index = function() return x end } +local g1, g2 = setmetatable({}, mt), setmetatable({}, mt) + +for i = 1, h do + g1[i], g2[i]= setmetatable({}, xmt), setmetatable({}, xmt) + for j = 1, w do + g1[i][j], g2[i][j] = math.random() > 0.5 and 1 or 0, 0 + end +end + +for _ = 1, 100 do + + for i = 1, h do + for j = 1, w do + local b = g1[i][j] + + local c = + g1[i-1][j-1] + g1[i-1][j] + g1[i-1][j+1] + + g1[ i ][j-1] + 0 + g1[ i ][j+1] + + g1[i+1][j-1] + g1[i+1][j] + g1[i+1][j+1] + + if b == 1 then + if c >= 2 and c <= 3 then + g2[i][j] = 1 + else + g2[i][j] = 0 + end + else + if c == 3 then + g2[i][j] = 1 + else + g2[i][j] = 0 + end + end + + end + end + + g1, g2 = g2, g1 + + for i = 1, h do + for j = 1, w do + io.write(g1[i][j] == 1 and 'X' or ' ') + end + print() + end + print(string.rep('-', w+2)) + io.read() + +end +