// 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();

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 19PE
icon
Related questions
Question

// 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();

    

}
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 << "It" << mat[0][0] << "I" << mat[0][1] << "I" << mat[0][2]<<endl;
cout << "\t_I_I_"<<endl;
cout <<"\t" << mat[1][0] << "I" << mat[1][1] << "|" << mat[1][2]<<endl;
cout << "\t _|_I_"<<endl;
cout << "lt" << mat[2][0] << "I" << mat[2][1] << "l" << 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.!";
}
Transcribed Image Text:} 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 << "It" << mat[0][0] << "I" << mat[0][1] << "I" << mat[0][2]<<endl; cout << "\t_I_I_"<<endl; cout <<"\t" << mat[1][0] << "I" << mat[1][1] << "|" << mat[1][2]<<endl; cout << "\t _|_I_"<<endl; cout << "lt" << mat[2][0] << "I" << mat[2][1] << "l" << 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.!"; }
I/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.I" << 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] =
{
cout << "Player 2 wins...!!" << endl;
cout << "You have played Great..!" « endl;
break;
}
mat[2][2])
=
if (mat[0][0] == 'o' && mat[0][0] == mat[1][1] && mat[0][0] == mat[2][2])
{
cout << "Player 2 wins..!" << endl;
cout << "You have played Great..!!" << endl;
break;
}
if (mat[0][2] == 'o' && mat[0][2] == mat[1][1] && mat[0][0] == mat[2][0])
{
cout << "Player 2 wins...!" << endl;
cout << "You have played Great.!!" << endl;
break;
}
Transcribed Image Text:I/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.I" << 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] = { cout << "Player 2 wins...!!" << endl; cout << "You have played Great..!" « endl; break; } mat[2][2]) = if (mat[0][0] == 'o' && mat[0][0] == mat[1][1] && mat[0][0] == mat[2][2]) { cout << "Player 2 wins..!" << endl; cout << "You have played Great..!!" << endl; break; } if (mat[0][2] == 'o' && mat[0][2] == mat[1][1] && mat[0][0] == mat[2][0]) { cout << "Player 2 wins...!" << endl; cout << "You have played Great.!!" << endl; break; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning