Beispiel #1
0
        public override double Evaluate(PM_Maze maze)
        {
            int numNodes   = maze.CellsPositions_All_Set().Count;
            int numCorners = maze.Num_Cells_Corners();

            double percentage = (double)numCorners / (double)numNodes;

            return(percentage);
        }
        public static List <HashSet <Vec2i> > Islands(
            this PM_Maze maze,
            Random rand)
        {
            List <HashSet <Vec2i> > islands = new List <HashSet <Vec2i> >();

            HashSet <Vec2i> allCells = maze.CellsPositions_All_Set();

            while (allCells.Count > 0)
            {
                Vec2i           randomCell = allCells.Random_Item(rand);
                HashSet <Vec2i> island     = maze.BFS_ReachableCells_Set(randomCell, true);
                islands.Add(island);
                allCells.ExceptWith(island);
                //allCells.RemoveWhere(x => island.Contains(x));
            }

            return(islands);
        }
        public static void Repair_Connectivity(
            this PM_Maze maze,
            Random rand
            )
        {
            List <HashSet <Vec2i> > islands = maze.Islands(rand);

            while (islands.Count > 1)
            {
                HashSet <Vec2i> selectedIsland = islands.Pop_Random_Item(rand);

                HashSet <Vec2i> allOtherNodes = maze.CellsPositions_All_Set();
                allOtherNodes.ExceptWith(selectedIsland);

                HashSet <Vec2i> frontier = new HashSet <Vec2i>();
                foreach (var cell in selectedIsland)
                {
                    //HashSet<PM_V2> inactiveEdges = maze.

                    bool         isFrontier = false;
                    List <Vec2i> geomNei    = maze.Cell_GeometricNeighbors_List(cell);
                    foreach (var nei in geomNei)
                    {
                        if (selectedIsland.Contains(nei) == false)
                        {
                            isFrontier = true;
                            break;
                        }
                    }

                    if (isFrontier)
                    {
                        frontier.Add(cell);
                    }
                }

                Vec2i           selectedNode  = frontier.Random_Item(rand);
                HashSet <Vec2i> possibleJoins = maze.Cell_GeometricNeighbors_Set(selectedNode);
                possibleJoins.ExceptWith(selectedIsland);

                Vec2i joinedNode = possibleJoins.Random_Item(rand);

                maze.OP_AddEdge(new UEdge2i(selectedNode, joinedNode));

                int joinedIslandIndex = -1;
                for (int i = 0; i < islands.Count; i++)
                {
                    if (islands[i].Contains(joinedNode))
                    {
                        joinedIslandIndex = i;
                        break;
                    }
                }
                HashSet <Vec2i> joinedIsland = islands[joinedIslandIndex];

                islands.RemoveAt(joinedIslandIndex);

                joinedIsland.UnionWith(selectedIsland);

                islands.Add(joinedIsland);
            }
        }