Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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();
                    }
                }
            }
        }
Ejemplo n.º 3
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);
        }