Finding 3 or more numbers next to each other

I am trying to make something similar to this but I have no idea how to detect if the numbers are near each other when someone clicked on it.

So if I clicked on that 1 showed in the image below, how would I check if there are 3 or more numbers near it, not diagonally.

image

(I have a table with all the positions and the values)

1 Like

A 2D array would probably help here to simplify the math (you can use a 1D array but the math will be a bit more involved to properly calculate indices), as well as recursion.
If you were to do that, you would have

local Grid = {
  {3, 2, 1, 2, 2},
  {2, 2, 3, 2, 2},
  {1, 3, 3, 2, 2},
  ...
}

Click on a number. For this example, the user clicked on Grid[0][2], which is the first row, third column (the first 1).

Then, simply run a recursive algorithm on the clicked tile and its adjacent neighbor in each cardinal direction that detects if any of the connected tiles are a 1. If they are, run the recursive algorithm on that tile. Repeat until the algorithm no longer runs. As you do this, store all the 1s in a table, and you have a table of all of the connected 1s.

2 Likes

Thank you so much!

I’ll try this.

1 Like

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