Hey everyone Here is my latest project, Conway’s Game of Life!

For those who don’t know how it works, it is a simulation where each cell is either alive or dead. There are a few rules to it though…

- If a cell has 2 or less alive cells around it, it dies
- If a cell has 4 or more alive cells around it, it dies
- If a cell has 3 cells around it and is dead, it becomes alive

Here is my code:

```
-- Game Of Life
-- Use this function to perform your initial setup
function setup()
gSize = 32
displayMode(FULLSCREEN)
grid = { width = WIDTH / gSize, height = HEIGHT / gSize - 2 }
for x = 1, grid.width do
grid[x] = {}
for y = 1, grid.height do
grid[x][y] = 0
end
end
paused = 0
backingMode(RETAINED)
background(255, 255, 255, 255)
t = 0
c = 1
speed = .25
end
-- This function gets called once every frame
function draw()
local g = { width = WIDTH / gSize, height = HEIGHT / gSize - 2 }
t = t + DeltaTime
if t > c then
c = c + speed
background(255 - 128 * paused)
for x = 1, grid.width do
g[x] = {}
local r = grid[x]
for y = 1, grid.height do
local n = numSurrounding(x, y, grid)
local l = r[y]
g[x][y] = l
fill(0, 255 * l, 0)
--text(n, (x - .5) * gSize + 1, (y - .5) * gSize + 1)
rect((x - 1) * gSize + 1, (y - 1) * gSize + 1, gSize - 2, gSize - 2)
if paused == 0 then
if n < 2 then
g[x][y] = 0
elseif n == 2 then
g[x][y] = grid[x][y]
elseif n == 3 then
g[x][y] = 1
elseif n > 3 then
g[x][y] = 0
end
end
end
end
grid = g
end
end
function touched(touch)
if touch.state == ENDED then
if touch.y < HEIGHT - gSize * 2 then
grid[math.ceil(touch.x / gSize)][math.ceil(touch.y / gSize)] = 1 - grid[math.ceil(touch.x / gSize)][math.ceil(touch.y / 32)]
else
paused = 1-paused
end
end
end
function numSurrounding(x, y, g)
local n = 0
for i = x - 1, x + 1 do
for j = y - 1, y + 1 do
if not (i < 1 or j < 1 or i > grid.width or j > grid.height or (i == x and j == y)) then
n = n + g[i][j]
end
end
end
return n
end
```

And here’s a video of it in action:

http://www.youtube.com/watch?v=bK0_i1XwdzU

DEBUG MODE: To enable this, there is a commented line in draw, Uncomment it, and comment the rect call below it

http://www.youtube.com/watch?v=s8Tkhq5DGS0