Exemplo n.º 1
0
    // iterative implementation of randomised depth first algorithm
    private void DepthFirstIterativeImplementation()
    {
        Stack mazeStack = new Stack();

        // mark as visited, push to stack
        maze[0, 0].visited = true;
        mazeStack.Push(maze[0, 0]);

        while (mazeStack.Count > 0)
        {
            MazePart currentMazePart = (MazePart)mazeStack.Pop();

            List <MazePart> currentMazePartUnvistedNeighbours = currentMazePart.GetAllUnvistedNeighbours(maze);
            if (currentMazePartUnvistedNeighbours.Count > 0)
            {
                // push current cell to stack
                mazeStack.Push(currentMazePart);

                // choose unvisited neighbour
                MazePart chosenNeighbour = currentMazePartUnvistedNeighbours[random.Next(0, currentMazePartUnvistedNeighbours.Count)];

                // remove wall
                chosenNeighbour.RemoveWall(currentMazePart);

                // mark chosenNeighbour visited
                chosenNeighbour.visited = true;
                mazeStack.Push(chosenNeighbour);

                // keep track of pathLength to put fruit at the end of the longest path
                chosenNeighbour.pathLength = currentMazePart.pathLength + 1;
            }
            // the longest path in the maze gets a piece of fruit as the end goal for the player
            else
            {
                if (currentMazePart.pathLength > fruitMazePart.pathLength)
                {
                    fruitMazePart = currentMazePart;
                }
            }
        }
    }
Exemplo n.º 2
0
    // recursive implementation of randomised depth first algorithm
    private void DepthFirstRecursiveImplementation(MazePart currentMazePart, int pathLength)
    {
        // added a pathLength tracker to put fruit at the end of the longest path after the maze is finished
        currentMazePart.pathLength = pathLength;
        if (currentMazePart.pathLength > fruitMazePart.pathLength)
        {
            fruitMazePart = currentMazePart;
        }

        // mark as visited
        currentMazePart.visited = true;

        // read comment in the else for why "true" is put as a condition
        while (true)
        {
            // get a list of all unvisitedNeighbours of this MazePart
            List <MazePart> currentMazePartUnvistedNeighbours = currentMazePart.GetAllUnvistedNeighbours(maze);

            if (currentMazePartUnvistedNeighbours.Count > 0)
            {
                // choose random unvisted neighbour, then remove it from the list
                MazePart chosenNeighbour = currentMazePartUnvistedNeighbours[random.Next(0, currentMazePartUnvistedNeighbours.Count)];
                currentMazePartUnvistedNeighbours.Remove(chosenNeighbour);

                // remove wall between chosenNeighbour and currentMazePart
                currentMazePart.RemoveWall(chosenNeighbour);

                // call this function with chosenNeighbour
                DepthFirstRecursiveImplementation(chosenNeighbour, pathLength + 1);
            }
            // when there are no more unvisted neighbours for currentMazePart,
            // break out of the while loop.
            else
            {
                // I understand this isn't the nicest way to do it but it works well,
                // and currentMazePartUnvistedNeighbours.Count > 0 should not be put as a condition,
                // because there could be (will be) MazeParts in that list that have been changed (visited) by a deeper call of this function.
                break;
            }
        }
    }