Exemplo n.º 1
0
        public void TestConstructor_IsMatrixWithSpecifiedRowsCount()
        {
            Field gameField = new Field();
            int rowsCountNeeded = 5;

            Assert.AreEqual(rowsCountNeeded, gameField.Matrix.GetLength(0));
        }
Exemplo n.º 2
0
        public void TestConstructor_IsMatrixWithSpecifiedColsCount()
        {
            Field gameField = new Field();
            int colsCountNeeded = 10;

            Assert.AreEqual(colsCountNeeded, gameField.Matrix.GetLength(1));
        }
Exemplo n.º 3
0
        public void TestColsProperty_GetColsValue()
        {
            Field gameField = new Field();
            int colsCount = 10;

            Assert.AreEqual(colsCount, gameField.Cols);
        }
Exemplo n.º 4
0
        public void TestFillWithRandomMines_AreMinesOnRandomPositionsEachTime()
        {
            Field gameField = new Field();

            gameField.FillWithRandomMines();
            string[,] firstMatrix = gameField.Matrix;

            gameField.Initialize();
            gameField.FillWithRandomMines();
            string[,] secondMatrix = gameField.Matrix;

            bool areDifferent = false;
            for (int i = 0; i < gameField.Rows; i++)
            {
                for (int j = 0; j < gameField.Cols; j++)
                {
                    if (firstMatrix[i, j] != secondMatrix[i, j])
                    {
                        areDifferent = true;
                    }
                }
            }

            Assert.AreEqual(true, areDifferent);
        }
        /// <summary>
        /// Creates a new playing field which the player interacts with.
        /// </summary>
        /// <returns>An instance of IField</returns>
        public override IField CreateField()
        {
            IField field = new Field(FieldRows, FieldCols);
            IGenerator fieldPopulator = new GeneralFieldPopulator(field);
            IGeneratable cellRepresentor = new CellRepresentor('?');

            fieldPopulator.Generate(cellRepresentor, field.Rows * field.Columns);

            return field;
        }
        public void TestRevealNumberWithEightBombs()
        {
            // Create a Test Field
            int testRow = 5;
            int testCall = 10;
            int mines = 15;
            string[,] testMineFiled = new string[testRow, testCall];

            IField mineFieldTest = new Minesweeper.Field(testRow, testCall, mines);

            mineFieldTest.GetDefaultField();

            // bombs All Around
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    testMineFiled[i, j] += "*";
                }
            }

            testMineFiled[1, 1] += string.Empty;

            int revialedCells = 0;
            string expectedRevealNumber = "8";

            mineFieldTest.RevialCell(1, 1);

            if (testMineFiled[1, 1] == string.Empty)
            {
                int[] directionRow = { 1, 1, 1, 0, -1, -1, -1, 0 };
                int[] directionCol = { 1, 0, -1, -1, -1, 0, 1, 1 };
                int minesCounterRevealNumber = 0;

                for (int direction = 0; direction < 8; direction++)
                {
                    int newRow = directionRow[direction] + 1;
                    int newCol = directionCol[direction] + 0;
                    if (mineFieldTest.IsMoveInBounds(newRow, newCol))
                    {
                        if (testMineFiled[newRow, newCol] == "*")
                        {
                            minesCounterRevealNumber++;
                        }
                    }
                }

                testMineFiled[1, 0] = Convert.ToString(minesCounterRevealNumber);
                string result = testMineFiled[1, 0];
                revialedCells++;
                Assert.AreEqual(expectedRevealNumber, result);
            }
        }
        /// <summary>
        /// Creates a new minesweeper field and populates it with mines.
        /// </summary>
        /// <returns>An instance of IField</returns>
        public override IField CreateField()
        {
            IField field = new Field(FieldRows, FieldCols);
            IGenerator mineGenerator = new MineGenerator(field);
            IGeneratable mine = new Mine('*');
            IAdjacencyMap mineMap = new MinesweeperAdjacencyMap(field, mine);

            mineGenerator.Generate(mine, FieldRows + FieldCols);
            mineMap.CreateNeighboursMap();

            return field;
        }
        public string Format(Field field)
        {
            var result = "";
            for (var row = 0; row < field.Height; row++)
            {
                result += Environment.NewLine;

                for (var col = 0; col < field.Width; col++)
                {
                    var cell = field[row, col];
                    result += cell.IsMine ? "*" : cell.MineCount.ToString();
                }
            }

            return result.Trim();
        }
        public void TestGetDefaultFieldMethodNegative()
        {
            int row = 5;
            int call = 10;
            int mines = 15;
            string[,] testMineFiled = new string[row, call];
            string emptySymbol = " ";
            IField mineFieldTest = new Minesweeper.Field(row, call, mines);
            mineFieldTest.GetDefaultField();

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < call; j++)
                {
                    testMineFiled[i, j] = string.Empty;
                    Assert.AreNotEqual(emptySymbol, testMineFiled[i, j]);
                }
            }
        }
