Пример #1
0
        public HexMaze(int rowCount, int columnCount, WallSetupAlgorithm algorithm)
        {
            RowCount    = rowCount;
            ColumnCount = columnCount;

            Cells = new Cell[rowCount * columnCount];
            for (var row = 0; row < rowCount; row++)
            {
                for (var column = 0; column < columnCount; column++)
                {
                    var index = (row * columnCount) + column;
                    var cell  = new Cell();

                    Cells[index]       = cell;
                    CellIndexMap[cell] = index;

                    // Link cells together if they've been created so far.  Coordinates assume offset rows and straight
                    // columns with the first cell top left most of all cells.
                    var northWestCell = GetCell(column % 2 == 1 ? row : row - 1, column - 1);
                    var northCell     = GetCell(row - 1, column);
                    var northEastCell = GetCell(column % 2 == 1 ? row : row - 1, column + 1);
                    var southWestCell = GetCell(column % 2 == 1 ? row + 1 : row, column - 1);

                    LinkCellsIfNotAlreadyLinked(cell, northWestCell);
                    LinkCellsIfNotAlreadyLinked(cell, northCell);
                    LinkCellsIfNotAlreadyLinked(cell, northEastCell);
                    LinkCellsIfNotAlreadyLinked(cell, southWestCell);
                }
            }

            SetupWalls(algorithm);
            SetStartAndFinishingCells();
        }
Пример #2
0
        private void SetupWalls(WallSetupAlgorithm setupAlgorithm)
        {
            switch (setupAlgorithm)
            {
            case WallSetupAlgorithm.BinaryTree:
                new BinaryTree().Run(this);
                break;

            case WallSetupAlgorithm.Sidewinder:
                new Sidewinder().Run(this);
                break;

            case WallSetupAlgorithm.AldousBroder:
                new AldousBroder().Run(this);
                break;

            case WallSetupAlgorithm.Wilson:
                new Wilson().Run(this);
                break;

            case WallSetupAlgorithm.HuntAndKill:
                new HuntAndKill().Run(this);
                break;

            case WallSetupAlgorithm.RecursiveBackTracker:
                new RecursiveBackTracker().Run(this);
                break;

            default:
                throw new NotSupportedException($"Algorithm {setupAlgorithm} not supported");
            }
        }
Пример #3
0
        public TriangleMaze(int rowCount, int columnCount, WallSetupAlgorithm algorithm)
        {
            RowCount    = rowCount;
            ColumnCount = columnCount;

            Cells = new Cell[rowCount * columnCount];
            for (var row = 0; row < rowCount; row++)
            {
                for (var column = 0; column < columnCount; column++)
                {
                    var index = (row * columnCount) + column;
                    var cell  = new Cell();

                    Cells[index]       = cell;
                    CellIndexMap[cell] = index;

                    var leftCell = GetCell(row, column - 1);
                    if (leftCell != null)
                    {
                        LinkCellsIfNotAlreadyLinked(cell, leftCell);
                    }

                    var topCell = !IsCellPointingUp(row, column) ? GetCell(row - 1, column) : null;
                    if (topCell != null)
                    {
                        LinkCellsIfNotAlreadyLinked(cell, topCell);
                    }
                }
            }

            SetupWalls(algorithm);
            SetStartAndFinishingCells();
        }
Пример #4
0
        public MazeStats(IMaze maze, WallSetupAlgorithm wallSetupAlgorithm)
        {
            if (maze == null)
            {
                throw new ArgumentNullException(nameof(maze));
            }

            AddStat("Maze Type", maze.GetType().Name.AddSpacesBetweenUpperCaseLetters());
            AddStat("Algorithm", wallSetupAlgorithm.ToString().AddSpacesBetweenUpperCaseLetters());
            AddMazeSpecificStats(maze);
            AddStat("Total Cells", maze.AllCells.Count.ToString());
            AddDeadCellCount(maze);
        }
