Ejemplo n.º 1
0
        [TestCase(new int[] { 88, 88, 79, 79, 79, 88, 88, 79, 88 })] // Draw
        public void AllCheckedPositions_NoWinCondition_ShouldReturn_DrawCondition(int[] drawConditionNumbers)
        {
            //Arrange

            var expectedBoardState          = 0; // Draw Condition / All input ('X' or 'O') checked positions
            var expectedAllCheckedPositions = true;

            //Act

            //* ASCII 79 ==> 'O', ASCII 88 ==> 'X'

            //Base case 3 x 3

            // ---------------------------------------------
            //                  TEST CASE 1

            // 1 2 3      ====>     X X O     ====>     Draw
            // 4 5 6                O O X
            // 7 8 9                X O X

            // ---------------------------------------------

            var boardState =
                _ticTacToeService.CheckTicTacToeBoardState(drawConditionNumbers, _fixedNumberOfRowsAndColumns);

            var allCheckedPositions = _ticTacToeService.CheckAllTicTacToePositionsChecked(drawConditionNumbers);

            //Assert

            Assert.AreEqual(expectedBoardState, boardState);
            Assert.AreEqual(expectedAllCheckedPositions, allCheckedPositions);
        }
Ejemplo n.º 2
0
        private static void InitializeTicTacToe(int numberOfRowsAndColumns)
        {
            //Console message printing indicating the initialization of the board
            Console.WriteLine("--------------------------------------------");
            Console.WriteLine("Initial state of the board:");

            //Get initial state of the tic-tac-toe board
            var board = _ticTacToeService.GetTicTacToeBoard(numberOfRowsAndColumns);

            //Print current tic-tac-toe board
            _ticTacToeService.PrintCurrentTicTacToeBoard(board, numberOfRowsAndColumns);

            Console.WriteLine();
            Console.WriteLine();

            //Loop that keeps track of selections by the player 1 and 2, state of the board, and validity of inputs.
            do
            {
                //Check for draw
                if (_ticTacToeService.CheckAllTicTacToePositionsChecked(board))
                {
                    Console.WriteLine("It is a draw!!");
                    Console.WriteLine("--------------------------------------------");
                    break;
                }

                //Let user know which player is currently on turn
                Console.WriteLine("Player 1: X and CPU: O");
                Console.WriteLine("\n");

                Console.WriteLine(player % 2 == 0 ? "CPU Chance" : "Player 1 Chance");
                Console.WriteLine("\n");

                bool isValid;

                if (player % 2 == 0)
                {
                    Console.WriteLine("CPU is thinking...");
                    Console.WriteLine();
                    choice = _ticTacToeRandomService.GenerateNextBestPossibleMove(board, numberOfRowsAndColumns);
                    Thread.Sleep(1000);
                    Console.WriteLine($"CPU has chosen {choice}...");
                    Thread.Sleep(1000);
                    Console.WriteLine();
                    isValid = true;
                }

                else
                {
                    isValid = int.TryParse(Console.ReadLine(), out choice);
                }

                //Check valid input, if not valid, print error message and indicate user to enter valid input as presented
                if (!isValid | _ticTacToeService.CheckTicTacToeValidInput(choice, numberOfRowsAndColumns))
                {
                    _ticTacToeService.PrintTicTacToeInvalidInputMessage(board, numberOfRowsAndColumns);
                    continue;
                }

                //Check if input has already been entered/checked, if not, enter it, else, indicate user that selection has been entered already.
                if (board[choice - 1] != 'X' && board[choice - 1] != 'O')
                {
                    if (player % 2 == 0)
                    {
                        board[choice - 1] = 'O';
                        player++;
                    }

                    else
                    {
                        board[choice - 1] = 'X';
                        player++;
                    }
                }

                else
                {
                    Console.WriteLine($"Sorry the position {choice} is already marked ");
                    Console.WriteLine("\n");
                }

                //Print current state of the board
                _ticTacToeService.PrintCurrentTicTacToeBoard(board, numberOfRowsAndColumns);

                Console.WriteLine();
                Console.WriteLine();

                //Keep looping until win condition is found or draw condition is found
            } while (_ticTacToeService.CheckTicTacToeBoardState(board, numberOfRowsAndColumns) != 1);

            Console.WriteLine();

            //Win condition found print winner
            if (_ticTacToeService.CheckTicTacToeBoardState(board, numberOfRowsAndColumns) == 1)
            {
                Console.WriteLine(player % 2 == 0 ? "Player 1 Wins!!" : "CPU Wins!!");
                Console.WriteLine();

                _ticTacToeService.PrintCurrentTicTacToeBoard(board, numberOfRowsAndColumns);

                Console.WriteLine();
                Console.WriteLine("--------------------------------------------");
            }

            //Indicate user to play again
            //If player 1 wins, player 2 starts next game and viceversa, if a choice of replay game is entered.
            //If game ends in draw, player to start is the one that did not go last in the last game, if a choice of replay is entered .

            Console.WriteLine();
            Console.WriteLine("Would you like to play again? (Y/N)");
            Console.WriteLine();

            var playAgainOption = Console.ReadLine();

            if (playAgainOption?.ToLower() == "y")
            {
                Console.WriteLine();
                InitializeTicTacToe(numberOfRowsAndColumns);
                Console.WriteLine();
            }
        }