示例#1
0
        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;
                }
            }
        }
        public void TestCellIsDetonated_WithDetonatedCells()
        {
            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.DetonateMine(row, col);
                        Assert.IsTrue(testGame.CellIsDetonated(row, col));
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
        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;
                    }
                }
            }
        }