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: 3875024 • 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)*

• 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

C++ Code:

  

#include <iostream.h>

#include <conio.h>

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();

void board(string , string);

int main()

{

int player = 1,i,choice,count,k=1;

string player1,player2;

char mark;

clrscr();

cout << " please enter the name of player 1:";

cin >> player1;

cout << " please enter the name of player2:";

cin >> player2;

cout << " How many rounds would you like to play";

cin >> count ;

while(k<=count)

{

do

{

board(player1,player2);

player=(player%2)?1:2;

if(player == 1)

cout << player1 << ", enter a number: ";

else

cout << player2 << ", enter a number: ";

cin >> choice;

mark=(player == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = mark;

else if (choice == 2 && square[2] == '2')

square[2] = mark;

else if (choice == 3 && square[3] == '3')

square[3] = mark;

else if (choice == 4 && square[4] == '4')

square[4] = mark;

else if (choice == 5 && square[5] == '5')

square[5] = mark;

else if (choice == 6 && square[6] == '6')

square[6] = mark;

else if (choice == 7 && square[7] == '7')

square[7] = mark;

else if (choice == 8 && square[8] == '8')

square[8] = mark;

else if (choice == 9 && square[9] == '9')

square[9] = mark;

else

{

cout<<"Invalid move ";

player--;

getch();

}

i=checkwin();

player++;

}while(i==-1);

board();

if(i==1)

cout<<"==>Player "<<--player<<" win ";

else

cout<<"==>Game draw";

k++;

}

getch();

return 0;

}

int checkwin()

{

if (square[1] == square[2] && square[2] == square[3])

return 1;

else if (square[4] == square[5] && square[5] == square[6])

return 1;

else if (square[7] == square[8] && square[8] == square[9])

return 1;

else if (square[1] == square[4] && square[4] == square[7])

return 1;

else if (square[2] == square[5] && square[5] == square[8])

return 1;

else if (square[3] == square[6] && square[6] == square[9])

return 1;

else if (square[1] == square[5] && square[5] == square[9])

return 1;

else if (square[3] == square[5] && square[5] == square[7])

return 1;

else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&

square[4] != '4' && square[5] != '5' && square[6] != '6' &&

square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;

else

return -1;

}

void board(string player1 , string player2)

{

cout << " Tic Tac Toe ";

cout << player1 "(X) - "player2 "(O)" << endl << endl;

cout << endl;

cout << " | | " << endl;

cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << endl;

}