Update: I'm trying the recursive version of the Flood-fill algorithm, but I'm having trouble. In order to check the west adjacent tile, I use x-1, for the east, x+1 etc. Problem is, as soon as x = 0, it will infinitely loop between 0 and 1. I also have to check that that tile exists, so I'm using ifs to figure it out in order to not get an IndexOutOfRangeException (I tried using try/catch, but it's pretty much the same result, I figured this would be simpler). Here's my code right now (yes, I'm reproducing mineSweeper):
bool destroyAdjacentTiles(int x, int z) {
int GridSize = Cubes.GetLength(0);
int minX = x == 0 ? x : x-1;
int maxX = x == GridSize - 1 ? x : x+1;
int minZ = z == 0 ? z : z-1;
int maxZ = z == GridSize - 1 ? z : z+1;
Debug.Log(string.Format("Cube: {0}, {1}; X {2}-{3}; Z {4}-{5}", x, z, minX, maxX, minZ, maxZ));
CubeBehavior thisCube = Cubes[x, z].GetComponent<CubeBehavior>();
if (thisCube.isMine) { Destroy(thisCube); Cubes[x,z] = null; return false; } // TO DO: Make this = game over
// BELOW: Always, no matter what, causes the object to destroy, dunno why. So I removed it for the time being.
//if (thisCube.surroundingMines >= 1) Destroy(thisCube); return true;
// if the function doesn't return by now, means it's a to-be-destroyed cube, so destroy it
//Destroy(thisCube.gameObject);
//Cubes[x,z] = null;
//
if (x > 0)
if (Cubes[x-1,z]) destroyAdjacentTiles(x-1, z);
if (z > 0)
if (Cubes[x,z-1] != null) destroyAdjacentTiles(x, z-1);
if (x < GridSize - 1)
if (Cubes[x+1,z] != null) destroyAdjacentTiles(x+1, z);
if (z < GridSize - 1)
if (Cubes[x,z+1] != null) destroyAdjacentTiles(x, z+1);
return true;
}