Conway's Game of Life


Perhaps my favorite program.

curr, new = {}, {}
for x=1,Safe_width do
  table.insert(curr, {})
  table.insert(new, {})
  for y=1,Safe_height do
    table.insert(curr[x], 0)
    table.insert(new[x], 0)
  end end
-- R pentomino
cx = math.floor(Safe_width/2)
cy = math.floor(Safe_height/2)
curr[cx][cy] = 1
curr[cx][cy+1] = 1
curr[cx][cy+2] = 1
curr[cx-1][cy] = 1
curr[cx+1][cy+1] = 1
function car.draw()
  color(0,0,0)
  for x=1,Safe_width do
    for y=1,Safe_height do
      if curr[x][y] > 0 then
        pt(x,y)
      end end end end
function car.update(dt)
  step()
end
function step()
  for x=2,Safe_width-1 do
    for y=2,Safe_height-1 do
      local n = nghs(x,y)
      if n < 2 or n > 3 then
        new[x][y] = 0
      elseif n == 2 then
        new[x][y] = curr[x][y]
      elseif n == 3 then
        new[x][y] = 1
      end end end
  curr, new = new, curr
end
function nghs(x,y)
  return curr[x-1][y-1] +curr[x-1][y] +curr[x-1][y+1] +
      curr[x][y-1] +curr[x][y+1] +
      curr[x+1][y-1] +curr[x+1][y] +curr[x+1][y+1]
end

If you try pasting this program into Lua Carousel, remember to first run the abbreviations on one of the example screens. Or if you've deleted that screen, here are the abbreviations this program uses:

g = love.graphics
pt = g.points
color = g.setColor

Get Lua Carousel

Leave a comment

Log in with itch.io to leave a comment.