示例#1
0
    /// <summary>
    /// Removes extra walls in the maze tiles
    /// </summary>
    private void RemoveExtraWalls()
    {
        // check board pattern
        for (int x = 0; x < mazeSize; x++)
        {
            for (int z = 0; z < mazeSize; z++)
            {
                MazeTile tile = currentMaze.tilesArray[x, z];

                // perform for all walls
                for (int dir = 0; dir < 4; dir++)
                {
                    Coordinate coordDir = Coordinate.GetCoordinate(dir);
                    Coordinate coord    = tile.coord + coordDir;

                    // check if coordinate points out of bounds
                    if (OutOfBounds(coord))
                    {
                        continue;
                    }

                    // no wall to break;
                    if (tile.walls[dir] == null)
                    {
                        continue;
                    }

                    MazeTile neighbour = currentMaze.tilesArray[coord.x, coord.z];

                    // check other wall
                    if (neighbour.walls[(dir + 2) % 4] == null)
                    {
                        continue;
                    }

                    // remove one wall because there are two
                    tile.BreakWall(dir);
                }
            }
        }
    }
示例#2
0
    /// <summary>
    /// Generates the maze path using DFS algorithm.
    /// </summary>
    private void GeneratePath()
    {
        // Variables for DFS
        Stack <MazeTile> tileStack = new Stack <MazeTile>();

        bool[,] visited = new bool[mazeSize, mazeSize];

        // Initialize DFS
        tileStack.Push(currentMaze.tilesArray[random.Next(0, mazeSize), random.Next(0, mazeSize)]);

        // Continue exploring paths until there are none remaining
        while (tileStack.Count > 0)
        {
            // Visit the top element of the stack
            MazeTile currentTile = tileStack.Peek();
            visited[currentTile.coord.x, currentTile.coord.z] = true;

            // Direction variables
            int        direction      = random.Next(0, 4);
            Coordinate c              = null;
            bool       foundDirection = false;

            // Attempt to find a valid direction to traverse
            for (int tries = 0; tries < 4; tries++)
            {
                c = currentTile.coord + Coordinate.GetCoordinate(direction);

                // check bounds
                if (OutOfBounds(c))
                {
                    direction = (direction + 1) % 4;
                    continue;
                }

                if (visited[c.x, c.z] == true)
                {
                    direction = (direction + 1) % 4;
                    continue;
                }

                // Found a direction
                foundDirection = true;
                break;
            }

            // check if found a direction
            if (foundDirection)
            {
                currentTile.BreakWall(direction);
                MazeTile t = currentMaze.tilesArray[c.x, c.z];

                // Break the wall from the opposite side too
                t.BreakWall((direction + 2) % 4);

                tileStack.Push(t);
            }
            else
            {
                // Go back
                tileStack.Pop();
            }
        }
    }