// Fix this program that works // Use welcome function that display the message in diffreent color // Use Draw function to diplay table in gree/yellow // Remove, add, any code that you believe is not useful #include #include using namespace std; char mat[3][3]; void table(); //function to print the table void welcome(); //function for welcome screen int main() { int i, j, m, n, sum = 0; char ch; welcome(); system("pause"); for (m = 0; m < 3; m++) for (n = 0; n < 3; n++) mat[m][n] ='W'; table(); while (sum < 10) { //================== for player 1 =================== cout << "Player 1 is X Choose the position : "; cout << "Row:"; cin >> i; cout << "Coloumn:"; cin >> j; //============== if position is wrong ============ for (; i > 3 || i < 1 || j>3 || j < 1 || ('x' == mat[i - 1][j - 1] || 'o' == mat[i - 1][j - 1]);) { cout << "Sorry!!!!wrong position, Choose the position again"; cout << "Row:"; cin >> i; cout << "Coloumn:"; cin >> j; } mat[i - 1][j - 1] = 'x'; sum++; //=============== to check if player 1 wins or not=================== if (mat[0][0] == 'x' && mat[0][0] == mat[0][1] && mat[0][0] == mat[0][2]) { table(); cout << "Player 1 wins…….!!!"; cout << "You have played Great…..!!!"; // break; } if (mat[1][0] == 'x' && mat[1][0] == mat[1][1] && mat[1][0] == mat[1][2]) { table(); cout << "Player 1 wins…….!!!"; cout << "You have played Great…..!!!"<> i; cout << "Coloumn:"; cin >> j; //if position is wrong for (; i > 3 || i < 1 || j>3 || j < 1 || ('x' == mat[i - 1][j - 1] || 'o' == mat[i - 1][j - 1]);) { cout << "Sorry!!!!wrong position, Choose the position again" << endl; cout << "Row:"<> i; cout << "Coloumn:" << endl; cin >> j; } mat[i - 1][j - 1] = 'o'; sum++; table();
// Fix this program that works
// Use welcome function that display the message in diffreent color
// Use Draw function to diplay table in gree/yellow
// Remove, add, any code that you believe is not useful
#include <iostream>
#include <windows.h>
using namespace std;
char mat[3][3];
void table(); //function to print the table
void welcome(); //function for welcome screen
int main()
{
int i, j, m, n, sum = 0;
char ch;
welcome();
system("pause");
for (m = 0; m < 3; m++)
for (n = 0; n < 3; n++)
mat[m][n] ='W';
table();
while (sum < 10)
{
//================== for player 1 ===================
cout << "Player 1 is X Choose the position : ";
cout << "Row:";
cin >> i;
cout << "Coloumn:";
cin >> j;
//============== if position is wrong ============
for (; i > 3 || i < 1 || j>3 || j < 1 || ('x' == mat[i - 1][j - 1] || 'o' == mat[i - 1][j - 1]);)
{
cout << "Sorry!!!!wrong position, Choose the position again";
cout << "Row:";
cin >> i;
cout << "Coloumn:";
cin >> j;
}
mat[i - 1][j - 1] = 'x';
sum++;
//=============== to check if player 1 wins or not===================
if (mat[0][0] == 'x' && mat[0][0] == mat[0][1] && mat[0][0] == mat[0][2])
{
table();
cout << "Player 1 wins…….!!!";
cout << "You have played Great…..!!!";
//
break;
}
if (mat[1][0] == 'x' && mat[1][0] == mat[1][1] && mat[1][0] == mat[1][2])
{
table();
cout << "Player 1 wins…….!!!";
cout << "You have played Great…..!!!"<<endl;
break;
}
if (mat[2][0] == 'x' && mat[2][0] == mat[2][1] && mat[2][0] == mat[2][2])
{
table();
cout << "Player 1 wins…….!!!";
cout << "You have played Great…..!!!"<<endl;
break;
}
if (mat[0][0] == 'x' && mat[0][0] == mat[1][0] && mat[0][0] == mat[2][0])
{
table();
cout << "Player 1 wins…….!!!";
cout << "You have played Great…..!!!";
break;
}
if (mat[0][1] == 'x' && mat[0][1] == mat[1][1] && mat[0][1] == mat[2][1])
{
table();
cout << "Player 1 wins…….!!!";
cout << "You have played Great…..!!!";
break;
}
if (mat[0][2] == 'x' && mat[0][2] == mat[1][2] && mat[0][2] == mat[2][2])
{
table();
cout << "Player 1 wins…….!!!"<<endl;
cout << "You have played Great…..!!!"<<endl;
break;
}
if (mat[0][0] == 'x' && mat[0][0] == mat[1][1] && mat[0][0] == mat[2][2])
{
table();
cout << "Player 1 wins…….!!!"<<endl;
cout << "You have played Great…..!!!"<<endl;
break;
}
if (mat[0][2] == 'x' && mat[0][2] == mat[1][1] && mat[0][0] == mat[2][0])
{
table();
cout << "Player 1 wins…….!!!"<<endl;
cout << "You have played Great…..!!!"<<endl;
break;
}
if (sum == 9) //to check the chances
{
table();
cout << "The game is over…….no one wins…HaHaHa…..!!!";
break;
}
//for player 2
cout << "Player 2 is’o’nChoose the position : " << endl;
cout << "Row:";
cin >> i;
cout << "Coloumn:";
cin >> j;
//if position is wrong
for (; i > 3 || i < 1 || j>3 || j < 1 || ('x' == mat[i - 1][j - 1] || 'o' == mat[i - 1][j - 1]);)
{
cout << "Sorry!!!!wrong position, Choose the position again" << endl;
cout << "Row:"<<endl;
cin >> i;
cout << "Coloumn:" << endl;
cin >> j;
}
mat[i - 1][j - 1] = 'o';
sum++;
table();
![```cpp
cout << "Would you like to play more.... ? (y / n) : " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
system("cls");
else
{
cout << "Thanks for Playing.........:)" << endl;
exit(0);
}
return 0;
}
//======================== draw table ====================
void table()
{
system("cls");
cout << " 1 2 3 " << endl;
cout << "\t" << mat[0][0] << "|" << mat[0][1] << "|" << mat[0][2] << endl;
cout << "\t_ _ _ " << endl;
cout << "\t" << mat[1][0] << "|" << mat[1][1] << "|" << mat[1][2] << endl;
cout << "\t_ _ _ " << endl;
cout << "\t" << mat[2][0] << "|" << mat[2][1] << "|" << mat[2][2] << endl;
}
//========================
void welcome()
{
/* Change the color here for welcome message*/
cout << "Welcome To Tic - Tac - Toe Game" << endl;
cout << "Press any key to continue.....!!";
}
```
The code is a part of a console-based Tic-Tac-Toe game written in C++. It includes functions to display the welcome message, draw the game table, and ask the user if they want to play again.
**Key Components:**
1. **Replay Functionality:**
- Prompts the user with "Would you like to play more....? (y / n) :".
- If the user inputs 'y' or 'Y', the game screen refreshes; otherwise, it displays a thank you message and exits.
2. **Draw Table Function:**
- Clears the screen and prints the Tic-Tac-Toe board structure.
- The board is displayed with a 3x3 grid, showing current gameplay positions.
3. **Welcome Function:**
- Displays a welcome message for the Tic-Tac-Toe game.
- Prompts the player to press any key to start the game.
```](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2c30b2c3-62fe-421c-b864-63a8dde6d2d9%2Ffada8e8c-ef25-425a-a995-d77097886159%2F5zh4f78_processed.png&w=3840&q=75)
![```cpp
// to check player 2 wins or not
if (mat[0][0] == 'o' && mat[0][0] == mat[0][1] && mat[0][0] == mat[0][2])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[1][0] == 'o' && mat[1][0] == mat[1][1] && mat[1][0] == mat[1][2])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[2][0] == 'o' && mat[2][0] == mat[2][1] && mat[2][0] == mat[2][2])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[0][0] == 'o' && mat[0][0] == mat[1][0] && mat[0][0] == mat[2][0])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[0][1] == 'o' && mat[0][1] == mat[1][1] && mat[0][1] == mat[2][1])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[0][2] == 'o' && mat[0][2] == mat[1][2] && mat[0][2] == mat[2][2])
{
cout << "Player 2 wins……!!!" << endl;
cout << "You have played Great…..!!!" << endl;
break;
}
if (mat[0][0] == 'o' && mat[0][0] == mat[1][1] && mat[0][0] == mat[2][2])
{
cout << "Player 2 wins](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F2c30b2c3-62fe-421c-b864-63a8dde6d2d9%2Ffada8e8c-ef25-425a-a995-d77097886159%2Ff8wjgrj_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









