I have a code. It is running after built. However, when running it only played only one person and ended. The intention of making it is to make two players playing tic tac toe. Where do I go wrong in this code.
Code:
//Timing. #include<iostream> using namespace std; int check( char g[3][3], int r, int c, int p) { if ( g[r][c] !='*') { cout << "\n\tSorry, wrong entry."; return 0; } char ch; if (p == 0) ch= 'X'; if (p == 1) ch= 'O'; g[r][c] = ch; cout << "\n\n"; for ( int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++ ) cout << " " << g[i][j]; cout << "\n"; } if(//Row Section (( g [0][0]==ch) && (g[0][1]==ch) && (g[0][2]==ch)) || ((g[1][0]==ch) && (g[1][1]==ch) && (g[1][2]==ch)) ||((g[2][0]==ch) && (g[2][1]==ch) && (g[2][2]==ch))|| //Column Section ((g[0][0]==ch) && (g[1][0]==ch) && (g[2][0]==ch))|| ((g[0][1]==ch) && (g[1][1]==ch) && (g[2][1]==ch))|| ((g[0][2]==ch)&& (g[1][2]==ch)&& (g[2][2]==ch))|| //Diagonal Sections ((g[0][0]==ch)&&(g[1][1]==ch)&& (g[2][2]==ch))||((g[0][2]==ch)&& (g[1][1]==ch)&& (g[2][0]==ch)) ) return -1; return 1; } //Main Execution int main( ) { char game[3][3]; int row, col; int count=0; int result; int end; cout << "\t\t\tTic-Tac-Toe Game\n"; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) game [i][j]='*'; do { cout << "\n\n\tPlayer " << (( count % 2)); cout << "\n\tEnter row: "; cin >> row; cout << "\tEnter column: "; cin >> col; //Play! result=check(game, row-1, col-1, count % 2); if ( result == -1) { cout << "\n\n\t Player " << (( count % 2) + 1) << "Wins! Congratulaions =) \n"; break; } } while (end == 0); return 0; }
Comment