Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a basic recursive backtracking alghorithm for a maze. It pretty much works but ocasionally leaves some tiles in the corners untouched. This is the recursive function:

void GenerateMaze(Coordinate tilePos)
    {
        Debug.Log("MazeGen");
        tileMap[tilePos.x, tilePos.y].visited = true;
        Shuffle<Coordinate>(directions);
        foreach(Coordinate d in directions)
        {
            if (tilePos.x + d.x >= 0 && 
            tilePos.x + d.x < mapWidth && 
            tilePos.y + d.y >= 0 &&
            tilePos.y + d.y < mapHeight)
            {
                if (!tileMap[tilePos.x + d.x, tilePos.y + d.y].visited)
                {
                    Carve(tilePos, d);
                    GenerateMaze(new Coordinate(tilePos.x + d.x, tilePos.y + d.y));
                }
            }
        }
    }

This is how the Carve method looks like:

private void Carve(Coordinate position, Coordinate direction)
    {
        if (direction.Equals(new Coordinate(-1, 0)))
        {
            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;
        }
    }

I'm betting on a typo or stupid mistake somewhere but I cannot find it. In my last result tileMap[4,4] stayed untouched without any walls. But the algorithm does backtrack as I often end up with dead ends. It does not happen often and up till now only on the sides.

Below is an example where two tiles on the bottom right have not been carved in too. enter image description here

Here is another one without walls, this time 3 unconnected tiles in another corner of the maze. enter image description here

Anyone can find whats wrong?

share|improve this question
    
Could you add screen shots (upload to imgur with the stack exchange tool) to show us what you have and what you expect? Reading a couple of chunks of code sometimes is harder when we don't see the intent. –  Alexandre Vaillancourt Mar 27 at 12:35
1  
omg put some parentheses to group expressions in that if statement in the first code block –  Babis Mar 27 at 14:29
    
@AlexandreVaillancourt I Cannot right now. But it creates a simple maze, except sometimes it does not pass all the tiles on the grid and those tiles will not be "carved" and thus unreachable. But since my method is recursive, passes next to these tiles and loops through all directions this should not happen. ---I basically get a full maze most of the time and sometimes a lone tile or two on the edge. –  Menno Gouw Mar 27 at 16:38
    
@Babis and why if I may ask? It is a single group that simply checks if the new location is out of bounds of the array, nothing special. –  Menno Gouw Mar 27 at 16:39
    
Just clarity, no special reason. Sometimes bugs are harder to identify with lack of clarity. What's exactly going on in Carve function? care to elaborate? –  Babis Mar 27 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.