Last active
December 30, 2015 20:29
-
-
Save davemackintosh/7881079 to your computer and use it in GitHub Desktop.
Conway's Game of Life in Lua for the Codea iOS app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gol = class() | |
function Gol:init(W, H) | |
-- Set the size | |
self.columns = W | |
self.rows = H | |
-- The varying states of cells | |
self._alive = 1 | |
self._dead = 0 | |
-- The game states | |
self._paused = 0 | |
self._stepping = 1 | |
-- Some public things | |
self.matrix = {} | |
-- This is a method to set for a draw method | |
-- Called for every cell every frame | |
self.drawMethod = function(cell) end | |
-- Create a blank board | |
self:create() | |
end | |
function Gol:create() | |
for r = 1, self.rows do | |
table.insert(self.matrix, {}) | |
for c = 1, self.columns do | |
table.insert(self.matrix[r], {}) | |
self.matrix[r][c] = self._dead | |
end | |
end | |
end | |
function Gol:clear() | |
for r = 1, self.rows do | |
for c = 1, self.columns do | |
self.matrix[r][c] = self._dead | |
end | |
end | |
end | |
function Gol:alive(c, r) | |
if self.matrix[r][c] == self._alive then | |
return true | |
end | |
return false | |
end | |
function Gol:dead(c, r) | |
if self.matrix[r][c] == self._dead then | |
return true | |
end | |
return false | |
end | |
function Gol:calculate() | |
local n = 0 | |
local tab = self.matrix | |
-- Neighbour positions from the current index | |
local nbrs = { | |
{-1,-1}, | |
{-1,0}, | |
{-1,1}, | |
{0,-1}, | |
{0,1}, | |
{1,-1}, | |
{1,0}, | |
{1,1} | |
} | |
for r = 2, self.rows - 1 do | |
for c = 2, self.columns - 1 do | |
n = 0 | |
-- How many neighbours does this cell have? | |
for _,a in ipairs(nbrs) do | |
if self:alive(c + a[2], r + a[1]) then | |
n = n + 1 | |
end | |
end | |
-- Kill or live | |
if self:alive(c, r) and n < 2 then | |
tab[r][c] = self._dead | |
elseif self:alive(c, r) and (n == 2 or n == 3) then | |
tab[r][c] = self._alive | |
elseif self:alive(c, r) and n >= 3 then | |
tab[r][c] = self._dead | |
elseif self:dead(c, r) and n == 3 then | |
tab[r][c] = self._alive | |
end | |
end | |
end | |
end | |
function Gol:draw() | |
for r = 1, self.rows do | |
for c = 1, self.columns do | |
self.drawMethod({ | |
x = c, | |
y = r, | |
s = self.matrix[r][c] | |
}) | |
end | |
end | |
end | |
function Gol:touched(touch) | |
local x = math.floor(touch.x / (WIDTH / self.columns)) | |
local y = math.floor(touch.y / (HEIGHT / self.rows)) | |
self.matrix[y][x] = self._alive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- golly | |
-- Use this function to perform your initial setup | |
function setup() | |
parameter.color("DeadCellColour", color(123,123,223,255)) | |
parameter.color("AliveCellColour", color(10,10,10,255)) | |
parameter.color("Background", color(255,255,255,255)) | |
parameter.boolean("Play") | |
gol = Gol(30, 30) | |
gol.drawMethod = function (cell) | |
wid = WIDTH / 30 | |
hei = HEIGHT / 30 | |
if cell.s == gol._alive then | |
fill(AliveCellColour) | |
else | |
fill(DeadCellColour) | |
end | |
rect(wid * (cell.x - 1), hei * cell.y, wid, hei) | |
end | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(Background) | |
-- If we've paused, stop calculating | |
if Play then | |
gol:calculate() | |
end | |
-- Always draw | |
gol:draw() | |
end | |
function touched(t) | |
gol:touched(t) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment