[TestCase(new int[] { 1, 2, 3, 4, 5, 6, 79, 79, 79 })] // Player 2 'O' Wins third row public void AllSameCheckedRows_ShouldReturn_WinCondition(int[] rowWinningNumbers) { //Arrange var expectedBoardState = 1; // Win Condition //* ASCII 79 ==> 'O', ASCII 88 ==> 'X' //Player 1: X and Player 2: O //Base case 3 x 3 // --------------------------------------------- // TEST CASE 1 // 1 2 3 ====> X X X ====> Win // 4 5 6 - - - // 7 8 9 - - - // --------------------------------------------- // TEST CASE 2 // 1 2 3 - - - // 4 5 6 ====> X X X ====> Win // 7 8 9 - - - // --------------------------------------------- // TEST CASE 3 // 1 2 3 - - - // 4 5 6 ====> - - - // 7 8 9 X X X ====> Win // --------------------------------------------- // TEST CASE 4 // 1 2 3 ====> O O O ====> Win // 4 5 6 - - - // 7 8 9 - - - // --------------------------------------------- // TEST CASE 5 // 1 2 3 - - - // 4 5 6 ====> O O O ====> Win // 7 8 9 - - - // --------------------------------------------- // TEST CASE 6 // 1 2 3 - - - // 4 5 6 ====> - - - // 7 8 9 O O O ====> Win // --------------------------------------------- var boardState = _ticTacToeService.CheckTicTacToeBoardState(rowWinningNumbers, _fixedNumberOfRowsAndColumns); //Assert Assert.AreEqual(expectedBoardState, boardState); }
public Tuple <int, bool> CheckTicTacToeWinningCondition(int[] board, List <int> moves, int numberOfRowsAndColumns) { foreach (var move in moves.Where(x => x != 0)) { var valuePlaced = board[move - 1]; board[move - 1] = move; var winCondition = _ticTacToeService.CheckTicTacToeBoardState(board, numberOfRowsAndColumns); if (winCondition == 1) { return(new Tuple <int, bool>(move, true)); } board[move - 1] = valuePlaced; } return(null); }
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(); } }