Post by Nils M. on Jun 19, 2016 11:44:07 GMT
Hey, it's been a while since I put anything here so I decided I would post something.
This is my recreation of the famous John Conway's Game of life. Its a very simple, but fun to play around with, simulation and if you dont know what Conway's Game of Life is I recommend you look it up because I think it is quite interesting.
This is my recreation of the famous John Conway's Game of life. Its a very simple, but fun to play around with, simulation and if you dont know what Conway's Game of Life is I recommend you look it up because I think it is quite interesting.
--[[
John Conway's Game Of Life
(1970)
]]--
GRIDSIZE = {21,21}
--size of grid {columns,rows}
--make bigger for more complez patterns and outcomes
draw.setscreen(1)
width,height = draw.getport()
draw.setantialias(false)
draw.settitle("Please rotate your device.")
repeat width,height = draw.getport()
until width < height
draw.settitle("Conway's Game Of Life")
function getScale()
local csx, csy
local c, r = GRIDSIZE[1],GRIDSIZE[2]
csx = math.floor(width/c)
csy = csx
return {csx,csy,c,r}
end
clock = {0,30,0} --current tick, max tick, elapsed ticks
playing = false
scale = getScale()
basex = (width/2)-((scale[1]*scale[3])/2)
basey = 100
--^ = {cellSizeX,cellSizeY,columns,rows}
life = {}
for i = 1,scale[3]*scale[4] do
life[i] = 1
end
life[math.ceil((scale[3]*scale[4])/2)] = 2
color = {1,1,0,1}
function drawGame()
--draw cells
for r = 1,scale[4] do
for c = 1,scale[3] do
if life[getState(c,r)] == 2 then
draw.fillrect(
basex+((c-1)*scale[1]),
basey+((r-1)*scale[2]),
basex+((c-1)*scale[1])+scale[1],
basey+((r-1)*scale[2])+scale[2],
color
)
end
end
end
--draw grid
for r = 1,scale[4] do
for c = 1,scale[3] do
draw.rect(
basex+((c-1)*scale[1]),
basey+((r-1)*scale[2]),
basex+((c-1)*scale[1])+scale[1],
basey+((r-1)*scale[2])+scale[2],
{0,0,0,1}
)
end
end
end
function drawUI()
draw.setfont("Helvetica-Bold",15)
if not playing then
draw.stringinrect("Click here to start simulation.\nClick a cell to change its state.",0,basey+(scale[2]*scale[4]+25),width,height,{0,0,0,1})
else
draw.stringinrect("TURN: "..math.floor(clock[3]/clock[2]),0,height-25,width,height,{1,0,0,1})
draw.stringinrect("Click here to EDIT the simulation.",0,basey+(scale[2]*scale[4]+25),width,height,{0,0,0,1})
end
end
function getState(c,r)
return ((r-1)*scale[3])+c
end
function touched(x,y)
if not playing then
if x > basex and x < basex+(scale[1]*scale[3]) then
if y > basey and y < basey+(scale[2]*scale[4]) then
local cell = getCell(x,y)
life[cell] = 3-life[cell]
end
end
if y > basey+(scale[2]*scale[4]) then
playing = true
end
else
if y > basey+(scale[2]*scale[4]) then
if playing then
playing = false
end
end
end
end
function null() end
draw.tracktouches(touched,null,null)
function getCell(x,y)
local c = math.ceil((x+basex)/scale[1])
local r = math.ceil((y-basey)/scale[2])
return ((r-1)*scale[3])+c
end
function logic()
local new = {}
for _,a in pairs (life) do
new[_] = a
end
for _,a in pairs (life) do
local n = getNeighbors(_)
local alive,dead = 0,0
for i = 1,#n do
if life[n[i]] == 2 then
alive = alive + 1
end
end
dead = #n-alive
if a == 2 then
if alive <= 1 then
new[_] = 1
elseif alive >= 4 then
new[_] = 1
end
elseif a == 1 then
if alive == 3 then
new[_] = 2
end
end
end
for _,a in pairs (new) do
life[_] = a
end
end
function getNeighbors(a)
local cells = {}
--top row
if (a <= scale[3]) then
if (a % scale[3] == 0) then --far right
table.insert(cells,a-1)
table.insert(cells,a+scale[3])
table.insert(cells,(a+scale[3])-1)
return cells
end
if (a == 1) then --far left
table.insert(cells,a+1)
table.insert(cells,a+scale[3])
table.insert(cells,(a+scale[3])+1)
return cells
end
table.insert(cells,a+1)
table.insert(cells,a-1)
table.insert(cells,a+scale[3])
table.insert(cells,a+scale[3]-1)
table.insert(cells,a+scale[3]+1)
return cells
end
--bottom row
if (a > (scale[3]*scale[4])-scale[3]) then
if (a == scale[3]*scale[4]) then --far right
table.insert(cells,a-1)
table.insert(cells,a-scale[3])
table.insert(cells,(a-scale[3])-1)
return cells
end
if (a == (scale[3]*scale[4])-scale[3]+1) then --far left
table.insert(cells,a+1)
table.insert(cells,a-scale[3])
table.insert(cells,(a-scale[3])+1)
return cells
end
table.insert(cells,a+1)
table.insert(cells,a-1)
table.insert(cells,a-scale[3])
table.insert(cells,a-scale[3]-1)
table.insert(cells,a-scale[3]+1)
return cells
end
--right column
if (a % scale[3] == 0) then
table.insert(cells,a-1)
table.insert(cells,a-scale[3])
table.insert(cells,a-scale[3]-1)
table.insert(cells,a+scale[3])
table.insert(cells,a+scale[3]-1)
return cells
end
--left column
if ((a+(scale[3]-1)) % scale[3] == 0) then
table.insert(cells,a+1)
table.insert(cells,a-scale[3])
table.insert(cells,a-scale[3]+1)
table.insert(cells,a+scale[3])
table.insert(cells,a+scale[3]+1)
return cells
end
table.insert(cells,a-1)
table.insert(cells,a+1)
table.insert(cells,a-scale[3])
table.insert(cells,a-scale[3]-1)
table.insert(cells,a-scale[3]+1)
table.insert(cells,a+scale[3])
table.insert(cells,a+scale[3]-1)
table.insert(cells,a+scale[3]+1)
return cells
end
while true do
draw.doevents()
if playing and not paused then
clock[1] = clock[1]+1
clock[3] = clock[3]+1
if clock[1]==clock[2] then
clock[1] = 0
logic()
end
end
draw.beginframe()
draw.clear()
draw.clearscreen()
drawGame()
drawUI()
draw.endframe()
end