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

(C++) The program should execute in the following manner. You must make sure the

ID: 3874857 • Letter: #

Question

(C++) The program should execute in the following manner. You must make sure the output matches the format below.

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)*

Further notes:

• You 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).

More guidance...

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.

Explanation / Answer


#include <iostream>
using namespace std;
/*******************************************************************
GAME PLAYER CLASS DEFINITION
********************************************************************/

class Player {
private:
string name;
public:
void setName(string n) {
name = n;
}

string getName() {
return name;
}
};
/*******************************************************************
GAME TICTACTOE CLASS DEFINITION
********************************************************************/
class TicTacToe {
private:
Player *p;
public:
TicTacToe(Player *pl) {
p = pl;
}
char square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int checkwin();
void board();
void resetSquare();
void getChoice(int playerTurn);
string getPlayerName(int index);
};

int main() {
int player = 1, i, turns, count=1;
string n;
Player p[2], temp;

// Get player details
cout << "Enter player 1 name:";
cin >> n;
p[0].setName(n);
cout << "Enter player 2 name:";
cin >> n;
p[1].setName(n);

cout << "Enter the number of turns: ";
cin >> turns;

while (count<=turns) {
TicTacToe game(p);
do {
game.board();
player = (player % 2) ? 1 : 2;
game.getChoice(player);
i = game.checkwin();
player++;
}
while (i == -1);
game.board();
cout << "Game " << count << " results: ";
  
// Print result of current round
if (i == 1)
cout << "==>Player " << game.getPlayerName(--player) << " win ";
else
cout << "==>Game draw";
  
count++;
  
// Swap players
temp = p[0];
p[0] = p[1];
p[1]=temp;
  
}
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 TicTacToe::checkwin() {
  
if (square[0][0] == square[0][1] && square[0][1] == square[0][2] && square[0][0]!=' ' && square[0][1]!=' ' && square[0][2]!=' ')
return 1;
  
else if (square[1][0] == square[1][1] && square[1][1] == square[1][2] && square[1][0]!=' ' && square[1][1]!=' ' && square[1][2]!=' ')
return 1;

else if (square[2][0] == square[2][1] && square[2][1] == square[2][2] && square[2][0]!=' ' && square[2][1]!=' ' && square[2][2]!=' ')
return 1;

else if (square[0][0] == square[1][0] && square[1][0] == square[2][0] && square[0][0]!=' ' && square[1][0]!=' ' && square[2][0]!=' ')
return 1;

else if (square[0][1] == square[1][1] && square[1][1] == square[2][1] && square[0][1]!=' ' && square[1][1]!=' ' && square[2][1]!=' ')
return 1;

else if (square[0][2] == square[1][2] && square[1][2] == square[2][2] && square[0][2]!=' ' && square[1][2]!=' ' && square[2][2]!=' ')
return 1;

else if (square[0][0] == square[1][1] && square[1][1] == square[2][2] && square[0][0]!=' ' && square[1][1]!=' ' && square[2][2]!=' ')
return 1;

else if (square[0][2] == square[1][1] && square[1][1] == square[2][0] && square[0][2]!=' ' && square[1][1]!=' ' && square[2][0]!=' ')
return 1;

else if (square[0][0] != ' ' && square[0][1] != ' ' && square[0][2] != ' ' && square[1][0] != ' ' && square[1][1] != ' ' && square[1][2]!= ' ' && square[2][0] != ' ' && square[2][1] != ' ' && square[2][2] != ' ')
return 0;

else
return -1;
}

/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/

void TicTacToe::board() {
system("cls");
cout << " Tic Tac Toe ";

cout << p[0].getName() << "(X) vs " << p[1].
getName() << "(O)" << endl << endl;
cout << endl;
  
cout << " 1 2 3 " << endl;
cout << " | | " << endl;
cout << "1 " << square[0][0] << " | " << square[0][1] << " | " << square[0][2] << endl;
cout << " _____|_____|_____" << endl;
  
cout << " | | " << endl;
cout << "2 " << square[1][0] << " | " << square[1][1] << " | " << square[1][2] << endl;
cout << " _____|_____|_____" << endl;
  
cout << " | | " << endl;
cout << "3 " << square[2][0] << " | " << square[2][1] << " | " << square[2][2] << endl;
cout << " | | " << endl << endl;
}

/*******************************************************************
FUNCTION TO GET CHOICE FROM THE USER
********************************************************************/
void TicTacToe::getChoice(int player) {
char mark;
int x,y;
// X is left iterator and Y is right iterator
cout << "Player " << p[player - 1].getName() << ", enter co ordinates: ";
cin >> x>>y;
  
char defaultValue = '0'+ (x*y);
cout<<defaultValue;

mark = (player == 1) ? 'X' : 'O';
  
if(square[x-1][y-1] == ' '){
square[x-1][y-1] = mark;
} else if(x>3 || x<1 || y<1 || y> 3){
cout << "Invalid move ";
player--;
}
}
/*******************************************************************
FUNCTION TO GET PLAYER NAME
********************************************************************/
string TicTacToe::getPlayerName(int index){
return p[index-1].getName();
}
/*******************************************************************
               END OF PROJECT
********************************************************************/