C++ Make a Tic Tac Toe game fo

C++ Make a Tic Tac Toe game for 2 players to play using 2Darrays and classes. Do not add more #include functions other thanthe ones listed.

I never said what type of code I needed in a previous question.I apologize and I can’t go back and change it so here is the samequestion with more information

Using the tictactoeGame class, write a main program that uses atictactoeGame to implement a game in which two players (you and afriend) take turns placing X’s and O’s onto the board. After eachturn, the current board configuration should be displayed, and oncea player connects three of a kind, the game should end declaringthat player the winner.

#include#includeusing namespace std;

class tictactoeGame{    public:

        charboardConfig[3][3];   // two dimensional array storescurrent board configuration      // Constructor:        // set boardConfig[i][j]to be ‘ ‘ (the space character)        // for 0<= i <= 2,0<= j <= 2        tictactoeGame()        {           //fill thisin        }

   //put an ‘X’ character at the givenlocation                bool placeX(int x, inty)        {           //fill thisin        }

   //put an ‘O’ character at the givenlocation               bool placeO(int x, inty)        {           //fill thisin        }

   //set all positions to character ‘ ‘.        void clear()        {           //fill thisin        }

        // Return true ifthere are 3 ‘X’ marks placed in a single        // column, row, ordiagnol. Return false otherwise.        bool xWins()        {           //fill thisin        }

        // Return true ifthere are 3 ‘O’ marks placed in a single        // column, row, ordiagnol. Return false otherwise.            bool oWins()        {           //fill thisin        }

        // Return true ifthere are either 3 ‘X’ marks or 3 ‘O’ marks        // placed in a singlecolumn, row, or diagnol, or if the board is full.    // Return false otherwise.        bool gameOver()        {           //fill thisin        }

        // cout a nicelooking picture of the board configuration        void display()        {           //fill thisin        }

};

int main(){return 0;}

Answer:

The code has been kept as simpler it could be for betterunderstanding, please use the only integer for the row value(x) andcolumn value(y) asked in the program. If the entered configurationof row and column already filled with X or O in that case code willsay invalid input and ask to enter again, but it is not handled forvalues other than an integer ( for ex. if you enter “asd” in theplace of row and column the program will terminate, if you want meto handle this it will be a little complex if you want, mention inthe comment section, I will provide it too). The explanation of thecode has been added to the comment lines and a sample screenshot ofthe running program has been added in the last as well.

CODE:

