/// <summary> /// Cpus the move. /// </summary> /// <param name="boardPositions">The board positions.</param> /// <param name="choosingCharacter">The choosing character.</param> /// <param name="index">The index which helps in choosing character from array for cpu</param> public static void CpuMove(char[] boardPositions, char[] choosingCharacter, int index) { // while loop moves until position is entered by cpu in array and displayed in board. while (true) { int positionEnteredByCpu; int positionForWinning = CpuMoveForWinning(boardPositions, choosingCharacter); //if position for winning is 0, then their is no position at which cpu can win or block position for win of user. if (positionForWinning == 0) { //calls the method to enter character at corner position, at middle and if no case is satisfied, randomly - by cpu. positionEnteredByCpu = TicTacToe.FillingVoidPositionByCpu(boardPositions); } //calls the method and returns the value where either the cpu is winning or blocking position for winning of user. else { positionEnteredByCpu = positionForWinning; } //adding character in array using position returned from methods and displaying in boards. if (boardPositions[positionEnteredByCpu] == ' ') { boardPositions[positionEnteredByCpu] = choosingCharacter[index % 2]; TicTacToe.Board(boardPositions); break; } } }
/// <summary> /// Defines the entry point of the application. /// </summary> static void Main() { bool flag = true; while (flag) { Console.WriteLine("Welocome to TicTacToe Game"); //Toss to decide, who will move, either computer or user. int tossResult = TicTacToe.TossForGame(); //Array created for board positions char[] boardPositions = TicTacToe.CreatingBoard(); //choosing character for player char[] charactersArray = TicTacToe.ChoosingCharacter(); Console.WriteLine($"you will play with :{charactersArray[0]}"); Console.WriteLine($"CPU will play with :{charactersArray[1]}"); //displaying the board. TicTacToe.Board(boardPositions); //filling the values TicTacToe.MarkingPositions(boardPositions, charactersArray, tossResult); Console.WriteLine("Do you want to play again, press enter to Exit, Y to continue"); string inputForRematch = Console.ReadLine(); if (inputForRematch == "") { flag = false; } else if (inputForRematch.ToLower() == "y") { flag = true; } else { break; } } }
/// <summary> /// Adding the positions in array filled by user and cpu and displaying it on board. /// </summary> /// <param name="boardPositions">The board positions.</param> /// <param name="choosingCharacter">The choosing character.</param> /// <param name="tossResult">The toss result.</param> public static void MarkingPositions(char[] boardPositions, char[] choosingCharacter, int tossResult) { int checkForWin; int index = tossResult; //do while loop moves until game is drawn or any player wins. do { //switches input values between cpu and player one by one. Index value starts from toss result. switch (index % 2) { //input by user. case 0: { Console.WriteLine("Please enter the position between 1 to 9 where you want to fill your character"); while (true) { int positionEnteredByUser = Convert.ToInt32(Console.ReadLine()); if (positionEnteredByUser <= 9 && positionEnteredByUser >= 1) { //if position in array and board is vacant, character is added. if (boardPositions[positionEnteredByUser] == ' ') { boardPositions[positionEnteredByUser] = choosingCharacter[index % 2]; TicTacToe.Board(boardPositions); break; } else { Console.WriteLine("The position is already occupied, please enter position again"); } } else { Console.WriteLine("Please enter the correct position to fill"); } } break; } // input done by user. case 1: { //calling CpuMove method which processes input by cpu and enters in array and displays it on board. TicTacToe.CpuMove(boardPositions, choosingCharacter, index); break; } default: break; } // calling CheckingForWinning method to see if game is ending or not. checkForWin = TicTacToe.CheckingForWinning(boardPositions); //if checkForWin ==0, one of the player has win the game. if (checkForWin == 0) { if (index % 2 == 0) { Console.WriteLine("You have won the game"); } else { Console.WriteLine("Cpu has won the game"); } break; } index++; //if checkforWin= -1, than game is neither draw nor won by any player, positions are vacant in board. } while (checkForWin == -1); }