#include #include #include #include using namespace std; enum SquareLocation { T
ID: 3651093 • Letter: #
Question
#include#include
#include
#include
using namespace std;
enum SquareLocation { TOP_LEFT = 0, TOP_CENTER = 1, TOP_RIGHT = 2, MIDDLE_LEFT = 3, MIDDLE_CENTER = 4, MIDDLE_RIGHT = 5, BOTTOM_LEFT = 6, BOTTOM_CENTER = 7, BOTTOM_RIGHT = 8 };
enum Square { EMPTY, X, O };
static const int NUM_SQUARES = 9;
typedef struct { Square squares[NUM_SQUARES]; bool isXsTurn; } Game;
string SquareToString(Square aSquare) { if (aSquare == EMPTY) { return " "; }
else if (aSquare == X) { return "X"; }
else
if (aSquare == O) { return "O"; } else { assert(false);
//this should never happen, so we check with an assert!
}
} void DisplayInstructions()
{
cout << "You may enter numbers as shown below to represent which square you want to move to..." << endl << endl;
cout << " 0 | 1 | 2" << endl; cout << "-----------" << endl;
cout << " 3 | 4 | 5" << endl; cout << "-----------" << endl;
cout << " 6 | 7 | 8" << endl;
cout << endl;
}
void DisplayBoard (Game aGame)
{
cout << "The board currently looks like this:" << endl << endl;
cout << " " << SquareToString(aGame.squares[TOP_LEFT]) << " | " << SquareToString(aGame.squares[TOP_CENTER]) << " | " << SquareToString(aGame.squares[TOP_RIGHT]) << endl;
cout << "-----------" << endl;
cout << " " << SquareToString(aGame.squares[MIDDLE_LEFT]) << " | " << SquareToString(aGame.squares[MIDDLE_CENTER]) << " | " << SquareToString(aGame.squares[MIDDLE_RIGHT]) << endl;
cout << "-----------" << endl; cout << " " << SquareToString(aGame.squares[BOTTOM_LEFT]) << " | " << SquareToString(aGame.squares[BOTTOM_CENTER]) << " | " << SquareToString(aGame.squar BOTTOM_RIGHT]);
cout << endl << endl;
}
Game GetNewGame()
{ Game newGame; for (int i = TOP_LEFT; i <= BOTTOM_RIGHT; i++) { newGame.squares[i] = EMPTY; } newGame.isXsTurn = true; return newGame; } SquareLocation StringToLocation(string aInput) { return (SquareLocation) atoi(aInput.c_str()); } //We recommend you implement this function as part of your solution bool GameIsOver(Game aGame) { //Consider what conditions indicate the game is over //What are the three ways a game can end? } //We recommend you implement this function as part of your solution Game DoTurn(Game aGame) { //a turn should consist of the following //prompt the user for a move //check if they entered a valid move (if not, you should ask until they give you a valid move! //it would be nice to remind them of the instructions if they enter an invalid move... //once they have entered the right move, you need to update the game record //don't forget to keep track of whose turn it is! //return the updated game record when done } //We recommend you implement this function as part of your solution void DisplayResults(Game aGame) { //you should display the final state of the board //as well as a message about who won } //you can leave this main function unchanged if you want //it is sufficient to simply implement the three functions above //for full credit. however even if you use those suggested functions //you should probably create a few more helper functions for those //tasks int main() { //here we get a blank board and note that X plays first... Game currentGame = GetNewGame(); //let the user know how to play cout << "Welcome to the game of Tic Tac Toe!" << endl; DisplayInstructions(); //keep making moves until the game is over while (!GameIsOver(currentGame)) { DisplayBoard(currentGame); currentGame = DoTurn(currentGame); } //let the user know the end results... DisplayResults(currentGame); return 0; }
Explanation / Answer
the code was not understanble no spaces and no gaps please resend the qestion
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.