public override async void GenerateMaze()
        {
            while (cells.Count > 0)
            {
                await Mazes.PaintUpdate();

                (var row, var col) = (current.Row, current.Col);
                var adjacent = Adjacent(row, col);

                if (adjacent != -1)
                {
                    (var newRow, var newCol) = Direction(row, col, Cardinal[adjacent]);
                    current = Forge(row, col, newRow, newCol, adjacent);
                    cells.Push(current);
                }
                else
                {
                    current = cells.Pop();
                }

                Mazes.Current = current;
            }

            await Mazes.PaintUpdate(true);
        }
        public override async void GenerateMaze()
        {
            var totalVisited = 0;

            while (totalVisited < Mazes.MazeWidth * Mazes.MazeHeight - 1)
            {
                await Mazes.PaintUpdate();

                var point = Mazes.RNG.Next(Cardinal.Length) % Cardinal.Length;
                (var row, var col) = Direction(current.Row, current.Col, Cardinal[point]);

                if ((row, col) != (-1, -1))
                {
                    if (!Mazes.Cells[row, col].Visited)
                    {
                        current = Forge(current.Row, current.Col, row, col, point);
                        totalVisited++;
                    }
                    else
                    {
                        current = Mazes.Cells[row, col];
                    }
                }

                Mazes.Current = current;
            }

            await Mazes.PaintUpdate(true);
        }
Example #3
0
        public override async void GenerateMaze()
        {
            Mazes.Cell Choose(bool visited)
            {
                var chosen = new Mazes.Cell(-1, -1);

                do
                {
                    chosen = Mazes.Cells[Mazes.RNG.Next(Mazes.MazeHeight), Mazes.RNG.Next(Mazes.MazeWidth)];
                } while (chosen.Visited == visited || chosen == current);

                return(chosen);
            }

            var totalVisited = 0;

            current         = Choose(true);
            current.Visited = true;
            var initial = new Mazes.Cell(-1, -1);

            while (totalVisited < Mazes.MazeWidth * Mazes.MazeHeight - 1)
            {
                await Mazes.PaintUpdate();

                var chosen = Choose(true);
                var first  = Mazes.Cells[chosen.Row, chosen.Col];

                while (!chosen.Visited)
                {
                    await Mazes.PaintUpdate();

                    (var row, var col) = (-1, -1);

                    do
                    {
                        (row, col) = Direction(chosen.Row, chosen.Col, Cardinal[directions[chosen.Row, chosen.Col] = Mazes.RNG.Next(Cardinal.Length)]);
                    } while ((row, col) == (-1, -1));

                    Mazes.Current = initial = chosen = Mazes.Cells[row, col];
                }

                chosen = Mazes.Cells[first.Row, first.Col];

                while (chosen != initial)
                {
                    await Mazes.PaintUpdate();

                    (var row, var col) = Direction(chosen.Row, chosen.Col, Cardinal[directions[chosen.Row, chosen.Col]]);
                    Mazes.Current      = chosen = Forge(chosen.Row, chosen.Col, row, col, directions[chosen.Row, chosen.Col]);
                    totalVisited++;
                }
            }

            await Mazes.PaintUpdate(true);
        }
        public override async void GenerateMaze()
        {
            while (true)
            {
                await Mazes.PaintUpdate();

                (var row, var col) = (current.Row, current.Col);
                var adjacent = Adjacent(row, col);

                if (adjacent == -1)
                {
                    for (var r = 0; r < Mazes.MazeHeight; r++)
                    {
                        for (var c = 0; c < Mazes.MazeWidth; c++)
                        {
                            (row, col) = (r, c);

                            if (Mazes.Cells[row, col].Visited)
                            {
                                adjacent = Adjacent(row, col);

                                if (adjacent != -1)
                                {
                                    goto Path;
                                }
                            }
                        }
                    }

                    break;
                }

Path:
                (var newRow, var newCol) = Direction(row, col, Cardinal[adjacent]);
                Mazes.Current            = current = Forge(row, col, newRow, newCol, adjacent);
            }

            await Mazes.PaintUpdate(true);
        }