I am trying to make a wrap around cellular automaton and i am stuck on trying to make it wraparound to count a cells neighboors.
totalN is the amount of neighboors with the have the value of one (alive neighboors)
WIDTH and HEIGHT is reffering to the table, in the example 9*9
and i am checking each cells 8 neighboors in a grid like this:
grid = {
{1,0,0}
{0,1,1}
{1,1,1}
}
for y, xs in ipairs(grid) do
for x, value in ipairs(xs) do
local totalN = 0
for j=-1,1,1 do
for k=-1, 1, 1 do
if x+k < 1 then
cx = x+k + WIDTH else cx = x+k
end
if x+k > WIDTH then
cx = 0 else cx = x+k
end
if y+j < 1 then
cy = y+j + HEIGHT else cy = y+j
end
if y+j > HEIGHT then
cy = 0 else cx = y+j
end
if grid[cy][cx] == 1 then
totalN = totalN + 1
end
end
end
Last edited by NoreoAlles on Sun Sep 18, 2022 12:58 pm, edited 1 time in total.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Thats what modulo is for.
If you say wrap every USE of an objects x position in x%100 it will always act as if its limited to 0-99.
Like pos1%100==pos2%100.
You dont need to do that on assign, so in reality stuff can wander of the screen to near inf but the position check and renderer themself simply wrap them.
BrotSagtMist wrote: ↑Sun Sep 18, 2022 8:51 am
Thats what modulo is for.
If you say wrap every USE of an objects x position in x%100 it will always act as if its limited to 0-99.
Like pos1%100==pos2%100.
You dont need to do that on assign, so in reality stuff can wander of the screen to near inf but the position check and renderer themself simply wrap them.
Im sorry, could you give me a example i dont get it.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
newGen = empty -- not equal, but same table, coping below makes nothing
for y, xs in ipairs(empty) do
for x, value in ipairs(xs) do
newGen[y][x] = empty[y][x]
end
end
newGen = empty -- not equal, but same table, coping below makes nothing
for y, xs in ipairs(empty) do
for x, value in ipairs(xs) do
newGen[y][x] = empty[y][x]
end
end
I know that the code is uneccesary, but it worked when i was only checking each cell if it wasnt on the "edge"
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle