0
\$\begingroup\$

I am currently working on a puzzle game where blocks drop and you match nearby colors Blocks are stored within a int 2D array that stores with a value between 1-3 once placed, and it should find connected blocks of the same value afterwards.

I believe this should be done by creating a function that when called, recursively finds same values next to the block, and add those within a list of 2D locations, then remove the values at those positions once all have been found.

How would this be done?

//this coroutine checks for all nearby blocks of same color//
public IEnumerator Flood(int x, int y, int OldCol, int NewCol) {   
    WaitForSecondsRealtime wait = new WaitForSecondsRealtime(0.016f); //small delay//
    if(x >= 0 && x < BloackBoardWidth && y >= 0 && y < BlockBoardHeight) {//is the check in range?//           
        yield return wait;
        if(GridCol[x,y] == OldCol) {//is the grid color same same as the old color?//
            //removes the color//
            GridCol[x,y] = 0;   
            StartCoroutine(Flood(x + 1, y, OldCol, NewCol));
            StartCoroutine(Flood(x - 1, y, OldCol, NewCol));
            StartCoroutine(Flood(x, y + 1, OldCol, NewCol));
            StartCoroutine(Flood(x, y - 1, OldCol, NewCol));                    
        }
    }         
}
\$\endgroup\$
3
  • \$\begingroup\$ It works roughly as you describe, have you tried to implement it? If you encounter an error, share your code and the wrong behaviour. \$\endgroup\$ Commented Mar 29, 2022 at 20:45
  • \$\begingroup\$ Currently have it working through a flood fill coroutine based method, it however ignores the minimum count needed Edit: Function was too long, here's a pastebin pastebin.com/bpDciFqP \$\endgroup\$ Commented Mar 29, 2022 at 21:00
  • 2
    \$\begingroup\$ This sounds like a standard flood fill or connected component search, which you can implement with a basic depth-first or breadth-first search algorithm. Where have you run into trouble applying a standard approach like this to your case? Be sure to edit your question to show code and describe the problems with it – don't relegate important info like that to the comments. \$\endgroup\$ Commented Mar 29, 2022 at 21:37

0

You must log in to answer this question.