Created
June 7, 2012 05:14
-
-
Save abyx/2886721 to your computer and use it in GitHub Desktop.
Game of Life in Codea | Video available here http://www.youtube.com/watch?v=G5yg64joL-8
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
Board = class() | |
CELL_SIZE = 30 | |
function Board:init(size) | |
self.grid = {} | |
for i = 1, size do | |
self.grid[i] = {} | |
for j = 1, size do | |
self.grid[i][j] = false | |
end | |
end | |
end | |
function Board:draw() | |
-- Codea does not automatically call this method | |
stroke(176, 176, 176, 255) | |
strokeWidth(1) | |
for i = 1, #self.grid do | |
for j = 1, #self.grid[i] do | |
if (self.grid[i][j]) then | |
fill(0, 0, 0, 255) | |
else | |
fill(255, 255, 255, 255) | |
end | |
rect(i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE) | |
end | |
end | |
end | |
function Board:setCellAt(x, y, isAlive) | |
self.grid[x][y] = isAlive | |
end | |
function Board:copyGrid() | |
self.copy = self.grid | |
self.grid = {} | |
for i = 1, #self.copy do | |
self.grid[i] = {} | |
for j = 1, #self.copy[i] do | |
self.grid[i][j] = self.copy[i][j] | |
end | |
end | |
end | |
function Board:countNeighboursAt(x, y) | |
-- deal with edges | |
minX = math.max(x - 1, 1) | |
maxX = math.min(x + 1, #self.copy) | |
minY = math.max(y - 1, 1) | |
maxY = math.min(y + 1, #self.copy[1]) | |
-- count live cells in the radius that are not this one | |
count = 0 | |
for i = minX, maxX do | |
for j = minY, maxY do | |
if not (x == i and y == j) and self.copy[i][j] then | |
count = count + 1 | |
end | |
end | |
end | |
return count | |
end | |
function Board:shouldBeAliveAt(x, y) | |
neighbours = self:countNeighboursAt(x, y) | |
return (self.copy[x][y] and (neighbours == 2 or neighbours == 3)) or | |
(not self.copy[x][y] and neighbours == 3) | |
end | |
function Board:evolve() | |
self:copyGrid() | |
for i = 1, #self.grid do | |
for j = 1, #self.grid[i] do | |
self.grid[i][j] = self:shouldBeAliveAt(i, j) | |
end | |
end | |
end | |
function Board:touched(touch) | |
-- Codea does not automatically call this method | |
if touch.state ~= ENDED then | |
x = math.floor(touch.x/CELL_SIZE) | |
y = math.floor(touch.y/CELL_SIZE) | |
if x >= 1 and x <= #self.grid and y >= 1 and y <= #self.grid[x] then | |
self:setCellAt(x, y, true) | |
end | |
end | |
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
BOARD_SIZE = 24 | |
-- Use this function to perform your initial setup | |
function setup() | |
frame=0 | |
board = Board(BOARD_SIZE) | |
-- cool initial state | |
board:setCellAt(2,1,true) | |
board:setCellAt(3,2,true) | |
board:setCellAt(1,3,true) | |
board:setCellAt(2,3,true) | |
board:setCellAt(3,3,true) | |
board:setCellAt(17,8,true) | |
board:setCellAt(18,8,true) | |
board:setCellAt(19,8,true) | |
end | |
-- This function gets called once every frame | |
function draw() | |
background(255, 255, 255, 255) | |
board:draw() | |
-- manually reduce frame rate... | |
if frame % 5 == 0 then | |
board:evolve() | |
end | |
frame = frame + 1 | |
end | |
function touched(touch) | |
board:touched(touch) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Video of the result is available here: http://www.youtube.com/watch?v=G5yg64joL-8