Beispiel #1
0
        public override async void GenerateMaze()
        {
            while (true)
            {
                await Mazes.PaintUpdate();

                (var choice, var adjacent) = (-1, -1);
                (var row, var col)         = (0, 0);

                while (adjacent == -1)
                {
                    if (choice != -1)
                    {
                        cells.RemoveAt(choice);
                    }
                    if (cells.Count <= 0)
                    {
                        goto GenerationEnd;
                    }

                    choice = (Mazes.RNG.Next(2) == 0) ? 0 : cells.Count - 1;
                    var cell = cells[choice];
                    (row, col) = (cell.Row, cell.Col);
                    adjacent   = Adjacent(row, col);
                }

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

GenerationEnd:
            await Mazes.PaintUpdate(true);
        }
        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);
        }
Beispiel #3
0
        public override async void GenerateMaze()
        {
            var choices = new List <Mazes.Cell>();

            for (var r = 0; r < Mazes.MazeHeight; r++)
            {
                for (var c = 0; c < Mazes.MazeWidth; c++)
                {
                    await Mazes.PaintUpdate();

                    var cell = Mazes.Current = Mazes.Cells[r, c];
                    cell.Visited = true;
                    var cardinal = "N";

                    if (r == 0)
                    {
                        cardinal = "E";
                    }
                    else if (c == Mazes.MazeWidth - 1)
                    {
                        cardinal = "N";
                    }
                    else
                    {
                        cardinal = new[] { "N", "E" }
                    }.Pick();
Beispiel #4
0
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     App = new Mazes();
     Application.Run(App);
 }
Beispiel #5
0
        public override async void GenerateMaze()
        {
            int ChooseOrientation(int width, int height) =>
            (width < height) ? 0 : (height < width) ? 1 : Mazes.RNG.Next(2);

            async Task Division(int row, int col, int width, int height, int orientation)
            {
                if (width < 2 || height < 2)
                {
                    return;
                }

                await Mazes.PaintUpdate();

                var horizontal = (orientation == 0);

                var wallRow = row + horizontal.ThenDefault(Mazes.RNG.Next(height - 2));
                var wallCol = col + (!horizontal).ThenDefault(Mazes.RNG.Next(width - 2));
                var passRow = wallRow + (!horizontal).ThenDefault(Mazes.RNG.Next(height));
                var passCol = wallCol + horizontal.ThenDefault(Mazes.RNG.Next(width));

                (var dirRow, var dirCol) = (horizontal) ? (0, 1) : (1, 0);

                var length = (horizontal) ? width : height;
                var dir    = (horizontal) ? 3 : 1;

                for (var i = 0; i < length; i++)
                {
                    Mazes.Current         = Mazes.Cells[wallRow, wallCol];
                    Mazes.Current.Visited = true;

                    await Mazes.PaintUpdate();

                    if (wallRow != passRow || wallCol != passCol)
                    {
                        Mazes.Cells[wallRow, wallCol][Cardinal[dir]] = false;
                        (var newRow, var newCol) = Direction(wallRow, wallCol, Cardinal[dir]);
                        Mazes.Cells[newRow, newCol][Opposite[dir]] = false;
                    }

                    wallRow += dirRow;
                    wallCol += dirCol;
                }

                (var nextWidth, var nextHeight) = (horizontal) ? (width, wallRow - row + 1) : (wallCol - col + 1, height);

                await Division(row, col, nextWidth, nextHeight, ChooseOrientation(nextWidth, nextHeight));

                (var nextRow, var nextCol) = (horizontal) ? (wallRow + 1, col) : (row, wallCol + 1);
                (nextWidth, nextHeight)    = (horizontal) ? (width, row + height - wallRow - 1) : (col + width - wallCol - 1, height);

                await Division(nextRow, nextCol, nextWidth, nextHeight, ChooseOrientation(nextWidth, nextHeight));
            }

            await Division(0, 0, Mazes.MazeWidth, Mazes.MazeHeight, ChooseOrientation(Mazes.MazeWidth, Mazes.MazeHeight));

            await Mazes.PaintUpdate(true);
        }
Beispiel #6
0
        public override async void GenerateMaze()
        {
            while (!sets.ToArray1D().All(x => x == sets[0, 0]))
            {
                await Mazes.PaintUpdate();

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

                while (point == -1)
                {
                    (row, col) = (Mazes.RNG.Next(Mazes.MazeHeight), Mazes.RNG.Next(Mazes.MazeWidth));
                    point      = Adjacent(row, col, sets);
                }

                (var newRow, var newCol) = Direction(row, col, Cardinal[point]);
                Mazes.Current            = Mazes.Cells[newRow, newCol];
                Union(newRow, newCol, sets[newRow, newCol], sets);
            }

            await Mazes.PaintUpdate(true);
        }
        public override async void GenerateMaze()
        {
            for (var r = 0; r < Mazes.MazeHeight; r++)
            {
                for (var c = 0; c < Mazes.MazeWidth; c++)
                {
                    await Mazes.PaintUpdate();

                    var cell     = Mazes.Cells[r, c];
                    var cardinal = "N";

                    if (r == 0)
                    {
                        cardinal = "W";
                    }
                    else if (c == 0)
                    {
                        cardinal = "N";
                    }
                    else
                    {
                        cardinal = new[] { "N", "W" }
                    }.Pick();
Beispiel #8
0
        public override async void GenerateMaze()
        {
            void JoinSet(int row, int col, int newRow, int newCol, ref bool joined)
            {
                Forge(row, col, newRow, newCol, 0);
                Union(row, col, sets[row, col], sets);
                joined = true;
            }

            for (var r = 0; r < Mazes.MazeHeight; r++)
            {
                if (r != 0)
                {
                    var joined = false;

                    for (var c = Mazes.MazeWidth - 1; c >= 0; c--)
                    {
                        await Mazes.PaintUpdate();

                        Mazes.Current                = Mazes.Cells[r, c];
                        (var setRow, var setCol)     = Direction(r, c, "N");
                        (var checkRow, var checkCol) = Direction(r, c - 1, "N");

                        if (c == Mazes.MazeWidth - 1)
                        {
                            sets[r, c] = sets[setRow, setCol];
                        }
                        if (c == 0 && !joined || (checkRow, checkCol) != (-1, -1) && !joined && sets[checkRow, checkCol] != sets[r, c] || Mazes.RNG.Next(5) == 0)
                        {
                            JoinSet(r, c, setRow, setCol, ref joined);
                        }

                        if ((checkRow, checkCol) != (-1, -1) && sets[checkRow, checkCol] != sets[setRow, setCol])
                        {
                            joined = false;
                        }
                    }
                }

                for (var c = 0; c < Mazes.MazeWidth; c++)
                {
                    Mazes.Cells[r, c].Visited = true;

                    await Mazes.PaintUpdate();

                    if (Mazes.RNG.Next(2) == 0 || r == Mazes.MazeHeight - 1)
                    {
                        (var row, var col) = Direction(r, c, "E");

                        if ((row, col) != (-1, -1) && sets[row, col] != sets[r, c])
                        {
                            Forge(r, c, row, col, 1);
                            Union(row, col, sets[row, col] = sets[r, c], sets);
                        }
                    }

                    Mazes.Current = Mazes.Cells[r, c];
                }
            }

            await Mazes.PaintUpdate(true);
        }