Пример #5
0
        public MaskedMaze(int rowCount, int columnCount, IReadOnlyList <bool> mask, WallSetupAlgorithm algorithm)
        {
            if (mask == null)
            {
                throw new ArgumentNullException(nameof(mask));
            }
            if (rowCount * columnCount != mask.Count)
            {
                throw new ArgumentException($"Expected mask to have {rowCount * columnCount} values, instead had {mask.Count}");
            }

            RowCount    = rowCount;
            ColumnCount = columnCount;
            Cells       = new Cell[rowCount * columnCount];
            for (var row = 0; row < rowCount; row++)
            {
                for (var column = 0; column < columnCount; column++)
                {
                    var index = (row * columnCount) + column;
                    if (mask[index])
                    {
                        var cell = new Cell();

                        // Setup adjacent walls
                        var northernCell = row > 0 ? GetCell(row - 1, column) : null;
                        if (northernCell != null)
                        {
                            var wall = new CellWall(cell, northernCell);
                            cell.CellWalls.Add(wall);
                            northernCell.CellWalls.Add(wall);
                        }

                        var westernCell = column > 0 ? GetCell(row, column - 1) : null;
                        if (westernCell != null)
                        {
                            var wall = new CellWall(cell, westernCell);
                            cell.CellWalls.Add(wall);
                            westernCell.CellWalls.Add(wall);
                        }

                        Cells[index]       = cell;
                        CellIndexMap[cell] = index;
                    }
                }
            }

            VerifyMazeIsValid();
            SetupWalls(algorithm);
            SetStartingAndFinishingCell();
        }
Пример #6
0
        public RectangularMaze(int rowCount, int columnCount, WallSetupAlgorithm setupAlgorithm)
        {
            RowCount    = rowCount;
            ColumnCount = columnCount;

            Cells = new Cell[rowCount * columnCount];
            for (var row = 0; row < rowCount; row++)
            {
                for (var column = 0; column < columnCount; column++)
                {
                    var index = (row * columnCount) + column;
                    var cell  = new Cell();

                    // Setup adjacent walls
                    var northernCell = row > 0 ? GetCell(row - 1, column) : null;
                    if (northernCell != null)
                    {
                        var wall = new CellWall(cell, northernCell);
                        cell.CellWalls.Add(wall);
                        northernCell.CellWalls.Add(wall);
                    }

                    var westernCell = column > 0 ? GetCell(row, column - 1) : null;
                    if (westernCell != null)
                    {
                        var wall = new CellWall(cell, westernCell);
                        cell.CellWalls.Add(wall);
                        westernCell.CellWalls.Add(wall);
                    }

                    Cells[index]       = cell;
                    CellIndexMap[cell] = index;
                }
            }

            SetupWalls(setupAlgorithm);
            SetStartingAndEndingCells();
        }
Пример #7
0
        private void SetupWalls(WallSetupAlgorithm algorithm)
        {
            switch (algorithm)
            {
            case WallSetupAlgorithm.Wilson:
                new Wilson().Run(this);
                break;

            case WallSetupAlgorithm.AldousBroder:
                new AldousBroder().Run(this);
                break;

            case WallSetupAlgorithm.HuntAndKill:
                new HuntAndKill().Run(this);
                break;

            case WallSetupAlgorithm.RecursiveBackTracker:
                new RecursiveBackTracker().Run(this);
                break;

            default:
                throw new NotSupportedException($"Masked mazes does not support the {algorithm} algorithm");
            }
        }
Пример #8
0
        public CircularMaze(int ringCount, int cellCountScaleFactor, int halveFactor, WallSetupAlgorithm algorithm)
        {
            RingCount = ringCount;

            var cells = new List <Cell>();

            for (var currentRing = 0; currentRing < ringCount; currentRing++)
            {
                var cellCountInRing = currentRing == 0 ? 1 : (int)Math.Pow(2, currentRing / halveFactor) * cellCountScaleFactor;
                var degreesPerCell  = 360f / cellCountInRing;
                var currentDegree   = 0f;

                var cellsInRing = new List <Cell>();
                for (var cellIndex = 0; cellIndex < cellCountInRing; cellIndex++)
                {
                    var cell     = new Cell();
                    var position = new CircularPosition(currentRing, currentDegree, currentDegree + degreesPerCell);
                    cellsInRing.Add(cell);
                    cells.Add(cell);
                    _cellPositionMap[cell] = position;

                    currentDegree += degreesPerCell;
                }

                _cellsInRingMap[currentRing] = cellsInRing.ToArray();
            }

            _cells = cells.ToArray();
            BuildWalls();
            SetupWalls(algorithm);
            SetStartingAndFinishingCells();
        }