Hi I have a homework assignment and I have to take a previous solution and do a certain set of instructions to it.
Here is the code:
// Program: TicTacToe with loops
// Date: October 20, 2012
// Author: Yours Truly
int main(void)
{
bool gameOver = false; // game loop control flag
char posLocation; // user input variable for position choice
char playAgain = 'A'; // program exit control flag
char playerSymbol = 'X'; // current player symbol
string pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9; // board display variables
while( !gameOver ) // Loop until a winner is determined or all squars have been used
{
posLocation = GetValidUserInput(playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Get vaild user inpur
SetGameBoardPosition(posLocation, playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Set the appropriate board position
DisplayGameBoard(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Display the game board
gameOver = CheckForGameOver(playerSymbol, pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9); // Check for end of game condition and change the player symbol
}
playAgain = RequestAdditionalGame(); // Determine whether to play the game again
}
DisplayCredits(); // Display the credit screen
return 0;
}
cout << "Enter \"A\" to play again, any other key to exit: " << endl << endl; // getting user input for whether the player wants to play again
cin >> userEntry;
userEntry = toupper(userEntry);
cout << endl << endl;
return(userEntry);
}
void UpdatePlayer(char& playerSymbol)
{
if(playerSymbol == 'O') // alternating between the X and O player
playerSymbol = 'X';
else
playerSymbol = 'O';
}
while( !validInput)
{
cout << "Enter the location number where you want to place an \"" << playerSymbol << "\": ";
cin >> entry; // getting user input for the board position
if( entry >= '1' && entry <= '9')
{
if( (entry == '1' && pos1 == "1") || (entry == '2' && pos2 == "2") || (entry == '3' && pos3 == "3") ||
(entry == '4' && pos4 == "4") || (entry == '5' && pos5 == "5") || (entry == '6' && pos6 == "6") ||
(entry == '7' && pos7 == "7") || (entry == '8' && pos8 == "8") || (entry == '9' && pos9 == "9") )
{
validInput = true;
}
else
{
cout << "You have entered a position that has already been used. ";
validInput = false;
}
}
else
{
cout << "You have entered an incorrect value. Please enter a number from 1 to 9. ";
}
cout << endl << endl;
}
return entry;
}
void DisplayCredits(void)
{
cout << endl << endl;
cout << "************* CREDITS *************" << endl; // displaying the game credits
cout << " Designer: Me *" << endl;
cout << " Programmer: Me *" << endl;
cout << " Art Production: Me *" << endl;
cout << " Everything Else: All Me *" << endl;
cout << "*************************************" << endl;
cout << endl << endl;
system("pause");
}
And here is what I have to do to it:
1. Remove the pos1 through pos9 variables and replace them with an array of char containing 9 elements.
2. Modify the affected functions to accept the new array as a parameter and adjust the function code appropriately.
3. In order to handle some type conversion issues you’ll run into, investigate the “atoi” and “itoa” functions.
2. Modify the affected functions to accept the new array as a parameter and adjust the function code appropriately.
1 2 3 4
void affectedFunction(string pos[9])
{
// ...
}
3. In order to handle some type conversion issues you’ll run into, investigate the “atoi” and “itoa” functions.
itoa() isn't standard. And since you're using std::strings instead of C strings char[], you might as well make one more step forward and look into string streams. http://cplusplus.com/reference/sstream/
int main(void) is archaic.
And, please use code tags, which preserve indentation and have syntax highlighting.
Very sorry about that I'm new here and am in a rush because this has to be submitted by midnight. I am in over my head here and have no idea what half of these instructions mean so sorting this out is proving to be difficult. What I gave you if exactly what my professor gave me without any instruction so you can see my problem.