예제 #1
0
        /// <summary>
        /// Handling explosions.
        /// </summary>
        /// <param name="cell">Coordinates and eventually value of the cell.</param>
        /// <param name="chainEnabled">Boolean value if chain reaction enabled.</param>
        /// <param name="chainedBombs">Array for holding chained bombs.</param>
        /// <returns>Number of bombs detonated.</returns>
        public int Explode(Cell cell, bool chainEnabled, CompositeBomb chainedBombs)
        {
            var fieldRow = cell.Position.Row;
            var fieldCol = cell.Position.Col;

            // by default cell comes with coordinates only - ex. ChainReaction
            if (cell.Value == 0)
            {
                cell.Value = this.Grid[fieldRow, fieldCol].Value;

                // if no bomb  - detonate the cell and exit the method
                if (cell.Value == 0)
                {
                    this.Grid[fieldRow, fieldCol].Value = DetonatedCell;
                    return 0;
                }
            }

            var bomb = new ExplosionStrategy(cell.Value);
            var explosion = this.InvertExplosion ? bomb.GetInvertedExplosion() : bomb.GetExplosion();

            this.InvertExplosion = false;

            var minesExplodedThisRound = 0;

            // bomb explodes
            for (var explosionRow = -BombRadius; explosionRow <= BombRadius; explosionRow++)
            {
                for (var explosionCol = -BombRadius; explosionCol <= BombRadius; explosionCol++)
                {
                    if (Validators.IsInBounds(fieldRow + explosionRow, this.Size) &&
                        Validators.IsInBounds(fieldCol + explosionCol, this.Size))
                    {
                        if (explosion[explosionRow + BombRadius, explosionCol + BombRadius] == DetonationSpot)
                        {
                            if (this.Grid[fieldRow + explosionRow, fieldCol + explosionCol].Value > EmptyCell)
                            {
                                if (chainEnabled)
                                {
                                    var clonedCell = this.Grid[fieldRow + explosionRow, fieldCol + explosionCol].Clone() as Cell;
                                    chainedBombs.Add(clonedCell);
                                    continue;
                                }

                                minesExplodedThisRound++;
                            }

                            this.Grid[fieldRow + explosionRow, fieldCol + explosionCol].Value = DetonatedCell;
                        }
                    }
                }
            }

            return minesExplodedThisRound;
        }
        /// <summary>
        /// Player shoots in playfield
        /// </summary>
        /// <returns>Bombs detonated.</returns>
        public int TakeAShot()
        {
            var coordinates = this.input.GetTargetCoordinates();

            var chainedBombs = new CompositeBomb();

            var bombsDetonated = this.Field.Explode(new Cell(coordinates), this.ChainReactionEnabled, chainedBombs);

            if (this.ChainReactionEnabled)
            {
                this.ChainReactionEnabled = false;
                bombsDetonated += chainedBombs.ChainReact(this.Field);
            }

            return bombsDetonated;
        }