totalN is the amount of neighboors with the have the value of one (alive neighboors)
WIDTH and HEIGHT is reffering to the table, in the example 9*9
and i am checking each cells 8 neighboors in a grid like this:
Code: Select all
grid = {
{1,0,0}
{0,1,1}
{1,1,1}
}
for y, xs in ipairs(grid) do
for x, value in ipairs(xs) do
local totalN = 0
for j=-1,1,1 do
for k=-1, 1, 1 do
if x+k < 1 then
cx = x+k + WIDTH else cx = x+k
end
if x+k > WIDTH then
cx = 0 else cx = x+k
end
if y+j < 1 then
cy = y+j + HEIGHT else cy = y+j
end
if y+j > HEIGHT then
cy = 0 else cx = y+j
end
if grid[cy][cx] == 1 then
totalN = totalN + 1
end
end
end