I have a flood fill function and it has a flaw with checking neighboring tiles

  1. What do you want to achieve? Each tile has 4 neighbors. +1,-1,+9,-9. I want it to give me all the neighbors of set tile (ie: 15 → 16,14,6,24

  2. What is the issue? The problem with this is when i get to a tile thats on the border (ie: 18) the neighbors are 17,19,9,27 but 19 is on the other side of the grid meaning its not a neighbor.
    Heres a picture to show what I’m talking about (this is on a 10x10 grid but same concept):

local gridSize = 9^2
local grid = {}
for i = 1,gridSize do
	table.insert(grid,i)
end

local function getNeighbors(tile,grid)
	local neighbors = {
		tile + 1;
		tile - 1;
		tile + 9;
		tile - 9;
	}
	
	for _,neighbor in ipairs(neighbors) do
		--check if its in the grid
		if neighbor >= 1 and neighbor <= gridSize then
			--detect if its a neighbor
			???
		end
	end
end

nvm i figured it out

local neighborRow = math.floor((neighbor - 1) / 9) + 1
local neighborCol = (neighbor - 1) % 9 + 1

if math.abs(neighborRow - row) <= 1 and math.abs(neighborCol - col) <= 1 then

then i jsut inserted it in a diff table and retuned that

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.