示例#1
0
        //Create
        public static Labyrinth Create(Cell[][] cells)
        {
            if (cells.Length > 50 || cells.Length < 5 || cells[0].Length < 5 || cells[0].Length > 50)
            {
                throw new SizeOutOfRange("Kruskal.SizeOutOfRange: Size of labyrinth out of range");
            }

            Labyrinth labyrinth = new Labyrinth(cells);

            labyrinth.SetOutsideWalls(false);
            Random random = new Random();
            int    Sets   = labyrinth.Rows * labyrinth.Columns;

            Cell CurrentCell;
            int  row, col, NextId = 1;

            while (Sets > 1)
            {
                row         = random.Next(0, labyrinth.Rows);
                col         = random.Next(0, labyrinth.Columns);
                CurrentCell = labyrinth[row, col];
                if (CurrentCell.IsWall())
                {
                    int WallId;
                    do
                    {
                        WallId = random.Next(0, 4);
                        if (CurrentCell.Walls[WallId])
                        {
                            break;
                        }
                    } while (true);
                    int NextRow = row, NextCol = col;
                    GetNextCell(ref labyrinth, ref NextRow, ref NextCol, WallId, out bool Correct);
                    if (!Correct)
                    {
                        continue;
                    }
                    UniteCells(ref labyrinth, new CellPoint(row, col), new CellPoint(NextRow, NextCol), ref NextId);
                    --Sets;
                }
                else
                {
                    continue;
                }
            }
            SendStepInfo?.Invoke(labyrinth, new StepInfo(new CellPoint(-1, -1), new CellPoint(-1, -1)));
            return(labyrinth);
        }