The game of life

Here’s the copy of the game of life. Same rules. If you want to edit the rules use the first few lines of setup.
http://en.wikipedia.org/wiki/Conway’s_Game_of_Life
Press next to stop looping.


--# Main
displayMode(FULLSCREEN_NO_BUTTONS)

function setup()
    
    tdx, tdy = 0, 0
    Suffocation = 4
    birth = 3
    loneliness = 2
    LoopTime = .1
    NumberOfGrids = 40
    
    c = {NumberOfGrids}
    
    Background = image(WIDTH, HEIGHT)
    
    setContext(Background)
    pushStyle()
    
    stroke(127, 127, 127, 255)
    strokeWidth(1)
    
    for i = 1,c[1] + 1 do
        line((i - 1)*WIDTH/c[1], 0, (i - 1)*WIDTH/c[1], HEIGHT)
        line(0, (i - 1)*HEIGHT/c[1], WIDTH, (i - 1) * HEIGHT/c[1])
    end
    
    popStyle()
    setContext()
    
    Rects = image(WIDTH/c[1], HEIGHT/c[1])
    
    setContext(Rects)
    
    pushStyle()
    
    fill(255, 255, 255, 255)
    rectMode(CORNER)
    rect(0, 0, Rects.width, Rects.height)
    
    popStyle()
    setContext()
    c = {NumberOfGrids, Background, Rects}
    lives = {}
    Allowed = true
    
end

function draw()
    translate(51, 0)
    background(0, 0, 0, 255)
    fill(127, 127, 127, 255)
    strokeWidth(1)
    line(-50, HEIGHT - 50, 0, HEIGHT - 50)
    line(-50, HEIGHT/2 + 200, 0 , HEIGHT/2 + 200)
    line(-50, 100, 0, 100)
    text("close",-25,25)
    text("next",-25,HEIGHT/2)
    text("loop",-25,HEIGHT/2+250)
    text("clear",-25,HEIGHT-25)
    sprite(c[2], WIDTH/2, HEIGHT/2)
    
    for k,v in pairs(lives) do
        pushStyle()
        
        spriteMode(CORNER)
        sprite(c[3], (v.x - 1)*WIDTH/c[1], (v.y - 1)*HEIGHT/c[1])
        
        popStyle()
    end
    
end

function touched(touch)
    local PlaceTheLife = true
    local RemoveTheLife = {false, 0}
    local tx = touch.x - 50 - tdx
    local ty = touch.y - tdy
    
    if touch.state == BEGAN and touch.x < 50 and touch.y > 50 and touch.y < HEIGHT/2 + 200 then
        NextGen()
        cont = false
    elseif touch.state == BEGAN and touch.x < 50 and touch.y > HEIGHT/2 + 200 and touch.y < HEIGHT - 50 then
        cont = true
        NextGen()
    elseif touch.x < 50 and touch.y > HEIGHT - 50 then
        lives = {}
        cont = false
    elseif touch.x < 50 and touch.y < 100 then
        close()
    else
    
    if touch.state == BEGAN or touch.state == MOVING then
        
        for j = 1,c[1] + 1 do
        for i = 1,c[1] + 1 do
            
            if tx > (i - 1)*WIDTH/c[1] and tx < i*WIDTH/c[1] and 
               ty > (j - 1)*HEIGHT/c[1] and ty < j*HEIGHT/c[1] then
                XPlaced = i
                YPlaced = j
            end
        
        end
        end
        
    end
    
    if ChangedPosition(touch, XPlaced, YPlaced) then
        if touch.state == BEGAN or touch.state == MOVING then
                    
            for k,v in pairs(lives) do
                if v == vec2(XPlaced, YPlaced) then
                    PlaceTheLife = false
                    RemoveTheLife = {true, k}
                end
            end
                    
            if PlaceTheLife then
                table.insert(lives, vec2(XPlaced, YPlaced))
            end
                    
            if RemoveTheLife[1] then
                table.remove(lives, RemoveTheLife[2])
            end
            
        end
    
    end
    
    if touch.state == ENDED then
        touchPast = nil
    end
    end
end

function ChangedPosition(touch, a, b)
    Xconfirm, Yconfirm = false, false
    local tx = touch.x - 50
    local ty = touch.y
    
    if not touchPast then
        touchPast = vec2(math.min(tx, (a - 1)*WIDTH/c[1]), math.min(ty, (b - 1)*HEIGHT/c[1]))
        return true
    end
    
    if tx > touchPast.x + WIDTH/c[1] or tx < touchPast.x then
        Xconfirm = true
        touchPast = vec2(math.min(tx, (a - 1)*WIDTH/c[1]), math.min(ty, (b - 1)*HEIGHT/c[1]))
        return true
    end
    if ty > touchPast.y + HEIGHT/c[1] or ty < touchPast.y then
        Yconfirm = true
        touchPast = vec2(math.min(tx, (a - 1)*WIDTH/c[1]), math.min(ty, (b - 1)*HEIGHT/c[1]))
        return true
    end
    
    return false
    
end

function NextGen()
    
    local deaths = {}
    local births = {}
    
    local SX = c[1]
    local SY = c[1]
    local EX = 1
    local EY = 1
    
    for k,v in pairs(lives) do
        SX = math.min(v.x, SX)
        SY = math.min(v.y, SY)
        EX = math.max(v.x, EX)
        EY = math.max(v.y, EY)
    end
    
    
    for j = SY - 1, EY + 1 do
    for i = SX - 1, EX + 1 do
        
        local Nb = 0
        
        if Nb < Suffocation then
            if FindTable(lives, vec2(i + 1, j + 1)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i + 1, j)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i + 1, j - 1)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i, j + 1)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i, j - 1)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i - 1, j + 1)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i - 1, j)) then
                Nb = Nb + 1
            end
        end
        if Nb < Suffocation then
            if FindTable(lives, vec2(i - 1, j - 1)) then
                Nb = Nb + 1
            end
        end
        
        if Nb == Suffocation then
            table.insert(deaths, vec2(i, j))
        elseif Nb == birth then
            table.insert(births, vec2(i, j))
        elseif Nb < loneliness then
            table.insert(deaths, vec2(i, j))
        end
    end
    end
    
    for k,v in pairs(deaths) do
        for a,b in pairs(lives) do
            if v == b then
                table.remove(lives, a)
            end
        end
    end
    
    for k,v in pairs(births) do
        GiveBirth = true
        for a,b in pairs(lives) do
            if v == b then
                GiveBirth = false
            end
        end
        if GiveBirth then
            table.insert(lives, v)
        end
    end
    if cont then
        tween.delay(LoopTime, NextGen)
    end
end

function FindTable(table, value)
    for k,v in pairs(table) do
        if v == value then
            return true
        end
    end
    return false
end