Esempio n. 1
0
        private MineField(int zoneSize, int numberOfBombs)
        {
            if (numberOfBombs <= 0)
            {
                throw new NoBombException();
            }

            if (numberOfBombs >= zoneSize * zoneSize)
            {
                throw new TooManyBombsException();
            }

            BombCount = numberOfBombs;
            ZoneSize  = zoneSize;

            _cells = new ICell[zoneSize, zoneSize];

            GenerateGameCells();
            PutBombs();
            ScanNumberOfBombsAroundEmptyCells();

            void GenerateGameCells()
            {
                for (var row = 0; row < zoneSize; row++)
                {
                    for (var column = 0; column < zoneSize; column++)
                    {
                        _cells[row, column] = new UndiscoveredCell(row, column);
                    }
                }
            }

            void PutBombs()
            {
                var generator = new BombGenerator(numberOfBombs);

                do
                {
                    _cells.OfType <UndiscoveredCell>()
                    .Where(a => !a.HasBomb)
                    .ForEach(a => a.HasBomb = generator.Put());
                } while (generator.BombNumber > 0);
            }

            void ScanNumberOfBombsAroundEmptyCells()
            {
                _cells.OfType <UndiscoveredCell>()
                .Where(a => a.HasBomb)
                .SelectMany(CellsAround)
                .ForEach(u => ((UndiscoveredCell)u).NumberOfBombsAround++);
            }
        }
Esempio n. 2
0
 public FlaggedCell(int row, int column, UndiscoveredCell source) : base(row, column)
 {
     _source = source ?? throw new ArgumentNullException(nameof(source));
 }