I have a basic backtracking algorithm that generates mazes for me. But sometimes it does not "visit" all tiles/cells. I'm wondering what is wrong, the algorithm does backtrack properly and it should check all directions on each tile/cell but the "unvisited" tiles/cells do not get touched at all.
This is the backtracking algorithm:
void GenerateMaze(Coordinate tilePos)
{
//Mark the current position visited
tileMap[tilePos.x, tilePos.y].visited = true;
//Randomize directions
Shuffle<Coordinate>(directions);
foreach(Coordinate d in directions)
{
//Check if the new position is within bounds
if (tilePos.x + d.x >= 0 && tilePos.x + d.x < mapWidth && tilePos.y + d.y >= 0 && tilePos.y + d.y < mapHeight)
{
//Check if the tile is already visited
if (!tileMap[tilePos.x + d.x, tilePos.y + d.y].visited)
{
//Carve through walls from this tile to next
Carve(tilePos, d);
//Recursively call this method on the next tile
GenerateMaze(new Coordinate(tilePos.x + d.x, tilePos.y + d.y));
}
}
}
}
In case you are interested, this is the Carve
method:
private void Carve(Coordinate position, Coordinate direction)
{
if (direction.Equals(new Coordinate(-1, 0)))
{
Debug.Log("Carving West from: ");
tileMap[position.x, position.y].west = true;
tileMap[position.x + direction.x, position.y + direction.y].east = true;
}
else if (direction.Equals(new Coordinate(1, 0)))
{
tileMap[position.x, position.y].east = true;
tileMap[position.x + direction.x, position.y + direction.y].west = true;
}
else if (direction.Equals(new Coordinate(0, -1)))
{
tileMap[position.x, position.y].south = true;
tileMap[position.x + direction.x, position.y + direction.y].north = true;
}
else if (direction.Equals(new Coordinate(0, 1)))
{
tileMap[position.x, position.y].north = true;
tileMap[position.x + direction.x, position.y + direction.y].south = true;
}
}
It just sets the correct wall flags to true depending on the direction the algorithm is going.
In the image bellow you see the maze has 3 "unvisited" tiles. This mostly happens in corners.
Here it leaves a single tile untouched but this time not on the sides.
On a 10x10 maze this seems to happen about 1/10 times. The problem tiles stay unvisited so the algorithm does not process them at all. But since it travels past them and every direction of there neighbors is tested they really should be joining the maze. So what can be wrong?