Yet Another Game of Life Simulation

This is my first program using Codea. After I finished it and registered on the forums (just now), I noticed another member already submitted Game of Life a month ago. Yet I want to submit this anyway as I coded this independent of the former submission and would like feedback on my code.

-- Conway's Game of Life
-- Coded on iPad with Codea
-- Author, Stephen Monroe

function setup()
    
    parameter("pause", 0, 1)
    parameter("delay", 0, 10)
    
    gridX = 24
    gridY = 24
    tick = 0
    count = 0
    
    cell = {}
    for i = 0, gridX + 1 do
        cell[i] = {}
        for j = 0, gridY + 1 do
            cell[i][j] = 0
        end
    end
    
    myFPS = FPS(4)
    
end

function draw()
    
    background(0)
    clearOutput()
    print("FPS: " .. myFPS:display())
    
    if pause < .5 then
        pause = math.floor(pause)
    else
        pause = math.ceil(pause)
    end
    
    delay = math.floor(delay)
    
    DrawGrid()
    if pause == 0 then
        TouchGrid()
    else
        Simulate(delay)
    end

end

function DrawGrid()
    
    fill(0, 0, 255, 255)
    strokeWidth(1)
    
    for i = 0, gridX do
        for j = 0, gridY do
            if cell[i + 1][j + 1] == 1 then
                rect(i * (WIDTH / gridX), j * (HEIGHT / gridY), (WIDTH / gridX), (HEIGHT / gridY))
            end
        end
    end
    
end

function TouchGrid()
    
    if CurrentTouch.state ~= BEGAN then
        return
    end
    
    if priorX == CurrentTouch.x and priorY == CurrentTouch.y then
        return
    end
    
    x = math.floor(CurrentTouch.x / (WIDTH / gridX))
    y = math.floor(CurrentTouch.y / (HEIGHT / gridY))
    
    priorX = CurrentTouch.x
    priorY = CurrentTouch.y
    
    if cell[x + 1][y + 1] == 0 then
        cell[x + 1][y + 1] = 1
    else
        cell[x + 1][y + 1] = 0
    end
    
end

function Simulate(delay)
    
    print("Tick: " .. tick)
    
    count = count + 1
    if count >= delay then
        count = 0
    else
        return
    end
    
    newCell = {}
    for i = 0, gridX + 1 do
        newCell[i] = {}
        for j = 0, gridY + 1 do
            newCell[i][j] = cell[i][j]
        end
    end
    
    for i = 1, gridX do
        for j = 1, gridY do
            
            proxy = cell[i][j - 1] + cell[i][j + 1] + cell[i - 1][j] + cell[i + 1][j] + cell[i - 1][j - 1] + cell[i + 1][j + 1] + cell[i - 1][j + 1] + cell[i + 1][j - 1]
            
            if cell[i][j] == 1 then
                if proxy < 2 or proxy > 3 then
                    newCell[i][j] = 0
                end
            else
                if proxy == 3 then
                    newCell[i][j] = 1
                end
            end
            
        end
    end
    
    for i = 0, gridX + 1 do
        for j = 0, gridY + 1 do
            cell[i][j] = newCell[i][j]
        end
    end
    
    tick = tick + 1
end

You seem to be missing an FPS class

I meant to remove that when I posted the code. That was so i could track the frame rate and see how large of a grid the ipad could support.

Very nice @metzyn. Just wondering if you could turn the pause off after a while so that the user wouldnt have to do it and just to give you a challenge see if you can get the colour of the squares to change after a number of seconds, add some rules e.g. if two different color squares touch they create a square mixed with those colours. its a great program, i just want to see you challenge yourself :slight_smile:

Thank you, @Dalorbi! I reset my iPad and now I don’t have this project any longer. I know I can just copy and paste but I’ve already moved on to recreating asteroids. In my objective to learn Lua and Codea, I’m starting off by mimicking existing projects with my own code. I’m learning quite a bit but there is such a learning curve as the documentation is very lacking. The support on the forums is great, just not enough posts to get all the info needed.