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.
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?
How do I go about doing this. Any help would be appreciated.
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);