Skip to content

Instantly share code, notes, and snippets.

Created Nov 7, 2011
Embed
What would you like to do?
Conway's Game of Life. Made in Codify
--- main
--[[ Original program
     Gilberto Ribeiro de Queiroz <gribeiro@dpi.inpe.br>
     Lab1: Lua Programming
     Ex3: Implement Conway’s Game of Life in LUA.
     http://www.dpi.inpe.br/~gribeiro/doutorado/cap342/lab1/
    I made some changes for iPad Codify.
    Alex alex812@gmail.com
--]]
live = 0
dead = 1
size = 30
dx = 22
dy = 22
xleft = 40
yleft = 40
generation = 0
-- Use this function to perform your initial setup
function setup()
    print("LIFE")
    iparameter("auto",0,1)
    game = SpatialGrid(size)
    x = {17,11,12,12,16,17,18}
    y = {14,15,15,16,16,16,16}
    for i = 1, #x do
        game.grid[y[i]][x[i]].past = live
    end
    watch("generation")
end
-- This function gets called once every frame
function draw()
    game:draw()
    if auto == 1 or CurrentTouch.state == BEGAN then
        game:play()
        game:synchronize()
        sound("shoot", 50)
        generation = generation + 1
    end
end
SpatialGrid = class()
function SpatialGrid:init(size)
    -- you can accept and set parameters here
    self.grid = {}
    self.size = size
    for i = 0, size + 1 do
        self.grid[i] = {}
        for j = 0, size + 1 do
            self.grid[i][j] = {past = dead, present = dead}          
        end
    end    
end
function SpatialGrid:synchronize()
    for i = 1, self.size do
        for j = 1, self.size do
            self.grid[i][j].past = self.grid[i][j].present
        end
        self.grid[i][self.size+1].past = live
        self.grid[i][0].past = live
    end
    for j = 1, self.size do
        self.grid[self.size+1][j].past = live
        self.grid[0][j].past = live
    end        
end
function SpatialGrid:play()
    for i = 1, self.size do
        for j = 1, self.size do
            n = self:liveNeighbours(i, j)
            if (self.grid[i][j].past == live) and (n < 2) then
                self.grid[i][j].present = dead
                
            elseif (self.grid[i][j].past == live) and (n > 3) then
                self.grid[i][j].present = dead
                
            elseif (self.grid[i][j].past == live) and ((n == 2) or (n == 3)) then
                self.grid[i][j].present = live 
                
            elseif (self.grid[i][j].past == dead) and (n == 3) then
                self.grid[i][j].present = live 
            else
                self.grid[i][j].present = self.grid[i][j].past 
            end
        end
    end
end
function SpatialGrid:liveNeighbours(i, j)
    count = 0
    if self.grid[i-1][j-1].past == live then count = count + 1 end
    if self.grid[i-1][j].past == live then count = count + 1 end
    if self.grid[i-1][j+1].past == live then count = count + 1 end
    if self.grid[i][j-1].past == live then count = count + 1 end
    if self.grid[i][j+1].past == live then count = count + 1 end
    if self.grid[i+1][j-1].past == live then count = count + 1 end
    if self.grid[i+1][j].past == live then count = count + 1 end
    if self.grid[i+1][j+1].past == live then count = count + 1 end
    return count
end
        
function SpatialGrid:draw()
    -- Codify does not automatically call this method
    local x = xleft
    local y = yleft
    background(0,0,0)
    for i = 0, self.size + 1 do        
        for j= 0, self.size + 1 do
            if self.grid[i][j].past == dead then 
                fill(0, 0, 255, 255
            else 
                fill(0, 255, 209, 255
            end
            rect(x, y, dx, dy)
            x = x + dx
        end
        x = xleft
        y = y + dy
    end
end
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment