Example #1
0
    //Martin Foltin - Automated Maze Generation and Human Interaction - 2011 - p20-22 - Recursive Backtracker
    //Recursive Backtracker
    private void recursiveBacktracker()
    {
        Stack <int> stack = new Stack <int> ();

        Random.InitState(Seed);

        cell current = mCells [0];

        if (current != null)
        {
            current.Visited = true;
            stack.Push(current.Index);

            while (stack.Count > 0)
            {
                List <int>  adjacentIndices = current.NeighbourIndices();
                List <cell> adjacentCells   = CellsFromIndices(adjacentIndices);

                int count = adjacentCells.Count;
                if (count == 0)                   //no unvisted neighbours
                {
                    if (stack.Count > 0)
                    {
                        current = mCells[stack.Pop()];
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    int next = Random.Range(0, count - 1);
                    stack.Push(adjacentCells [next].Index);
                    current.RemoveWall(adjacentCells [next]);
                    current         = adjacentCells[next];
                    current.Visited = true;
                }
            }
        }
    }
Example #2
0
    //Martin Foltin - Automated Maze Generation and Human Interaction - 2011 - p15-17 - Randomized Prim's Algorithm#
    // Randomised Prim's Algorithm
    private void primsAlgorithm()
    {
        Random.InitState(Seed);

        //initialise current inCells
        int  currentIndex = 0;
        cell current      = mCells [currentIndex];

        current.Visited = true;

        List <int> inCells = new List <int> ();

        inCells.Add(currentIndex);
        //initialise current frontier
        List <int> frontierCells = current.NeighbourIndices();

        while (!entireGridVisited())
        {
            int  fIndex = -1;
            cell fCell  = null;

            int  iIndex = -1;
            cell iCell  = null;
            bool find   = true;

            do
            {
                //choose least weight (or Random) frontier cell (cF)
                fIndex = frontierCells[Random.Range(0, frontierCells.Count)];
                fCell  = mCells [fIndex];

                //choose adjacent incell (cI) to frontier cell
                List <int> fCellNeighbours = fCell.NeighbourIndices();
                List <int> adjInCells      = new List <int> ();
                foreach (int nI in fCellNeighbours)
                {
                    if (mCells [nI].Visited)
                    {
                        adjInCells.Add(nI);
                    }
                }
                if (adjInCells.Count > 0)
                {
                    iIndex = adjInCells [Random.Range(0, adjInCells.Count)];
                    iCell  = mCells [iIndex];
                    find   = false;
                }
            } while (find);

            //Add cF to inCells
            fCell.Visited = true;
            inCells.Add(fIndex);

            //mark all out-cells around cF as fontier
            //remove visited
            List <int> fNeighbours = fCell.NeighbourIndices();
            foreach (int fInd in fNeighbours)
            {
                if (!mCells[fInd].Visited && !frontierCells.Contains(fInd))
                {
                    frontierCells.Add(fInd);
                }
            }

            //carve path between cI and cF
            iCell.RemoveWall(fCell);


            //remove duplicates
            //frontierCells.Sort();
            //for (int ii = 1; ii <= frontierCells.Count - 1; ii++) {
            //	if (frontierCells [ii] == frontierCells [ii - 1]) {
            //		frontierCells.RemoveAt (ii);
            //	}
            //}

            //Remove cF from frontierCells
            for (int ind = 0; ind < frontierCells.Count; ind++)
            {
                if (frontierCells[ind] == fIndex)
                {
                    frontierCells.RemoveAt(ind);
                    break;
                }
            }
        }
    }