local initialBoard = {
{0, 1, 0},
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
}
local function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
local function getNextState(board)
local rows = #board
local cols = #board[1]
local function countLiveNeighbors(board, x, y)
local count = 0
for dx = -1, 1 do
for dy = -1, 1 do
if not (dx == 0 and dy == 0) then
local nx, ny = x + dx, y + dy
if nx >= 1 and nx <= rows and ny >= 1 and ny <= cols then
count = count + board[nx][ny]
end
end
end
end
return count
end
local nextBoard = deepcopy(board)
for x = 1, rows do
for y = 1, cols do
local liveNeighbors = countLiveNeighbors(board, x, y)
if board[x][y] == 1 and (liveNeighbors < 2 or liveNeighbors > 3) then
nextBoard[x][y] = 0
elseif board[x][y] == 0 and liveNeighbors == 3 then
nextBoard[x][y] = 1
end
end
end
return nextBoard
end
local function printBoard(board)
io.write("\27[2J\27[H") -- clear console
for _, row in ipairs(board) do
for _, cell in ipairs(row) do
io.write(cell == 1 and "1 " or "0 ")
end
io.write("\n")
end
end
local function runGame(initialBoard, generations)
local board = initialBoard
for i = 1, generations do
printBoard(board)
board = getNextState(board)
os.execute("sleep 0.5") -- Wait 0.5 seconds between generations
end
end
runGame(initialBoard, 10)