Exemplo n.º 1
0
        public override void Detonate(Position position, IGameboard gameboard)
        {
            int x = position.X;
            int y = position.Y;
            for (int i = x - 1; i <= x + 1; i++)
            {
                for (int j = y - 1; j <= y + 1; j++)
                {
                    if (this.IsInsideField(gameboard, i, j))
                    {
                        this.ExplodeOneMine(gameboard, i, j);
                    }
                }
            }

            if (this.IsInsideField(gameboard, x - 2, y))
            {
                this.ExplodeOneMine(gameboard, x - 2, y);
            }

            if (this.IsInsideField(gameboard, x + 2, y))
            {
                this.ExplodeOneMine(gameboard, x + 2, y);
            }

            if (this.IsInsideField(gameboard, x, y - 2))
            {
                this.ExplodeOneMine(gameboard, x, y - 2);
            }

            if (this.IsInsideField(gameboard, x, y + 2))
            {
                this.ExplodeOneMine(gameboard, x, y + 2);
            }
        }
Exemplo n.º 2
0
 public override void Detonate(Position position, IGameboard gameboard)
 {
     for (int i = position.X - 1; i <= position.X + 1; i++)
     {
         for (int j = position.Y - 1; j <= position.Y + 1; j++)
         {
             if (this.IsInsideField(gameboard, i, j))
             {
                 this.ExplodeOneMine(gameboard, i, j);
             }
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// A Factory method. Returns a detonator depending on the mine radius.
        /// </summary>
        /// <param name="position">The position to be detonated</param>
        /// <param name="field">The field to which the position belongs</param>
        public DetonationPattern GetDetonationPattern(Position position, IGameboard gameboard)
        {
            DetonationPattern detonationPattern = null;

            Mine mine = (Mine)gameboard[position.X, position.Y];
            switch (mine.Radius)
            {
                case MineRadius.MineRadiusOne:
                    {
                        detonationPattern = new RadiusOneDetonator();
                        break;
                    }

                case MineRadius.MineRadiusTwo:
                    {
                        detonationPattern = new RadiusTwoDetonator();
                        break;
                    }

                case MineRadius.MineRadiusThree:
                    {
                        detonationPattern = new RadiusThreeDetonator();
                        break;
                    }

                case MineRadius.MineRadiusFour:
                    {
                        detonationPattern = new RadiusFourDetonator();
                        break;
                    }

                case MineRadius.MineRadiusFive:
                    {
                        detonationPattern = new RadiusFiveDetonator();
                        break;
                    }

                default:
                    {
                        throw new InvalidOperationException("No detonation pattern exists!");
                    }
            }

            return detonationPattern;
        }
Exemplo n.º 4
0
        public override void Detonate(Position position, IGameboard gameboard)
        {
            for (int i = position.X - 2; i <= position.X + 2; i++)
            {
                for (int j = position.Y - 2; j <= position.Y + 2; j++)
                {
                    bool ur = i == position.X - 2 && j == position.Y - 2;
                    bool ul = i == position.X - 2 && j == position.Y + 2;
                    bool dr = i == position.X + 2 && j == position.Y - 2;
                    bool dl = i == position.X + 2 && j == position.Y + 2;

                    if (ur)
                    {
                        continue;
                    }

                    if (ul)
                    {
                        continue;
                    }

                    if (dr)
                    {
                        continue;
                    }

                    if (dl)
                    {
                        continue;
                    }

                    if (this.IsInsideField(gameboard, i, j))
                    {
                        this.ExplodeOneMine(gameboard, i, j);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void GenerateMinesInField(IGameboard gameboard, int minesCount)
        {
            int size = gameboard.Size;
            Array mineRadiusValues = Enum.GetValues(typeof(MineRadius));
            int mineRadiusMaxIndex = mineRadiusValues.Length - 1;

            List<Position> usedPositions = new List<Position>();
            while (usedPositions.Count < minesCount)
            {
                int cellX = this.rand.GetRandom(0, size - 1);
                int cellY = this.rand.GetRandom(0, size - 1);
                Position position = new Position(cellX, cellY);
                if (usedPositions.Contains(position))
                {
                    continue;
                }

                usedPositions.Add(position);
                int cellType = this.rand.GetRandom(0, mineRadiusMaxIndex);
                MineRadius randomRadius = (MineRadius)mineRadiusValues.GetValue(cellType);
                ICell currentCell = new Mine(randomRadius);
                gameboard[cellX, cellY] = currentCell;
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Detonate the mine at position 'position'.
 /// </summary>
 public void Detonate(Position position)
 {
     DetonationPattern detonationPattern = this.detonationFactory.GetDetonationPattern(position, this);
     detonationPattern.Detonate(position, this);
 }
Exemplo n.º 7
0
        public void TestDetonate5WithAllMinesInsideFieldDoesNotDetonateUnnecessaryCells()
        {
            int n = 9;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(4, 4);
            board[4, 4] = new Mine(MineRadius.MineRadiusFive);
            board.Detonate(position);
            bool cellsAreDetonated = false;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (!((2 <= i) && (i <= 6) && (2 <= j) && (j <= 6)))
                    {
                        if (board[i, j].Exploded)
                        {
                            cellsAreDetonated = true;
                            break;
                        }
                    }
                }

                if (cellsAreDetonated)
                {
                    break;
                }
            }

            Assert.IsFalse(cellsAreDetonated);
        }
Exemplo n.º 8
0
        public void TestDetonate5WithAllMinesInsideFieldDetonates25Cells()
        {
            int n = 9;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(4, 4);
            board[4, 4] = new Mine(MineRadius.MineRadiusFive);
            board.Detonate(position);
            bool cellsAreDetonated = true;
            for (int i = 2; i <= 6; i++)
            {
                for (int j = 2; j <= 6; j++)
                {
                    if (!board[i, j].Exploded)
                    {
                        cellsAreDetonated = false;
                        break;
                    }
                }

                if (!cellsAreDetonated)
                {
                    break;
                }
            }

            Assert.IsTrue(cellsAreDetonated);
        }
Exemplo n.º 9
0
        public void TestDetonate4WithAllMinesInsideFieldDoesNotDetonateUnnecessaryCells()
        {
            int n = 9;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(4, 4);
            board[4, 4] = new Mine(MineRadius.MineRadiusFour);
            board.Detonate(position);
            bool moreThan21CellsAreDetonated = false;
            int i = 0;
            int j = 0;
            for (; i < n; i++)
            {
                for (; j < n; j++)
                {
                    if (!((2 <= i) && (i <= 6) && (2 <= j) && (j <= 6)) ||
                            (i == 2 && j == 2) ||
                            (i == 2 && j == 6) ||
                            (i == 6 && j == 6) ||
                            (i == 6 && j == 2))
                    {
                        if (board[i, j].Exploded)
                        {
                            moreThan21CellsAreDetonated = false;
                            break;
                        }
                    }
                }

                if (!moreThan21CellsAreDetonated)
                {
                    break;
                }
            }

            Assert.IsFalse(moreThan21CellsAreDetonated);
        }
Exemplo n.º 10
0
        public void TestDetonate4WithAllMinesInsideFieldDetonates21Cells()
        {
            int n = 7;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(3, 3);
            board[3, 3] = new Mine(MineRadius.MineRadiusFour);
            board.Detonate(position);
            bool cellsAreDetonated = true;
            for (int i = 2; i <= 4; i++)
            {
                for (int j = 1; j <= 5; j++)
                {
                    if (!board[i, j].Exploded)
                    {
                        cellsAreDetonated = false;
                        break;
                    }
                }

                if (!cellsAreDetonated)
                {
                    break;
                }
            }

            cellsAreDetonated = cellsAreDetonated &&
                    board[1, 2].Exploded == board[1, 3].Exploded &&
                    board[1, 3].Exploded == board[1, 4].Exploded &&
                    board[1, 4].Exploded == board[5, 2].Exploded &&
                    board[5, 2].Exploded == board[5, 3].Exploded &&
                    board[5, 3].Exploded == board[5, 4].Exploded &&
                    board[5, 4].Exploded == true;

            Assert.IsTrue(cellsAreDetonated);
        }
Exemplo n.º 11
0
        public void TestDetonate4With8MinesInsideFieldDetonates8Cells()
        {
            int n = 7;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(0, 6);
            board[0, 6] = new Mine(MineRadius.MineRadiusFour);
            board.Detonate(position);
            bool cellsAreDetonated =
                    board[0, 4].Exploded == board[0, 5].Exploded &&
                    board[0, 5].Exploded == board[0, 6].Exploded &&
                    board[0, 6].Exploded == board[1, 4].Exploded &&
                    board[1, 4].Exploded == board[1, 5].Exploded &&
                    board[1, 5].Exploded == board[1, 6].Exploded &&
                    board[1, 6].Exploded == board[2, 5].Exploded &&
                    board[2, 5].Exploded == board[2, 6].Exploded &&
                    board[2, 6].Exploded == true;

            Assert.IsTrue(cellsAreDetonated);
        }
Exemplo n.º 12
0
        public void TestDetonate3WithAllMinesInsideFieldDoesNotDetonateUnnecessaryCells()
        {
            int n = 9;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(4, 4);
            board[4, 4] = new Mine(MineRadius.MineRadiusThree);
            board.Detonate(position);
            bool moreThanThirteenCellsDetonated = false;
            int i = 0;
            int j = 0;
            for (; i < n; i++)
            {
                for (; j < n; j++)
                {
                    if (!((3 <= i) && (i <= 5) && (3 <= j) && (j <= 5)) &&
                        (i != 2 && j != 4) &&
                        (i != 4 && j != 6) &&
                        (i != 6 && j != 4) &&
                        (i != 2 && j != 2))
                    {
                        if (board[i, j].Exploded)
                        {
                            moreThanThirteenCellsDetonated = true;
                            break;
                        }
                    }
                }

                if (moreThanThirteenCellsDetonated)
                {
                    break;
                }
            }

            Assert.IsFalse(moreThanThirteenCellsDetonated, String.Format("A cell was detonated at {0}, {1}", i, j));
        }
Exemplo n.º 13
0
        public void TestDetonate3With6MinesInsideFieldDetonates6Cells()
        {
            int n = 7;
            IGameboard board = this.GenerateGameboard(n);
            Position position = new Position(6, 6);
            board[6, 6] = new Mine(MineRadius.MineRadiusThree);
            board.Detonate(position);
            bool cellsAreDetonated =
                    board[4, 6].Exploded == board[5, 5].Exploded &&
                    board[5, 5].Exploded == board[5, 6].Exploded &&
                    board[5, 6].Exploded == board[6, 4].Exploded &&
                    board[6, 4].Exploded == board[6, 5].Exploded &&
                    board[6, 5].Exploded == board[6, 6].Exploded &&
                    board[6, 6].Exploded == true;

            Assert.IsTrue(cellsAreDetonated);
        }
Exemplo n.º 14
0
 public virtual void Detonate(Position position, IGameboard gameboard)
 {
     gameboard[position.X, position.Y].Explode();
     gameboard.MinesCount--;
 }