Search code examples
c#wpfflood-fill

Modifying flood fill algorithm for specific task


I am trying to make a tile based game where tiles are filled with random colors and if the user clicks on a tile it disappears. This I have already accomplished.

enter image description here

Now, what I want to do is the tile disappears only if 2 or more adjacent tiles have the same color. I have used the flood fill algorithm to destroy tiles. How do I modify this code so that it works only if some count value is greater than 2.

This is the code that destroys a tile :

 private void Destroy(int x,int y,int old_Value,int new_Value)
    {
        if (GameArr[x,y].BlockValue == old_Value)
        {
            //if some count > 2 then only
            GameArr[x, y].BlockValue = 0;

            Destroy(x + 1, y, old_Value, new_Value);
            Destroy(x - 1, y, old_Value, new_Value);
            Destroy(x, y + 1, old_Value, new_Value);
            Destroy(x, y - 1, old_Value, new_Value);

        }
    }

How do I get this count value?

  • If I pass a count variable in the method itself and check if value exceeds 2 and then destroy, it won't destroy the first two tiles.
  • If I make another method to check if tiles are destroyable, it would set the blockValues to 0 while counting.

How do I go about doing this. Any help would be appreciated.


Solution

  • Well I found my answer.

    The count value was to be checked before I call my Destroy method

    if (GameArr[i - 1, j].BlockValue == old_Value) count++;
           if (GameArr[i, j - 1].BlockValue == old_Value) count++;
           if (GameArr[i + 1, j].BlockValue == old_Value) count++;
           if (GameArr[i, j + 1].BlockValue == old_Value) count++;
    
           if(count>2)
                Destroy(i, j,GameArr[i,j].BlockValue,0);