Exemplo n.º 10
0
        public void TestConstructor_IsMatrixInitialized()
        {
            Field gameField = new Field();

            bool isEmpty = true;

            for (int i = 0; i < gameField.Matrix.GetLength(0); i++)
            {
                for (int j = 0; j < gameField.Matrix.GetLength(1); j++)
                {
                    if (gameField.Matrix[i, j] != string.Empty)
                    {
                        isEmpty = false;
                        break;
                    }
                }
            }

            Assert.AreEqual(true, isEmpty);
        }
Exemplo n.º 11
0
        public void TestSymbolsWithoutMines()
        {
            Field mineFieldTest = new Minesweeper.Field(5, 10, 15);
            mineFieldTest.Initialize();
            Cell[,] testMineFiled = mineFieldTest.MineField;

            string expectedstarSymbol = string.Empty;
            int counter = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (testMineFiled[i, j].Value == expectedstarSymbol)
                    {
                        counter++;
                    }
                }
            }

            Assert.AreEqual(35, counter);
        }
Exemplo n.º 12
0
        public void TestNegativeEmprySymbol()
        {
            Field mineFieldTest = new Minesweeper.Field(5, 10, 15);
            mineFieldTest.Initialize();
            Cell[,] testMineFiled = mineFieldTest.MineField;

            string expectedEmptySymbol = string.Empty;
            string expectedstarSymbol = "*";
            int counter = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if ((testMineFiled[i, j].Value != expectedstarSymbol) &&
                        (testMineFiled[i, j].Value != expectedEmptySymbol))
                    {
                        counter++;
                    }
                }
            }

            Assert.AreEqual(0, counter);
        }
Exemplo n.º 13
0
        public void TestRowsProperty_GetRowsValue()
        {
            Field gameField = new Field();
            int rowsCount = 5;

            Assert.AreEqual(rowsCount, gameField.Rows);
        }
Exemplo n.º 14
0
        private void onRightClick(Field button)
        {
            if (marked == false)
            {
                button.BackColor = Color.OrangeRed;
                marked = true;
                frm_Main.Instance().Spielfeld.Markings++;
                frm_Main.Instance().setLabelMarkText(frm_Main.Instance().MarkingsCounter);
            }

            else
            {
                button.BackColor = Color.Gray;
                marked = false;
                frm_Main.Instance().Spielfeld.Markings--;
                frm_Main.Instance().setLabelMarkText(frm_Main.Instance().MarkingsCounter);
            }
            checkWin();
        }
Exemplo n.º 15
0
 public void SetGame(int height, int width, int mines)
 {
     field = new Field(height, width, mines);
     flags = Mines;
     gameState = GameState.NotPlaying;
     time = 0;
     totalTime = 0.0;
     faceValue = Face.Happy;
     selected = new FieldLocation(0, 0);
     corner = new FieldLocation(0, 0);
     faceSelected = false;
 }
Exemplo n.º 16
0
        public void TestInitialize_IsMatrixInitialized()
        {
            Field gameField = new Field();

            gameField.FillWithRandomMines();
            gameField.Initialize();

            bool isInitialized = true;

            for (int i = 0; i < gameField.Rows; i++)
            {
                for (int j = 0; j < gameField.Cols; j++)
                {
                    if (gameField.Matrix[i, j] != string.Empty)
                    {
                        isInitialized = false;
                        break;
                    }
                }
            }

            Assert.AreEqual(true, isInitialized);
        }
Exemplo n.º 17
0
        public void TestFillWithRandomMines_AreMinesWithSpecifiedCount()
        {
            Field gameField = new Field();
            gameField.FillWithRandomMines();

            int currentMinesCount = 0;
            string mine = "*";

            for (int i = 0; i < gameField.Rows; i++)
            {
                for (int j = 0; j < gameField.Cols; j++)
                {
                    if (gameField.Matrix[i, j] == mine)
                    {
                        currentMinesCount++;
                    }
                }
            }

            int minesCountNeeded = 15;
            Assert.AreEqual(minesCountNeeded, currentMinesCount);
        }
