//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); }
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); GetSelection(out int row, out int col); if (row < 0 || row >= cells.Length || col < 0 || col >= cells[0].Length) { throw new CellOutOfRangeExeption("Prim.CellOutOfRangeException: Selected inexistent cell"); } labyrinth.ToInside(row, col); Random random = new Random(); CellPoint cellPoint; while (labyrinth.BordersCount > 0) { int SomeBorder = random.Next(0, labyrinth.BordersCount); for (int irow = 0, k = 0; irow < labyrinth.Rows; ++irow) { for (int icol = 0; icol < labyrinth.Columns; ++icol) { if (labyrinth[irow, icol].Atribut == Atributs.Border) { if (k == SomeBorder) { labyrinth.ToInside(irow, icol); cellPoint = labyrinth.ConnectToInside(irow, icol); SendStepInfo?.Invoke(labyrinth, new StepInfo(cellPoint, new CellPoint(irow, icol))); irow = labyrinth.Rows; break; } else { ++k; } } } } } SendStepInfo?.Invoke(cells, new StepInfo(new CellPoint(-1, -1), new CellPoint(-1, -1))); return(labyrinth); }
private static void GetNextCell(ref Labyrinth labyrinth, ref int row, ref int col, int WallId, out bool Correct) { Correct = true; int LastRow = row; int LastCol = col; int LastId = WallId; if (WallId == 0) { --row; WallId = 2; } else if (WallId == 1) { ++col; WallId = 3; } else if (WallId == 2) { ++row; WallId = 0; } else if (WallId == 3) { --col; WallId = 1; } if (labyrinth[LastRow, LastCol].Id == labyrinth[row, col].Id) { if (labyrinth[row, col].Id != 0) { Correct = false; } } if (Correct) { Cell cell = labyrinth[row, col]; cell.Walls[WallId] = false; cell = labyrinth[LastRow, LastCol]; cell.Walls[LastId] = false; SendStepInfo?.Invoke(labyrinth, new StepInfo(new CellPoint(row, col), new CellPoint(LastRow, LastCol))); } }