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!!