Exemplo n.º 18
0
        public void TestIsMine_InvalidRowAndCol()
        {
            int cellRow = 13;
            int cellCol = -2;

            Field gameField = new Field();
            gameField.FillWithRandomMines();

            bool isMine = gameField.IsMine(cellRow, cellCol);
        }
Exemplo n.º 19
0
        public void TestIsMine_CellHasMine()
        {
            Random fixedRnd = new Random(5);

            int cellRow = fixedRnd.Next(0, 5);
            int cellCol = fixedRnd.Next(0, 10);

            Field gameField = new Field();
            Type fieldType = typeof(Field);

            fixedRnd = new Random(5);

            var fieldRandom = fieldType.GetField("random", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldRandom.SetValue(gameField, fixedRnd);

            gameField.FillWithRandomMines();

            bool isMine = gameField.IsMine(cellRow, cellCol);

            Assert.AreEqual(true, isMine);
        }
Exemplo n.º 20
0
        public void TestIsMine_InvalidRowLessThanMin()
        {
            int cellRow = -1;
            int cellCol = 5;

            Field gameField = new Field();
            gameField.FillWithRandomMines();

            bool isMine = gameField.IsMine(cellRow, cellCol);
        }
Exemplo n.º 21
0
        public void TestIsMine_InvalidRowGreaterThanMax()
        {
            int cellRow = 7;
            int cellCol = 5;

            Field gameField = new Field();
            gameField.FillWithRandomMines();

            bool isMine = gameField.IsMine(cellRow, cellCol);
        }
Exemplo n.º 22
0
        public void TestUpdate_IsUpdateCorrectSecondCheck()
        {
            Random fixedRnd = new Random(5);
            Field gameField = new Field();
            Type fieldType = typeof(Field);

            var fieldRandom = fieldType.GetField("random", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldRandom.SetValue(gameField, fixedRnd);
            gameField.FillWithRandomMines();

            int cellRow = 3;
            int cellCol = 1;
            gameField.Update(cellCol, cellCol);

            int adjacentMinesCount = 5;
            bool isUpdatedCorrectly = gameField.Matrix[cellRow, cellCol] != adjacentMinesCount.ToString();

            Assert.AreEqual(true, isUpdatedCorrectly);
        }
Exemplo n.º 23
0
        private void onLeftClick(Field button)
        {
            if (mine == true)
            {
                foreach (Field feld in frm_Main.Instance().Spielfeld.Spielfeld)
                {
                    if (feld.mine == true){
                        feld.Text = "M";
                        feld.BackColor = Color.Red;
                    }
                }
                MessageBox.Show("boom");
                gameOver();
            }

            else
            {
                if (button.suMines == 0)
                {
                    buttonRevealed(button);
                    foreach (Field feld in button.surroundings)
                    {
                        if (feld.check == false)
                        {
                            buttonRevealed(button);
                            onLeftClick(feld);
                        }
                    }
                }

                else
                {
                    buttonRevealed(button);
                }
            }
            checkWin();
        }
        public void TestRenderingValidField()
        {
            IField field = new Field(5, 10);

            string expected = field.ToString() + Environment.NewLine;

            using (StringWriter sw = new StringWriter())
            {
                Console.SetOut(sw);

                renderer.RenderGameField(field);

                Assert.AreEqual(expected, sw.ToString(), "The console renderer must invoke the toString() method of the field.");
            }
        }
Exemplo n.º 25
0
 public void TestConstructor_IsMatrixCreated()
 {
     Field gameField = new Field();
     Assert.AreEqual(true, gameField.Matrix != null);
 }
Exemplo n.º 26
0
        public void TestMatrixProperty_IsItEncapsulated()
        {
            Field gameField = new Field();

            string[,] matrixValue = gameField.Matrix;
            matrixValue[0, 1] = "test";

            bool areDifferent = false;

            for (int i = 0; i < gameField.Rows; i++)
            {
                for (int j = 0; j < gameField.Cols; j++)
                {
                    if (gameField.Matrix[i, j] != matrixValue[i, j])
                    {
                        areDifferent = true;
                        break;
                    }
                }
            }

            Assert.AreEqual(true, areDifferent);
        }
 public void TestPrintGameFieldNoBoomed()
 {
     using (ConsoleRedirector cr = new ConsoleRedirector())
     {
         Field gameField = new Field();
         bool hasBoomed = false;
         string scoreBoard = @"     0 1 2 3 4 5 6 7 8 9
        ---------------------
     0 |  ? ? ? ? ? ? ? ? ? ?|
     1 |  ? ? ? ? ? ? ? ? ? ?|
     2 |  ? ? ? ? ? ? ? ? ? ?|
     3 |  ? ? ? ? ? ? ? ? ? ?|
     4 |  ? ? ? ? ? ? ? ? ? ?|
        --------------------";
         Assert.IsFalse(cr.ToString().Contains(scoreBoard));
         ioManager.PrintGameField(gameField, hasBoomed);
         Assert.IsTrue(cr.ToString().Contains(scoreBoard));
     }
 }
Exemplo n.º 28
0
        public void TestUpdate_IsMatrixUpdated()
        {
            Random fixedRnd = new Random(5);

            int cellRow = fixedRnd.Next(0, 5) + 1;
            int cellCol = fixedRnd.Next(0, 10) + 1;

            Field gameField = new Field();
            Type fieldType = typeof(Field);

            fixedRnd = new Random(5);

            var fieldRandom = fieldType.GetField("random", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldRandom.SetValue(gameField, fixedRnd);

            gameField.FillWithRandomMines();
            gameField.Update(cellRow, cellCol);
            bool isUpdated = gameField.Matrix[cellRow, cellCol] != string.Empty && gameField.Matrix[cellRow, cellCol] != "*";

            Assert.AreEqual(true, isUpdated);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Controls the gameplay throughout other methods for validation and I/O.
        /// </summary>      
        public void Play()
        {
            IScoreBoard scoreBoard = new ScoreBoard();
            IField gameField = new Field();
            IIOManager ioManager = new ConsoleIOManager();
            int row = 0;
            int col = 0;
            int minesCounter = 0;
            int revealedCellsCounter = 0;
            bool hasExploded = false;

            while (true)
            {
                ioManager.PrintInitialMessage();
                StartNewGame(gameField, ref row, ref col, ref minesCounter, ref revealedCellsCounter, ref hasExploded);

                ioManager.PrintGameField(gameField, hasExploded);

                bool rowAndColAreValid = false;
                string input = string.Empty;

                while (true)
                {
                    input = ioManager.GetUserInput();

                    if (IsMoveEntered(input))
                    {
                        string[] inputParams = input.Split();
                        row = int.Parse(inputParams[0]);
                        col = int.Parse(inputParams[1]);

                        rowAndColAreValid = ValidateRowAndCol(gameField, row, col);

                        if (rowAndColAreValid)
                        {
                            bool cellHasMine = gameField.IsMine(row, col);

                            if (cellHasMine)
                            {
                                hasExploded = true;
                                ioManager.PrintGameField(gameField, hasExploded);
                                ioManager.PrintExplosionMessage(revealedCellsCounter);

                                string currentPlayerName = ioManager.GetUserNickname();
                                scoreBoard.AddPlayer(currentPlayerName, revealedCellsCounter);

                                ioManager.PrintScoreBoard(scoreBoard);
                                break;
                            }

                            bool winner = IsWinner(gameField.Matrix, minesCounter);

                            if (winner)
                            {
                                ioManager.PrintWinnerMessage();

                                string currentPlayerName = ioManager.GetUserNickname();
                                scoreBoard.AddPlayer(currentPlayerName, revealedCellsCounter);

                                break;
                            }

                            gameField.Update(row, col);
                            revealedCellsCounter++;

                            ioManager.PrintGameField(gameField, cellHasMine);
                        }
                        else
                        {
                            ioManager.PrintInvalidCommandMessage();
                        }
                    }
                    else if (IsCommandEntered(input))
                    {
                        bool isRestart = false;

                        switch (input)
                        {
                            case "top":
                                {
                                    ioManager.PrintScoreBoard(scoreBoard);
                                    continue;
                                }

                            case "exit":
                                {
                                    ioManager.PrintQuitMessage();
                                    return;
                                }

                            case "restart":
                                {
                                    isRestart = true;
                                    break;
                                }
                        }

                        if (isRestart)
                        {
                            break;
                        }
                    }
                    else
                    {
                        ioManager.PrintInvalidCommandMessage();
                    }
                }
            }
        }
Exemplo n.º 30
0
        private void buttonRevealed(Field button)
        {
            if (button.check == false)
            {
                frm_Main.Instance().Spielfeld.FieldsRevealed++;
            }

            if (button.marked == true)
            {
                button.marked = false;
                frm_Main.Instance().Spielfeld.Markings--;
                frm_Main.Instance().setLabelMarkText(frm_Main.Instance().MarkingsCounter);
            }

            button.Text = "" + button.suMines;
            button.BackColor = Color.White;
            button.Enabled = false;
            button.check = true;
        }