Example #1
0
        /// <summary>
        /// Prints "Invalid move!" every time invalid coordinates for a mine on 
        /// the field are inputted. Outputs the coordinates to mineRow, mineCol.
        /// </summary>
        private static void TryGetInputMineCoordinatesUntilValid(
            ConsoleBattleField game, out int mineRow, out int mineCol)
        {
            mineRow = -1;
            mineCol = -1;

            bool validInput = game.TryGetInputCoordinates(out mineRow, out mineCol);
            while (mineRow == -1 || !validInput || !game.CellIsMine(mineRow, mineCol))
            {
                Console.WriteLine("Invalid input!");
                validInput = game.TryGetInputCoordinates(out mineRow, out mineCol);
            }
        }
        public void TestCellIsDetonated_WithNoDetonatedCells()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);
            testGame.GenerateMines();

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    if (testGame.CellIsMine(row, col) || testGame.CellIsEmpty(row, col))
                    {
                        Assert.IsFalse(testGame.CellIsDetonated(row, col));
                    }
                }
            }
        }
        public void TestCellIsMine_WithMines()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);
            testGame.GenerateMines();
            var fieldCopy = testGame.GameFieldCopy;

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    if (fieldCopy[row, col] == FieldCell.Mine1 ||
                        fieldCopy[row, col] == FieldCell.Mine2 ||
                        fieldCopy[row, col] == FieldCell.Mine3 ||
                        fieldCopy[row, col] == FieldCell.Mine4 ||
                        fieldCopy[row, col] == FieldCell.Mine5)
                    {
                        Assert.IsTrue(testGame.CellIsMine(row, col));
                    }
                }
            }
        }
        public void TestMineCount()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);
            testGame.GenerateMines();

            int expectedCountMines = 0;

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    if (testGame.CellIsMine(row, col))
                    {
                        expectedCountMines++;
                    }
                }
            }

            Assert.AreEqual(expectedCountMines, testGame.MineCount);
        }
        public void TestGenerateMines_CountCorrectnessInTenCellField()
        {
            for (int i = 0; i < 20; i++)
            {
                ConsoleBattleField testGame = new ConsoleBattleField();
                testGame.InitializeGameField(10);
                testGame.GenerateMines();

                int expectedMinCountMines = 15; // >= 15% of the field
                int expectedMaxCountMines = 30; // <= 30% of the field

                int minesCount = 0;

                for (int row = 0; row < 10; row++)
                {
                    for (int col = 0; col < 10; col++)
                    {
                        if (testGame.CellIsMine(row, col))
                        {
                            minesCount++;
                        }
                    }
                }

                Assert.IsTrue(expectedMinCountMines <= minesCount &&
                    minesCount <= expectedMaxCountMines);
            }
        }
        public void TestGenerateMines_CountCorrectnessInFourCellField()
        {
            for (int i = 0; i < 20; i++)
            {
                ConsoleBattleField testGame = new ConsoleBattleField();
                testGame.InitializeGameField(2);
                testGame.GenerateMines();
                int expectedMinesCount = 1; // the only possible number of mines in 2x2 field
                // for the default 15%-30% of all field blocks

                int minesCount = 0;

                for (int row = 0; row < 2; row++)
                {
                    for (int col = 0; col < 2; col++)
                    {
                        if (testGame.CellIsMine(row, col))
                        {
                            minesCount++;
                        }
                    }
                }

                Assert.IsTrue(expectedMinesCount == minesCount);
            }
        }
        public void TestGenerateMines_CountCorrectnessInFieldLengthOfSix()
        {
            for (int i = 0; i < 20; i++)
            {
                ConsoleBattleField testGame = new ConsoleBattleField();
                testGame.InitializeGameField(6);
                testGame.GenerateMines();

                int expectedMinCountMines = 5;
                int expectedMaxCountMines = 11;

                int minesCount = 0;

                for (int row = 0; row < 6; row++)
                {
                    for (int col = 0; col < 6; col++)
                    {
                        if (testGame.CellIsMine(row, col))
                        {
                            minesCount++;
                        }
                    }
                }

                Assert.IsTrue(expectedMinCountMines <= minesCount &&
                    minesCount <= expectedMaxCountMines);
            }
        }
        public void TestDetonateMine_WithNoMine()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);
            testGame.GenerateMines();
            var fieldCopy = testGame.GameFieldCopy;

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    if (!testGame.CellIsMine(row, col))
                    {
                        testGame.DetonateMine(row, col);
                    }
                }
            }
        }