Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Pleas create a tic tac toe game. Must write a Player class to store each p

ID: 3875090 • Letter: #

Question

(C++) Pleas create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows must be numbered 1 to 3, and also the columns. The user will always enter the position of choice by entering two numbers with spaces between them.You can assume the user always enters a valid pair of numbers (within the 1-3 range) and will always select an open position (but it would be a good exercise to write code to test against this).

1. Write the Player class first. Make sure you can create instances of the class, call its member functions, and that those member functions work correctly.

2. Using a std::vector variable, create a representation of an empty game board with only the numbers 1-3, pipes, and dashes, but no x’s or o’s.

Each row will be one of the std::strings stored in the vector.

3. Write a loop that will print the empty board.

4. Write code that will prompt a user for a desired coordinate and update the variable to have an ’x’ at that position. Check it works by printing the board.Sample output:

This is a Tic Tac Toe game!

Please enter the name of player 1: [USER ENTERS A NAME]

Thanks. Please enter the name of player 2: [USER ENTERS A NAME]

How many rounds would you like to play? [USER ENTERS A NUMBER]

Let the game begin!

//This will be repeated while the number of rounds played has not exceeded the number of rounds chosen

//This will be repeated each round until one of the players has 3 in a row, 3 down, 3 in a diagonal, or the board is full.

//Board is displayed in a table format with pipes (|) and dashes (-), with 1 2 3 indicating row and column, and with player 1 represented by x’s and player 2 by o’s. See display*** It is [THE CORRECT PLAYER’S TURN].

Where would you like to play? Enter your row position and column position: row col: [USER ENTERS ROW NUMBER SPACE A COLUMN NUMBER]

//After the selection, the user’s token should be placed at the square and the board should be displayed again

//The board should be displayed after the round is over and a message (however you wish to convey the message) should say who won the round or state there was a draw if all 9 squares were filled but no one won; it should then give a summary of the current number of points each player has. The winning player should have their score increased by 1 and if there is a draw then neither player is awarded a point.

//When all the rounds have been played, there should be a final recap of the number of points and who the winner was (or state there was a draw if there was a tie in the scores)*

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
#define PLAYER1_MOVE "X"
#define PLAYER2_MOVE "O"
#define SIDE 3 // Length of the board
#define PLAYER1 1
#define PLAYER2 2


vector< vector<string> > board(3, vector<string>(3));//defining the board as a vector of vector of strings.
//Player Class
class Player
{
private:
string name;
int score;
public:
Player(){score=0;}

void setName(string name1)
{
name = name1;
}
void incrementScore()
{
score++;
}
string getName()
{
return name;
}
int getScore()
{
return score;
}
};
//Declaring and defining p1 and p2 as global variables.
Player p1;
Player p2;
void showBoard()//This will be called initially once and after every move.
{
printf(" ");
printf(" 1 2 3 ");
printf(" 1 %s | %s | %s ", board[0][0].c_str(),
board[0][1].c_str(), board[0][2].c_str());
printf(" -------------- ");
printf(" 2 %s | %s | %s ", board[1][0].c_str(),
board[1][1].c_str(), board[1][2].c_str());
printf(" -------------- ");
printf(" 3 %s | %s | %s ", board[2][0].c_str(),
board[2][1].c_str(), board[2][2].c_str());

return;
}

// A function to initialise the game.
void initialise()
{

// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = " ";
}

return;
}

void showInstructions()//Showing instructions to each player.
{

printf(" Where would you like to play? Enter your row position and column position: ");

return;
}
//TO display the message and increment scores when a player wins the game.
void declareWinner(int whoseTurn)
{
if (whoseTurn == PLAYER1)
{
cout << p1.getName() << " has won the game. ";
showBoard();
p1.incrementScore();
}
else
{
cout << p2.getName() << " has won the game. ";
showBoard();
p2.incrementScore();
}
return;
}

// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed()
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != " ")
return (true);
}
return(false);
}

// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed()
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != " ")
return (true);
}
return(false);
}

// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed()
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != " ")
return(true);

if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != " ")
return(true);

return(false);
}
//To check is some player has won the game.
bool gameOver()
{
return(rowCrossed() || columnCrossed()
|| diagonalCrossed() );
}

void playTicTacToe(int whoseTurn)
{

// Initialise the game
cout << "Lets Start a new Game. ";
initialise();
showBoard();


int x, y, noOfMoves=0;

// Keep playing till the game is over or it is a draw.
while (gameOver() == false && noOfMoves!=SIDE*SIDE)
{
if (whoseTurn == PLAYER1)
{
// Show the instructions before playing
cout << "It is " << p1.getName()<< "'s turn.";
showInstructions();
cin >> x >> y;
board[x-1][y-1] = PLAYER1_MOVE;
noOfMoves++;
showBoard();
whoseTurn = PLAYER2;
continue;
}

else if (whoseTurn == PLAYER2)
{
cout << "It is " << p2.getName()<< "'s turn.";
showInstructions();
cin >> x >> y;
board[x-1][y-1] = PLAYER2_MOVE;
showBoard();
whoseTurn = PLAYER1;
noOfMoves++;
continue;
}
}

// If the game has drawn
if (gameOver() == false &&
noOfMoves == SIDE * SIDE)
{
showBoard();
printf("It's a draw ");
}
else
{
// Toggling the user to declare the actual
// winner
if (whoseTurn == PLAYER1)
whoseTurn = PLAYER2;
else if (whoseTurn == PLAYER2)
whoseTurn = PLAYER1;

// Declare the winner
declareWinner(whoseTurn);
}
cout << p1.getName() << ": " << p1.getScore() << ", ";
cout << p2.getName() << ": " << p2.getScore() << endl;
return;
}
int main()
{
string name;
cout << "This is a Tic Tac Toe game!";
cout << " Please enter the name of player 1: ";
cin >> name;
cout << "Thanks.";
p1.setName(name);
cout << " Please enter the name of player 2: ";

cin >> name;
p2.setName(name);
cout << "Thanks.";
int noOfRounds;
cout << " How many rounds would you like to play? ";
cin >> noOfRounds;
for(int i=0;i<noOfRounds;i++)
{
//Each player alternates to play the first move starting with first player.
if(i%2==0)
playTicTacToe(PLAYER1);
else
playTicTacToe(PLAYER2);
}
cout << endl;
cout << p1.getName() << " has won " << p1.getScore() << " games. ";
cout << p2.getName() << " has won " << p2.getScore() << " games. ";
if(p1.getScore()>p2.getScore())
cout << p1.getName() << " is the winner.";
else if(p1.getScore() < p2.getScore())
cout << p2.getName() << " is the winner.";
else
cout << "It's a draw.";
}