Wraparound a Table of Tables [Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Wraparound a Table of Tables [Solved]

Post by NoreoAlles »

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:

Code: Select all

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
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Wraparound a Table of Tables

Post by BrotSagtMist »

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.
obey
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Wraparound a Table of Tables

Post by NoreoAlles »

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
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Wraparound a Table of Tables

Post by darkfrei »

Just use two functions: getValue and setValue:
(w and h are width and height of the map)

Code: Select all

function getValue (map, x, y, w, h)
  x = (x-1)%w+1
  y = (y-1)%h+1
  return map[y][x]
end

Code: Select all

function setValue (map, x, y, w, h, value)
  x = (x-1)%w+1
  y = (y-1)%h+1
  map[y][x] = value
end
Where x in the map is in range of [1, w] and y in range [1,h]
Last edited by darkfrei on Sun Sep 18, 2022 9:16 am, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Wraparound a Table of Tables

Post by darkfrei »

Also

Code: Select all

dxys = {
  {x=0,y=-1},
  {x=1,y=-1},
  {x=1,y=0},
  {x=1,y=1},
  {x=0,y=1},
  {x=-1,y=1},
  {x=-1,y=0},
  {x=-1,y=-1},
}
Then

Code: Select all

x0 = 1
y0 = 1
for k, dxy in ipairs(dxys) do
  local x, y = x0+dxy.x, y0+dxy.y
  -- other code with neighbor x and y
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Wraparound a Table of Tables

Post by NoreoAlles »

darkfrei wrote: Sun Sep 18, 2022 9:11 am Also

Code: Select all

dxys = {
  {x=0,y=-1},
  {x=1,y=-1},
  {x=1,y=0},
  {x=1,y=1},
  {x=0,y=1},
  {x=-1,y=1},
  {x=-1,y=0},
  {x=-1,y=-1},
}
Then

Code: Select all

x0 = 1
y0 = 1
for k, dxy in ipairs(dxys) do
  local x, y = x0+dxy.x, y0+dxy.y
  -- other code with neighbor x and y
end
Im still doing soemthing wrong, but cant get what.
Ill just give you the source
Attachments
CA Playground Problem.love
(3.74 KiB) Downloaded 41 times
Last edited by NoreoAlles on Sun Sep 18, 2022 9:24 am, 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
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Wraparound a Table of Tables

Post by darkfrei »

Here

Code: Select all

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
Must be:

Code: Select all

newGen = {}
    for y, xs in ipairs(empty) do 
        newGen[y] = {}
        for x, value in ipairs(xs) do 
            newGen[y][x] = value -- except table values 
        end
    end
Last edited by darkfrei on Sun Sep 18, 2022 9:35 am, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Wraparound a Table of Tables

Post by NoreoAlles »

darkfrei wrote: Sun Sep 18, 2022 9:23 am Here

Code: Select all

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
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Wraparound a Table of Tables

Post by ReFreezed »

What isn't working? The logic seem to wrap around fine. I tried drawing this on the right edge:

Code: Select all

   |
 x |
 xx|
  x|
  x|
   |
Running the simulation shows it wrapping around to the left side. Same happens vertically.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Wraparound a Table of Tables

Post by NoreoAlles »

ReFreezed wrote: Sun Sep 18, 2022 12:38 pm What isn't working? The logic seem to wrap around fine. I tried drawing this on the right edge:

Code: Select all

   |
 x |
 xx|
  x|
  x|
   |
Running the simulation shows it wrapping around to the left side. Same happens vertically.
Whoops, you´re right, i just messed up when counting the cell as its own neighboor!
Thanks to everbody who helped me with this issue! :)
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests