Question: #include #include using namespace std; int checkwin(char square[]); void board(string player1Name, string player2Name, char square[]); bool validateUseInput(char input[]); bool validatePlayerName(string playerName); int main() { char

#include #include

using namespace std; int checkwin(char square[]); void board(string player1Name, string player2Name, char square[]); bool validateUseInput(char input[]); bool validatePlayerName(string playerName); int main() { char userChoice; do { int player = 1, i, choice; char square[9] = { '0','1','2','3','4','5','6','7','8' }; string player1Name; string player2Name;

cout << "Enter Player 1 Name: "; while (true) { cin >> player1Name; bool valid = validatePlayerName(player1Name); if (valid) break; else { cout << "--Invalid Player Name , Must be characters only -- "; cout << "--Please Re-Enter-- "; } } cout << "Enter Player 2 Name: ";

while (true) { cin >> player2Name; bool valid = validatePlayerName(player2Name); if (valid) break; else { cout << "--Invalid Player Name , Must be characters only -- "; cout << "--Please Re-Enter-- "; }

}

char mark; do { board(player1Name, player2Name, square); player = (player % 2) ? 1 : 2;

if (player == 1) cout << "Player " << player1Name << ", enter a number: "; else if (player == 2) cout << "Player " << player2Name << ", enter a number: "; char input[10]; while (true) { cin >> input; bool valid = validateUseInput(input); if (valid == true) break; } choice = input[0] - 48;

mark = (player == 1) ? 'X' : 'O';

if (choice == 0 && square[0] == '0')

square[0] = mark; else if (choice == 1 && square[1] == '1')

square[1] = mark; else if (choice == 2 && square[2] == '2')

square[2] = mark; else if (choice == 3 && square[3] == '3')

square[3] = mark; else if (choice == 4 && square[4] == '4')

square[4] = mark; else if (choice == 5 && square[5] == '5')

square[5] = mark; else if (choice == 6 && square[6] == '6')

square[6] = mark; else if (choice == 7 && square[7] == '7')

square[7] = mark; else if (choice == 8 && square[8] == '8')

square[8] = mark; else { cout << "Invalid move "; player--; } i = checkwin(square); player++; } while (i == -1); board(player1Name, player2Name, square); if (i == 1) { int playerNumber = --player; if (playerNumber == 1) cout << "==>\aPlayer " << player1Name << " win "; else if (playerNumber == 2) cout << "==>\aPlayer " << player2Name << " win "; } else cout << "==>\aGame draw";

cout << endl;

cout << "Do you want to continue press y or Y :";

cin >> userChoice; } while (userChoice == 'y' || userChoice == 'Y');

cout << "Bye "; return 0; }

/*********************************************

FUNCTION TO RETURN GAME STATUS 1 FOR GAME IS OVER WITH RESULT -1 FOR GAME IS IN PROGRESS O GAME IS OVER AND NO RESULT **********************************************/

int checkwin(char square[]) { if (square[0] == square[1] && square[1] == square[2])

return 1; else if (square[3] == square[4] && square[4] == square[5])

return 1; else if (square[6] == square[7] && square[7] == square[8])

return 1; else if (square[0] == square[3] && square[3] == square[6])

return 1; else if (square[1] == square[4] && square[4] == square[7])

return 1; else if (square[2] == square[5] && square[5] == square[8])

return 1; else if (square[0] == square[4] && square[4] == square[8])

return 1; else if (square[2] == square[4] && square[4] == square[6])

return 1; else if (square[0] != '0' && square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8')

return 0; else return -1; }

/******************************************************************* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK ********************************************************************/

void board(string player1Name, string player2Name, char square[]) { cout << " \tTic Tac Toe ";

cout << "Player " << player1Name << " (X) - Player " << player2Name << " (O)" << endl << endl; cout << endl;

cout << " | | " << endl; cout << " " << square[0] << " | " << square[1] << " | " << square[2] << endl;

cout << "_____|_____|_____" << endl; cout << " | | " << endl;

cout << " " << square[3] << " | " << square[4] << " | " << square[5] << endl;

cout << "_____|_____|_____" << endl; cout << " | | " << endl;

cout << " " << square[6] << " | " << square[7] << " | " << square[8] << endl;

cout << " | | " << endl << endl; }

bool validateUseInput(char input[]) { int inputLength = strlen(input); if (inputLength>1) { cout << "----Invalid input Please enter valid input---- "; return false; } else if (input[0] >= 48 && input[0]<57) { return true; } else { cout << "----Invalid input Please enter valid input---- "; return false; } }

bool validatePlayerName(string playerName) { bool isValid = true; for (int i = 0; i= 65 && playerName[i] <= 90) || (playerName[i] >= 97 && playerName[i] <= 122))) { isValid = false; break; } } return isValid; }

I need two function for the code above please support me

the function are called playerOturn and playerXturn which allow to the user to make a choice furthemore th

char square[9] = { '0','1','2','3','4','5','6','7','8' }; is declared inside the do while loop please help me to declare outside the while but I want to make sure that will be declared as local variable

this is a C++ code

thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!