Adding small game of life example in Lua.
authorMaximumGeeker <nooneyouknow@dismail.de>
Sun, 21 Feb 2021 03:42:53 +0000 (03:42 +0000)
committerMaximumGeeker <nooneyouknow@dismail.de>
Sun, 21 Feb 2021 03:42:53 +0000 (03:42 +0000)
game_of_life/Lua/MaximumGeeker/life.lua [new file with mode: 0644]

diff --git a/game_of_life/Lua/MaximumGeeker/life.lua b/game_of_life/Lua/MaximumGeeker/life.lua
new file mode 100644 (file)
index 0000000..a61c8c2
--- /dev/null
@@ -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
+