# How to detect matches in match-3 game?

I am working on a match 3 game where the number of matched cells matters

(if you match 3 then they just get removed, if you match exactly 4 then something happens, and if you match more than 4 then something else happens)

Basically this image sums it up :

cells in the green frame should match and disappear, the ones in the red shouldn't. Could anyone please help me out, am kinda stacked on how to tackle this. Thank you

Edit Thank you all for your help! flood-fill was the right thing to do, i made a good progress and faced a different problem, please check the new thread

I'm guessing you set the cells using an array.

You could define variables as array. Evey time you place a figure, save it's coordinates on a variable and ask if any neighboring cell (on a X or Y axis) has a figure too, if it does, save those too and repeat until there are three or more matches.

You can use a loop that works as long as there are neighboring cells.

If there are more than three matches, then delete the cells on the already saved coordinates.

• so am gonna end up with multiple arrays ? in the image example am gonna have 1 array that has 3 cells, and another one that has 4 ? – alaslipknot Aug 1 '17 at 0:18

In your case, matching symbols dissappear with 3 or more, right? And the chained amount only changes one tile at a time (except when matches get erased)?

If you only got one symbol, you only need one matrix of int. Every entry of that matrix is zero. Whenever a field is changed and not erased, do the following:

1. add the numbers of every adjacent cell + 1 to the selected cell C (up, down, left and right)

2. Now do for every adjacent cell the following:

• for 0, do nothing
• for a 1, change that cell to the content of C
• for a 2, change that cell to the content of C and do the same for every adjacent cell (one cell adjacent should be a 1)
3. Check the content of C. If it's greater than two, check for a possible bonus and set every cell greater than 2 to 0 ( to delete the matching cells)

This works because of your two cell matching doesn't do anything and for more than one symbol, you need at least one more matrix for every symbol. Preferably symbol number + 1 matrix that keeps track, what symbol is in what cell (0 is empty cell, 1 is symbol a, 2 is symbol b and so on).

• thank you! can you please check the last edit on the original post – alaslipknot Aug 3 '17 at 16:19