コード例 #1
0
ファイル: Game.cs プロジェクト: velidar/HQC-Project-Team-Neon
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the \"Battle Field\" game.");

            ConsoleBattleField game = new ConsoleBattleField();
            int fieldSize = TryGetInputFieldSizeUntilValid(game);

            game.InitializeGameField(fieldSize);
            game.GenerateMines();

            while (true)
            {
                Console.WriteLine(String.Empty + game.GetBattleFieldAsString());

                int mineRow, mineCol;
                TryGetInputMineCoordinatesUntilValid(game, out mineRow, out mineCol);

                game.DetonateMine(mineRow, mineCol);
                if (game.MineCount == 0)
                {
                    Console.WriteLine(game.GetBattleFieldAsString());
                    Console.WriteLine("{0}Game over! {0}Detonated mines: {1}",
                        Environment.NewLine, game.DetonatedMinesCount);
                    break;
                }
            }
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: velidar/HQC-Project-Team-Neon
        /// <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);
            }
        }
コード例 #3
0
ファイル: Game.cs プロジェクト: velidar/HQC-Project-Team-Neon
        private static int TryGetInputFieldSizeUntilValid(ConsoleBattleField game)
        {
            int fieldSize = -1;
            bool validSize = false;
            while (!validSize)
            {
                Console.Write("Enter the size of the battle field: [{0}..{1}]: ",
                    game.MinFieldSize, game.MaxFieldSize);
                validSize = game.TryGetInputFieldSize(out fieldSize);
            }

            Console.WriteLine();
            return fieldSize;
        }
コード例 #4
0
        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));
                    }
                }
            }
        }
コード例 #5
0
        public void TestCellIsEmpty_WithNoEmptyCells()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);
            var fieldCopy = testGame.GameFieldCopy;

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 10; col++)
                {
                    if (FieldCell.EmptyCell != fieldCopy[row, col])
                    {
                        Assert.IsFalse(testGame.CellIsEmpty(row, col));
                    }
                }
            }
        }
コード例 #6
0
        public void TestGetInputFieldSize_OutOfRangeSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();

            using (var reader = new StringReader("125"))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsFalse(validFieldSize);
            }
        }
コード例 #7
0
        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));
                    }
                }
            }
        }
コード例 #8
0
        public void TestTryGetInputCoordinates_ValidCoordinates()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(5);
            int mineRow = 2,
                mineCol = 2;

            using (var reader = new StringReader(String.Format("{0} {1}", mineRow, mineCol)))
            {
                Console.SetIn(reader);
                var validCoords = testGame.TryGetInputCoordinates(out mineRow, out mineCol);
                Assert.IsTrue(validCoords);
            }
        }
コード例 #9
0
        public void TestTryGetInputCoordinates_GibberishCoords()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(5);
            int mineRow = -1,
                mineCol = -1;

            using (var reader = new StringReader("3 dsafgf"))
            {
                Console.SetIn(reader);
                var validCoords = testGame.TryGetInputCoordinates(out mineRow, out mineCol);
                Assert.IsFalse(validCoords);
            }
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        public void TestInitializeGameField_WithTenCells()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);

            FieldCell cell = FieldCell.EmptyCell;
            FieldCell[,] expectedGameField =
            {
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell },
                { cell, cell, cell, cell, cell, cell, cell, cell, cell, cell }
            };

            CollectionAssert.AreEqual(expectedGameField, testGame.GameFieldCopy);
        }
コード例 #12
0
        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);
                    }
                }
            }
        }
コード例 #13
0
        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);
            }
        }
コード例 #14
0
        public void TestGetInputFieldSize_GibberishSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();

            using (var reader = new StringReader("4fsjdasdi"))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsFalse(validFieldSize);
            }
        }
コード例 #15
0
        public void TestGetInputFieldSize_AcceptableSize()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            int validSize = 6;

            using (var reader = new StringReader(validSize.ToString()))
            {
                Console.SetIn(reader);

                int fieldSize;
                bool validFieldSize = testGame.TryGetInputFieldSize(out fieldSize);
                Assert.IsTrue(validFieldSize);
            }
        }
コード例 #16
0
        public void TestGetBattleFieldAsString()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(10);

            StringBuilder expectedPrintedField = new StringBuilder();

            expectedPrintedField.AppendLine("   0  1  2  3  4  5  6  7  8  9  ");
            expectedPrintedField.AppendLine("   ----------------------------");
            expectedPrintedField.AppendLine("0| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("1| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("2| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("3| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("4| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("5| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("6| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("7| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("8| -  -  -  -  -  -  -  -  -  - ");
            expectedPrintedField.AppendLine("9| -  -  -  -  -  -  -  -  -  - ");

            Assert.AreEqual(expectedPrintedField.ToString(),
                testGame.GetBattleFieldAsString());
        }
コード例 #17
0
        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);
            }
        }
コード例 #18
0
        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);
            }
        }
コード例 #19
0
        public void TestInitializeGameFieldWithOneCell()
        {
            ConsoleBattleField testGame = new ConsoleBattleField();
            testGame.InitializeGameField(1);

            FieldCell[,] expectedGameField = { { FieldCell.EmptyCell } };

            CollectionAssert.AreEqual(expectedGameField, testGame.GameFieldCopy);
        }
コード例 #20
0
        public void TestDetonateMine_WithMine1()
        {
            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 (FieldCell.Mine1 == fieldCopy[row, col])
                    {
                        testGame.DetonateMine(row, col);
                        Assert.IsTrue(testGame.CellIsDetonated(row, col));
                        if (row > 0 && col > 0)
                        {
                            Assert.IsTrue(testGame.CellIsDetonated(row - 1, col - 1));
                        }
                        if (row < 9 && col < 9)
                        {
                            Assert.IsTrue(testGame.CellIsDetonated(row + 1, col + 1));
                        }
                        if (row > 0 && col < 9)
                        {
                            Assert.IsTrue(testGame.CellIsDetonated(row - 1, col + 1));
                        }
                        if (row < 9 && col > 0)
                        {
                            Assert.IsTrue(testGame.CellIsDetonated(row + 1, col - 1));
                        }

                        return;
                    }
                }
            }
        }