I've been trying to write Conway's Game of Life https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life in lua/love for the last couple days, and I've felt like I was making progress...
...today I was working on the logic of the game, and I do not understand why it's not working as intended! Instead of the cells evolving like they should, they are always moving to the left (while evolving, somehow)! I've been trying to decide if I should scrap what I've done so far, and try again with a clear mind and a blank canvas, but I would really appreciate it if someone would help me out, and tell me what is causing this strange occurrence...
Here is the code I have so far:
Code: Select all
math.randomseed (math.sqrt (os.time () * os.time ()))
cells = {}
cells.map = {}
cells.x = 80
cells.y = 60
cells.w = love.graphics.getWidth () / cells.x
cells.h = love.graphics.getHeight () / cells.y
for i = 1, cells.x do
cells.map[i] = {}
for j = 1, cells.y do
if math.random (2) == 1 then
cells.map[i][j] = 0
else
cells.map[i][j] = 1
end
end
end
cells.newMap = {}
for i = 1, cells.x do
cells.newMap[i] = {}
for j = 1, cells.y do
cells.newMap[i][j] = {}
end
end
function cells.getNeighbors (_cells_map, _i, _j)
cells_map = _cells_map
x = _i
y = _j
if x == 1 or y == 1 then
ul = 0
else
ul = cells_map[x-1][y-1]
end
if y == 1 then
uu = 0
else
uu = cells_map[x][y-1]
end
if x == cells.x or y == 1 then
ur = 0
else
ur = cells_map[x+1][y-1]
end
if x == 1 then
ll = 0
else
ll = cells_map[x-1][y]
end
if x == cells.x then
rr = 0
else
rr = cells_map[x+1][y]
end
if x == 1 or y == cells.y then
dl = 0
else
dl = cells_map[x-1][y+1]
end
if y == cells.y then
dd = 0
else
dd = cells_map[x][y+1]
end
if x == cells.x or y == cells.y then
dr = 0
else
dr = cells_map[x+1][y+1]
end
living = (
ul + uu + ur
+ ll + 0 + rr
+ dr + dd + dr
)
return living
end
function cells.update ()
for i = 1, cells.x do
for j = 1, cells.y do
cells.getNeighbors (cells.map, i, j)
if cells.map[i][j] == 1 then
if living == 0 or living == 1 then
cells.newMap[i][j] = 0
elseif living == 2 or living == 3 then
cells.newMap[i][j] = 1
elseif living > 3 and living < 9 then
cells.newMap[i][j] = 0
else
--cells.newMap[i][j] = 1
end
end
if cells.map[i][j] == 0 then
if living == 3 then
cells.newMap[i][j] = 1
else
cells.newMap[i][j] = 0
end
end
if i == cells.x and j == cells.y then
cells.map = cells.newMap
end
end
end
end
function cells.draw ()
for i = 1, cells.x do
for j = 1, cells.y do
if cells.map[i][j] == 0 then
love.graphics.rectangle ('line', ((cells.w * i) - cells.w), ((cells.h * j) - cells.h), cells.w, cells.h)
else
love.graphics.rectangle ('fill', ((cells.w * i) - cells.w), ((cells.h * j) - cells.h), cells.w, cells.h)
end
end
end
end
return cells
Code: Select all
local cells = require "cells"
function love.load ()
end
function love.update (dt)
cells.update ()
end
function love.draw ()
cells.draw ()
--for i = 1, cells.x do
-- for j = 1, cells.y do
-- love.graphics.print (tostring (cells.map[i][j]), 0 + (10 * i) - 10, 0 + (10 * j) - 10)
-- end
--end
end
Thanks for taking the time to look at my work