Question: I want to create a class for the program and a header then do an operator overload this is the code #include #include using namespace

I want to create a class for the program and a header then do an operator overload this is the code

#include #include using namespace std;

const int X = 1, O = -1, EMPTY = 0 int board [3][3]; int currentPlayer; void clearBoard(){ for(int i = 0; i< 3; i++) for (int j = 0; j < 3; j++) board[i][j] = EMPTY; currentPlayer = X; } void putMark(int i,int j){ board[i][j] = currentPlayer; currentPlayer= -currentPlayer; } bool isWin(int mark){ int win = 3*mark; return ((board[0][0]+ board[0][1] + board[0][2] == win ) || (board[1][0] + board[1][1] + board [1][2] == win) || (board[2][0] + board[2][1] + board [2][2] == win) || (board[0][0] + board[1][0] + board [2][0] == win) || (board[0][1] + board[1][1] + board [2][1] == win) || (board[0][2] + board[1][2] + board [2][2] == win) || (board[0][0] + board[1][1] + board [2][2] == win) || (board[2][0] + board[1][1] + board [0][2] == win)); } int getWinner(){ if (isWin(X)) return X; else if (isWin(O)) return O; else return EMPTY; } void printBoard(){ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { switch (board[i][j]){ case X: cout << "X"; break; case O: cout << "O"; break; case EMPTY: cout << " "; break; } if (j < 2) cout << "l"; } if (i < 2) cout << " -+-+- " << endl; } } int main(){ clearBoard(); putMark(0,0); putMark(1,1); putMark(0,1); putMark(0,2); putMark(2,0); putMark(1,2); putMark(2,2); putMark(2,1); putMark(1,0); printBoard(); int winner = getWinner(); if (winner != EMPTY) cout<< "" << (winner == X? 'X' : 'O') << " wins " << endl; else cout << "Tie"<< endl; return EXIT_SUCCESS; }

The expected output should be Overload the << operator to print the board after each players turn.

and write a test program to test the class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!