#include<iostream>using namespace std;class tictactoeGame{    public:        char boardConfig[3][3];   // two dimensional array stores current board configuration    // Constructor:        // set boardConfig[i][j] to be ' ' (the space character)        // for 0<= i <= 2, 0<= j <= 2        tictactoeGame()        {           //fill this in           for(int i=0;i<=2;i++)            for(int j=0;j<=2;j++)                boardConfig[i][j] = ' ';            // initialized all the values with space        }   //put an 'X' character at the given location        bool placeX(int x, int y)        {           //fill this in           if (x>=0 && x<=2 && y>=0 && y<=2 && boardConfig[x][y]==' '){   // check if it is valid, if it is place x on the position else return false                boardConfig[x][y] = 'X';                return true;           }           return false;        }   //put an 'O' character at the given location        bool placeO(int x, int y)        {           //fill this in           if (x>=0 && x<=2 && y>=0 && y<=2 && boardConfig[x][y]==' '){      // same for player O                boardConfig[x][y] = 'O';                return true;           }           return false;        }   //set all positions to character ' '.        void clear()                                                       // to clear the board, assign space ' ' to all the places in board        {           //fill this in           for(int i=0;i<=2;i++)            for(int j=0;j<=2;j++)                boardConfig[i][j] = ' ';        }        // Return true if there are 3 'X' marks placed in a single        // column, row, or diagnol. Return false otherwise.        bool xWins()        {           //fill this in           // check for row win           bool win = true;           for(int i=0;i<=2;i++){                win = true;                for(int j=0;j<=2;j++){                   // checking for all the row values if it is equa to X                    if (boardConfig[i][j]!='X'){                        win = false;                        continue;                    }                }                if (win)                    return win;           }           // check for col win           win = true;           for(int i=0;i<=2;i++){                win = true;                for(int j=0;j<=2;j++){                    if (boardConfig[j][i]!='X'){          // checking for all the column values if any column value consists 3 x                        win = false;                        continue;                    }                }                if (win)                    return win;           }           // check for diagonal win           win = true;           for(int i=0;i<=2;i++){                if (boardConfig[i][i]!='X')              // checking for main diagonal ( i==j for main diagonal)                        win = false;           }           if (win)                return win;            win = true;            for(int i=0;i<=2;i++){                int j = 2-i;                 if (boardConfig[i][j]!='X')            // checking for other diagonal                        win = false;            }            return win;        }        // Return true if there are 3 'O' marks placed in a single        // column, row, or diagnol. Return false otherwise.        bool oWins()                                        // same as xWins()        {           //fill this in           // check for row win           bool win = true;           for(int i=0;i<=2;i++){                win = true;                for(int j=0;j<=2;j++){                    if (boardConfig[i][j]!='O'){                        win = false;                        continue;                    }                }                if (win)                    return win;           }           // check for col win           win = true;           for(int i=0;i<=2;i++){                win = true;                for(int j=0;j<=2;j++){                    if (boardConfig[j][i]!='O'){                        win = false;                        continue;                    }                }                if (win)                    return win;           }           // check for diagonal win           win = true;           for(int i=0;i<=2;i++){                if (boardConfig[i][i]!='O')                        win = false;           }           if (win)                return win;            win = true;            for(int i=0;i<=2;i++){                int j = 2-i;                 if (boardConfig[i][j]!='O')                        win = false;            }            return win;        }        // Return true if there are either 3 'X' marks or 3 'O' marks        // placed in a single column, row, or diagnol, or if the board is full.    // Return false otherwise.        bool gameOver()        {           //fill this in           if (xWins() || oWins())               // if any one of player (x or o) won return true else false                return true;           return false;        }        // cout a nice looking picture of the board configuration        void display()                           // for displaying the board        {           //fill this in            cout<<"\n\n";            cout<<"\t\t\t"<<boardConfig[0][0]<<" | "<<boardConfig[0][1]<<" | "<<boardConfig[0][2]<<endl;            cout<<"\t\t\t---------"<<endl;            cout<<"\t\t\t"<<boardConfig[1][0]<<" | "<<boardConfig[1][1]<<" | "<<boardConfig[1][2]<<endl;            cout<<"\t\t\t---------"<<endl;            cout<<"\t\t\t"<<boardConfig[2][0]<<" | "<<boardConfig[2][1]<<" | "<<boardConfig[2][2]<<endl;    return;        }};int main(){    tictactoeGame game;    int i = 0,x,y;    game.display();    while(!game.gameOver()){            // loop until the game over        if (i%2==0){                    // i is a counter if i is even it denotes its x's turn else its y's turn            cout<<"X's turn: "<<endl;            cout<<"Enter integer values of x and y to place X (follow 0-indexing): ";  // ask the user to enter the position values for the board            cin>>x>>y;            while(!game.placeX(x,y)){                cout<<"Invalid input! please enter again: ";    // if placeX return false keep asking for the valid input                cin>>x>>y;            }            game.display();        // after placing x display the board            if (game.xWins()){     // check if x wins or not and declare if won.                cout<<"Player X won!"<<endl;            }        }        else{            cout<<"O's turn: "<<endl;      // same for O's turn            cout<<"Enter integer values of x and y to place O (follow 0-indexing): ";            cin>>x>>y;            while(!game.placeO(x,y)){                cout<<"Invalid input! please enter again: ";                cin>>x>>y;            }            game.display();            if (game.oWins()){                cout<<"Player O won!"<<endl;            }        }        i++;    }    game.clear();           // in last clear the boardconfig.    return 0;}

OUTPUT(SCREENSHOT)

NOTE: If you have any queries regarding the solution,you can menstion in the comment box. HAPPY LEARNING!!


 
"Our Prices Start at $11.99. As Our First Client, Use Coupon Code GET15 to claim 15% Discount This Month!!"
Calculate the price
Make an order in advance and get the best price
Pages (550 words)
$0.00
*Price with a welcome 15% discount applied.
Pro tip: If you want to save more money and pay the lowest price, you need to set a more extended deadline.
We know how difficult it is to be a student these days. That's why our prices are one of the most affordable on the market, and there are no hidden fees.

Instead, we offer bonuses, discounts, and free services to make your experience outstanding.
How it works
Receive a 100% original paper that will pass Turnitin from a top essay writing service
step 1
Upload your instructions
Fill out the order form and provide paper details. You can even attach screenshots or add additional instructions later. If something is not clear or missing, the writer will contact you for clarification.
Pro service tips
How to get the most out of your experience with brilliantassignmenthelp.com
One writer throughout the entire course
If you like the writer, you can hire them again. Just copy & paste their ID on the order form ("Preferred Writer's ID" field). This way, your vocabulary will be uniform, and the writer will be aware of your needs.
The same paper from different writers
You can order essay or any other work from two different writers to choose the best one or give another version to a friend. This can be done through the add-on "Same paper from another writer."
Copy of sources used by the writer
Our college essay writers work with ScienceDirect and other databases. They can send you articles or materials used in PDF or through screenshots. Just tick the "Copy of sources" field on the order form.
Testimonials
See why 20k+ students have chosen us as their sole writing assistance provider
Check out the latest reviews and opinions submitted by real customers worldwide and make an informed decision.
11,595
Customer reviews in total
96%
Current satisfaction rate
3 pages
Average paper length
37%
Customers referred by a friend
OUR GIFT TO YOU
15% OFF your first order
Use a coupon FIRST15 and enjoy expert help with any task at the most affordable price.
Claim my 15% OFF Order in Chat
Close

Sometimes it is hard to do all the work on your own

Let us help you get a good grade on your paper. Get professional help and free up your time for more important courses. Let us handle your;

  • Dissertations and Thesis
  • Essays
  • All Assignments

  • Research papers
  • Terms Papers
  • Online Classes