#include <iostream> #include <string> using namespace std; int checkwin(char squ
ID: 3665095 • Letter: #
Question
#include <iostream>
#include <string>
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 << "==>Player " << player1Name << " win ";
else if (playerNumber == 2)
cout << "==>Player " << player2Name << " win ";
}
else
cout << "==>Game 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 << " Tic 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<playerName.length(); i++)
{
if (!((playerName[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
Explanation / Answer
void playerXTurn()
{
int playerX;
char markX = 'X';
displayGameBoard();
cout << " First Enter a location for 'X' or please enter <T> to terminate the game: " << endl
<< " Now lease enter your selection > ";
cin >> playerX;
if (playerX == 1 && ticTac[1] == '1')
{
ticTac[1] = markX;
} // end if playerX for ticTac[1]
else if (playerX == 2 && ticTac[2] == '2')
{
ticTac[2] = markX;
} // end if... else playerX for ticTac[2]
else if (playerX == 3 && ticTac[3] == '3')
{
ticTac[3] = markX;
} // end if... else playerX for ticTac[3]
else if (playerX == 4 && ticTac[4] == '4')
{
ticTac[4] = markX;
} // end if... else playerX for ticTac[4]
else if (playerX == 5 && ticTac[5] == '5')
{
ticTac[5] = markX;
} // end if... else playerX for ticTac[5]
else if (playerX == 6 && ticTac[6] == '6')
{
ticTac[6] = markX;
} // end if... else playerX for ticTac[6]
else if (playerX == 7 && ticTac[7] == '7')
{
ticTac[7] = markX;
} // end if... else playerX for ticTac[7]
else if (playerX == 8 && ticTac[8] == '8')
{
ticTac[8] = markX;
} // end if... else playerX for ticTac[8]
else if (playerX == 9 && ticTac[9] == '9')
{
ticTac[9] = markX;
} // end if... else playerX for ticTac[9]
else if (playerX != 1, 2, 3, 4, 5, 6, 7, 8, 9)
{
cout << " Your selection is out of range of 1-9, select a location between 1 and 9 " << endl;
}
else
{
cout << "This osition is already occupied please select another position." << endl;
}
}
void playerOTurn()
{
int playerO;
char markO = 'O';
displayGameBoard();
cout << " Now Please enter a location for 'O' or enter <T> to terminate the game: " << endl
<< " Now please enter your selection > ";
cin >> playerO;
if (playerO == 1 && ticTac[1] == '1')
{
ticTac[1] = markO;
} // end if... else playerO for ticTac[1]
else if (playerO == 2 && ticTac[2] == '2')
{
ticTac[2] = markO;
} // end if... else playerO for ticTac[2]
else if (playerO == 3 && ticTac[3] == '3')
{
ticTac[3] = markO;
} // end if... else playerO for ticTac[3]
else if (playerO == 4 && ticTac[4] == '4')
{
ticTac[4] = markO;
} // end if... else playerO for ticTac[4]
else if (playerO == 5 && ticTac[5] == '5')
{
ticTac[5] = markO;
} // end if... else playerO for ticTac[5]
else if (playerO == 6 && ticTac[6] == '6')
{
ticTac[6] = markO;
} // end if... else playerO for ticTac[6]
else if (playerO == 7 && ticTac[7] == '7')
{
ticTac[7] = markO;
} // end if... else playerO for ticTac[7]
else if (playerO == 8 && ticTac[8] == '8')
{
ticTac[8] = markO;
} // end if... else playerO for ticTac[8]
else if (playerO == 9 && ticTac[9] == '9')
{
ticTac[9] = markO;
} // end if... else playerO for ticTac[9]
else if (playerO != 1, 2, 3, 4, 5, 6, 7, 8, 9)
{
cout << " your selection is out of range 1-9, " << endl;
}
else
{
cout << "This position is already occupied please choode another position." << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.