コード例 #1
0
    public static void GenerateLoops(MazeNode root, int loops, int size)
    {
        MazeNode         current;
        Stack <MazeNode> visited  = new Stack <MazeNode>();
        Stack <MazeNode> visited2 = new Stack <MazeNode>();

        int[,,] disconnectingWalls     = new int[size, size, 4];
        MazeNode[,,] disconnectedNodes = new MazeNode[size, size, 4];
        int counter;
        int counter2;
        int counter3;

        while (loops > 0)
        {
            int      largestDisconnection = 0;
            int      largestCol           = 0;
            int      largestRow           = 0;
            int      largestDir           = 0;
            int      i;
            int      j;
            int      k;
            MazeNode largestDisconnectedNode = root;

            visited  = new Stack <MazeNode>();
            visited2 = new Stack <MazeNode>();
            visited.Push(root);
            visited2.Push(root);

            for (counter = 0; counter < size; counter++)
            {
                for (counter2 = 0; counter2 < size; counter2++)
                {
                    for (counter3 = 0; counter3 < 4; counter3++)
                    {
                        disconnectingWalls[counter, counter2, counter3] = 0;
                    }
                }
            }

            counter = 0;

            while (visited.Count > 0 && counter < 1000)
            {
                current = visited.Pop();

                for (counter = 0; counter < 4; counter++)
                {
                    int disconnected;
                    if (counter == 0 && current.Left == null || counter == 1 && current.Forward == null || counter == 2 && current.Right == null || counter == 3 && current.Backward == null)
                    {
                        MazeNode disconnectedNode = findNode(current, counter);
                        if (disconnectedNode != null)
                        {
                            if (disconnectedNode.Col != 0 && disconnectedNode.Row != 0)
                            {
                                disconnected = DistanceBetween(current, disconnectedNode);
                                disconnectingWalls[current.Col, current.Row, counter] = disconnected;
                                disconnectedNodes[current.Col, current.Row, counter]  = current;
                            }
                        }
                    }
                }

                foreach (MazeNode n in current.GetAdjacentNodes())
                {
                    if (!visited2.Contains(n))
                    {
                        visited.Push(n);
                        visited2.Push(n);
                    }
                }
            }

            for (i = 0; i < size; i++)
            {
                for (j = 0; j < size; j++)
                {
                    for (k = 0; k < 4; k++)
                    {
                        if (disconnectingWalls[i, j, k] > largestDisconnection)
                        {
                            largestDisconnection    = disconnectingWalls[i, j, k];
                            largestDisconnectedNode = disconnectedNodes[i, j, k];
                            largestCol = i;
                            largestRow = j;
                            largestDir = k;
                        }
                    }
                }
            }

            MazeNode mazeNode = findNode(largestDisconnectedNode, largestDir);
            if (mazeNode != null)
            {
                largestDisconnectedNode.AddEdge(mazeNode);
                disconnectingWalls[largestCol, largestRow, largestDir] = 0;
                disconnectingWalls[mazeNode.Col, mazeNode.Row, (largestDir + 2) % 4] = 0;
            }
            loops--;
            largestDisconnection = 0;
            largestDir           = 0;
            largestCol           = 0;
            largestRow           = 0;
        }
    }