0

I'm trying to create an implementation of Conway's Game of Life in Lua.

The game field is a bidimensional circular table with 100 rows and 47 columns (4700 cells total), where a cell can have a value of 1 (alive) or 0 (dead).

The problem is when I try to create a blinker and make a pattern of 3 alive cells next to each other, both side cells die in the next generation and no other ones become alive. I also had tried to create a glider, but in the next generation it's top-middle cell and bottom-left one died and then the pattern became still.

When I attempted to debug, all variables seemed to have the right values, so I think that the problem is somewhere in value assignment in function nextGen() from table field to nfield and vice versa. What may I be doing wrong?

-- Game window's width and height. 
-- Rows 48, 49, and 50 are used for control tips and program information
local width = 100
local height = 50

-- Current generation field
local field = {}
-- Next generation field
local nfield = {}

-- UI Color codes
local black = 0x000000
local white = 0xFFFFFF

-- Field clearing/generation
local function clearField()
  -- Making the field array circular (code from https://stackoverflow.com/a/63169007/12696005)
  setmetatable(field, {
  __index = function(t,i)
    local index = i%width
    index = index == 0 and width or index
    return t[index] end
  })
  --
  for gx=1,width do
    field[gx] = {}
    nfield[gx] = {}
    -- Making the field array circular pt.2
    setmetatable(field[gx], {
    __index = function(t,i)
      local index = i%height-3
      index = index == 0 and height-3 or index
      return t[index] end
    })
    --
    for gy=1,height-3 do
      field[gx][gy] = 0
    end
  end
end

--Field redraw
local function drawField(data)
  for x=1, width do
    for y=1,height-3 do
      if data[x][y]==1 then
        -- Alive cell
        display.setBackgroundColor(white)
        display.setForegroundColor(black)
      else
        -- Dead cell
        display.setBackgroundColor(black)
        display.setForegroundColor(white)
      end
      -- Drawing the cell
      display.fillRectangle(x, y, 1, 1, " ")
    end
  end
end

--- Calculating next generation field
local function nextGen()
  -- Going through all the table
  for x=1,width do
    for y=1,height-3 do
      local livingCells = 0
      -- Calculation of living cells around x:y
      for i=x-1,x+1 do
        for j=y-1,y+1 do
          livingCells = livingCells + field[i][j]
        end
      end
      -- Substracting the x:y cell from the overall amount
      livingCells = livingCells - field[x][y]
      -- Cell spawns
      if field[x][y]==0 and livivngCells==3 then
        nfield[x][y] = 1
      -- Cell dies
      elseif field[x][y]==1 and (livingCells < 2 or livingCells > 3) then
        nfield[x][y] = 0
      -- Remains the same
      else
        nfield[x][y] = field[x][y]
      end
    end
  end
end

-- Game loop
clearField()
while true do
  local lastEvent = {events.listenFilter(filter)}
  -- Cell creation/deletion
  if lastEvent[1] == "mouseClicked" and lastEvent[5] == "lmb" then
    --State invertion and cell redrawing
    if field[lastEvent[3]][lastEvent[4]]==1 then
      field[lastEvent[3]][lastEvent[4]] = 0
      display.setBackgroundColor(black)
      display.setForegroundColor(white)
    else
      field[lastEvent[3]][lastEvent[4]] = 1
      display.setBackgroundColor(white)
      display.setForegroundColor(black)
    end
    display.fillRectangle(lastEvent[3], lastEvent[4], 1, 1, " ")
  elseif lastEvent[1] == "key_pressed" then
    -- Previous generation
    if lastEvent[4] == keyboard.lbracket then
      drawField(field)
    -- Next generation
    elseif lastEvent[4] == keyboard.rbracket then
      display.setBackgroundColor(blue)
      display.fillRectangle(1, height-2, width, 1, " ")
      nextGen()
      drawField(nfield)
      for i=1,width do
        for j=1,height-3 do
          field[i][j] = nfield[i][j]
        end
      end
    elseif lastEvent[4] == keyboard.backslash then
      -- more code --
    end
  end
end

Variables display, keyboard and events are libraries.

1 Answers1

0

Turns out the only problem was a misspeled livingCells in the line if field[x][y]==0 and livivngCells==3 then.