SigTerm said this: "Smells like a bad code. squareOne..squareSix should be probably array. Instead of char constants, enums or ints should be probably used... " How do i do that with this code for the board also if someone could suggest the code I would have to use to get users to enter the row and column for the X and O for the game.
void showboard(char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
char &squareEight, char &squareNine)
{
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl
<< "-+-+-"<< endl
<< squareFour << "|" << squareFive << "|" << squareSix << endl
<< "-+-+-"<< endl
<< squareSeven << "|" << squareEight << "|" << squareNine << endl;
The problem I am having is taking this code and turning it to the a function. Listed below is the code of what I am trying to make into a function. Followed after it the "checkboard" function is me trying to make the code above it into a function
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
if (squareOne != '1') {
if (squareTwo == squareOne && squareThree == squareOne) {
bGameOver = true;
}
if (squareFour == squareOne && squareSeven == squareOne) {
bGameOver = true;
}
}
if (squareFive != '5')
{
if (squareOne == squareFive && squareNine == squareFive)
{
bGameOver = true;
}
if (squareTwo == squareFive && squareEight == squareFive)
{
bGameOver = true;
}
if (squareFour == squareFive && squareSix == squareFive)
{
bGameOver = true;
}
if (squareThree == squareFive && squareSeven == squareFive)
{
bGameOver = true;
}
}
if (squareNine != '9')
{
if (squareThree == squareNine && squareSix == squareNine)
{
bGameOver = true;
}
if (squareSeven == squareNine && squareEight == squareNine)
{
bGameOver = true;
}
}
/* Need to check the board full (no-win condition*/)
if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
squareFour != '4' && squareFive != '5' && squareSix != '6' &&
squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << currentPlayer << " wins!" << endl;
}
// Print ending board
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
cout << "-+-+-"<< endl;
cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
cout << "-+-+-"<< endl;
cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
/* Clear the board*/
squareOne = '1';
squareTwo = '2';
squareThree = '3';
squareFour = '4';
squareFive = '5';
squareSix = '6';
squareSeven = '7';
squareEight = '8';
squareNine = '9';
}
currentPlayer[100] = 1;
}
} while (!bGameOver);
void checkboard(char** pCurrentPlayer, char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
char &squareEight, char &squareNine, char &playerMarker, bool bGameOver)
{
bGameOver = false;
bool bWinGame = true;
// Check for end of game conditions
if (squareOne != '1') {
if (squareTwo == squareOne && squareThree == squareOne) {
bGameOver = true;
}
if (squareFour == squareOne && squareSeven == squareOne) {
bGameOver = true;
}
}
if (squareFive != '5')
{
if (squareOne == squareFive && squareNine == squareFive)
{
bGameOver = true;
}
if (squareTwo == squareFive && squareEight == squareFive)
{
bGameOver = true;
}
if (squareFour == squareFive && squareSix == squareFive)
{
bGameOver = true;
}
if (squareThree == squareFive && squareSeven == squareFive)
{
bGameOver = true;
}
}
if (squareNine != '9')
{
if (squareThree == squareNine && squareSix == squareNine)
{
bGameOver = true;
}
if (squareSeven == squareNine && squareEight == squareNine)
{
bGameOver = true;
}
}
// Need to check the board full (no-win condition)
if (squareOne != '1' && squareTwo != '2' && squareThree != '3' &&
squareFour != '4' && squareFive != '5' && squareSix != '6' &&
squareSeven != '7' && squareEight != '8' && squareNine != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
system("cls");
if (bGameOver)
{
if (bWinGame)
{
cout << "Player " << pCurrentPlayer << " wins!" << endl;
}
// Print ending board
cout << squareOne << "|" << squareTwo << "|" << squareThree << endl;
cout << "-+-+-"<< endl;
cout << squareFour << "|" << squareFive << "|" << squareSix << endl;
cout << "-+-+-"<< endl;
cout << squareSeven << "|" << squareEight << "|" << squareNine << endl;
cout << "Game Over!" << endl;
cout << "Play again (y/n)?" << endl;
char cPlayAgain;
cin >> cPlayAgain;
if (cPlayAgain == 'y')
{
bGameOver = false;
// Clear the board
squareOne = '1';
squareTwo = '2';
squareThree = '3';
squareFour = '4';
squareFive = '5';
squareSix = '6';
squareSeven = '7';
squareEight = '8';
squareNine = '9';
}
pCurrentPlayer = "1";
}
}