Assuming that the grid is a 2D array and you have the coordinates of tile n
, you can find its immediate neighbors (north, east, south, west) in the following way:
index.tsx691 chars37 lines
This function takes the grid as a 2D array, and the x and y coordinates of the tile you want to find the neighbors of. It returns an array containing the immediate neighbors of that tile. The function checks each of the four possible directions and adds the neighbor to the array if it exists. If it doesn't exist (i.e., at the edge of the grid), that direction is skipped.
gistlibby LogSnag