private void BuildWalls()
        {
            for (var currentRing = 1; currentRing < RingCount; currentRing++)
            {
                var cellsInRing      = _cellsInRingMap[currentRing];
                var cellsInInnerRing = _cellsInRingMap[currentRing - 1];
                var cellsInOuterRing = currentRing < RingCount - 1 ? _cellsInRingMap[currentRing + 1] : new Cell[0];

                var cellsInOrder = cellsInRing.Select(x => new { cell = x, pos = _cellPositionMap[x] })
                                   .OrderBy(x => x.pos.StartingDegree)
                                   .Select(x => x.cell)
                                   .ToArray();

                for (var index = 0; index < cellsInOrder.Length; index++)
                {
                    var cell          = cellsInOrder[index];
                    var adjacentCells = new List <Cell>();

                    // Find inner neighbor(s)
                    if (currentRing == 1)
                    {
                        // Auto bind it to center cell
                        adjacentCells.Add(_cells[0]);
                    }
                    else
                    {
                        adjacentCells.AddRange(cellsInInnerRing.Where(x => CheckIfCellsAlign(cell, x)));
                    }

                    // outer neighbors
                    if (cellsInOuterRing.Any())
                    {
                        adjacentCells.AddRange(cellsInOuterRing.Where(x => CheckIfCellsAlign(cell, x)));
                    }

                    // Find next cell to either side
                    adjacentCells.Add(index == 0 ? cellsInOrder[cellsInOrder.Length - 1] : cellsInOrder[index - 1]);
                    adjacentCells.Add(index == cellsInOrder.Length - 1 ? cellsInOrder[0] : cellsInOrder[index + 1]);

                    // build walls
                    foreach (var adjacentCell in adjacentCells)
                    {
                        // Make sure this wall doesn't already exist
                        if (adjacentCell.CellWalls.All(x => x.GetOtherCell(adjacentCell) != cell))
                        {
                            var wall = new CellWall(cell, adjacentCell);
                            cell.CellWalls.Add(wall);
                            adjacentCell.CellWalls.Add(wall);
                        }
                    }
                }
            }
        }
Exemple #2
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();
        }
Exemple #3
0
        protected static void LinkCellsIfNotAlreadyLinked(Cell current, Cell neighbor)
        {
            if (neighbor == null)
            {
                return;
            }

            if (neighbor.CellWalls.Any(x => x.GetOtherCell(neighbor) == current))
            {
                return;
            }

            var wall = new CellWall(current, neighbor);

            current.CellWalls.Add(wall);
            neighbor.CellWalls.Add(wall);
        }
        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();
        }