Example #1
0
        public void TestIdentifyBomb()
        {
            var field = MineSweeperFactory.CreateMineField(40, 40, 20);

            for (int i = 0; i < 40; ++i)
            {
                for (int j = 0; j < 40; ++j)
                {
                    try
                    {
                        var n = field[i, j];

                        // since we don't get an exception no bomb is on this location
                        int bombsThusFar = field.IdentifyBomb(i, j);
                    }
                    catch (BombExplodedException)
                    {
                    }
                    catch (NoBombException ex)
                    {
                        Assert.IsTrue(ex.GetType() == typeof(NoBombException));
                    }
                }
            }
        }
Example #2
0
        public void TestFactory()
        {
            var field = MineSweeperFactory.CreateMineField(16, 16, 40);

            Assert.IsNotNull(field);
            Assert.AreEqual(16, field.Width);
            Assert.AreEqual(16, field.Height);
            Assert.AreEqual(40, field.NumberOfBombs);
        }
Example #3
0
        public void TestFindEmptyField()
        {
            var minesweeperfields = MineSweeperFactory.CreateMineField(40, 40, 200);

            var random = new Random();

            bool emptyFieldFound = false;

            while (!emptyFieldFound)
            {
                try
                {
                    var fields =
                        minesweeperfields[
                            random.Next(0, minesweeperfields.Width), random.Next(0, minesweeperfields.Height)];

                    if (fields[0].NumberOfBombs == 0)
                    {
                        emptyFieldFound = true;
                    }
                    else
                    {
                        continue;
                    }

                    foreach (var field in fields)
                    {
                        System.Diagnostics.Debug.WriteLine("Field in list <{0},{1},{2}>", field.X, field.Y,
                                                           field.NumberOfBombs);
                    }

                    Assert.IsTrue(true);
                }
                catch (BombExplodedException ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
        }
Example #4
0
        public void TestBoundaries()
        {
            try
            {
                MineSweeperFactory.CreateMineField(5, 10, 20);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.GetType() == typeof(TooSmallMineFieldException));
            }

            try
            {
                MineSweeperFactory.CreateMineField(1000, 10, 20);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.GetType() == typeof(TooSmallMineFieldException));
            }

            try
            {
                MineSweeperFactory.CreateMineField(1001, 10, 20);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.GetType() == typeof(TooBigMineFieldException));
            }

            try
            {
                MineSweeperFactory.CreateMineField(40, 40, 800);
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.GetType() == typeof(TooManyBombsException));
            }
        }
Example #5
0
        public void TestCountBombs()
        {
            var field = MineSweeperFactory.CreateMineField(10, 10, 10);

            int numberOfBombs = 0;

            for (int i = 0; i < 10; ++i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    try
                    {
                        var results = field[i, j];
                    }
                    catch (Exception)
                    {
                        numberOfBombs++;
                    }
                }
            }

            Assert.AreEqual(10, numberOfBombs);
        }
Example #6
0
        public void TestValidateAdjacentBombs()
        {
            var field  = MineSweeperFactory.CreateMineField(40, 40, 500);
            var random = new Random();

            bool emptyFieldFound = false;

            int numberOfBombs          = 0;
            int numberOfBombsToBeFound = 0;
            int c = 15;

            while (!emptyFieldFound)
            {
                try
                {
                    c = random.Next(3, 18);

                    var result = field[c, c];
                    numberOfBombsToBeFound = result[0].NumberOfBombs;
                    emptyFieldFound        = true;
                }
                catch (BombExplodedException)
                {
                }
            }

            numberOfBombs += CheckPosition(field, c - 1, c - 1);
            numberOfBombs += CheckPosition(field, c - 1, c);
            numberOfBombs += CheckPosition(field, c - 1, c + 1);
            numberOfBombs += CheckPosition(field, c, c - 1);
            numberOfBombs += CheckPosition(field, c, c + 1);
            numberOfBombs += CheckPosition(field, c + 1, c - 1);
            numberOfBombs += CheckPosition(field, c + 1, c);
            numberOfBombs += CheckPosition(field, c + 1, c + 1);

            Assert.AreEqual(numberOfBombsToBeFound, numberOfBombs);
        }