#include using namespace std; void printBoard(int b[][3], int rows) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (b[i][j] == 0) cout << "-"; else if (b[i][j] == 1) cout << "X"; else if (b[i][j] == 2) cout << "O"; cout << "\t"; } cout << endl; } } bool checkWinner(int b[][3], int rows, int &who) { for (int i = 0; i < 3; i++) { if (b[i][0] == b[i][1] && b[i][1] == b[i][2] && b[i][0] != 0) { who = b[i][0]; return true; } } for (int j = 0; j < 3; j++) { if (b[0][j] == b[1][j] && b[1][j] == b[2][j] && b[0][j] != 0) { who = b[0][j]; return true; } } if (b[0][2] == b[1][1] && b[1][1] == b[2][0] && b[0][2] != 0) { who = b[0][2]; return true; } if (b[0][0] == b[1][1] && b[1][1] == b[2][2] && b[0][0] != 0) { who = b[0][0]; return true; } return false; } void calculate(int b[][3], int& r, int& c) { if (b[1][1] == 0) { r = 1; c = 1; } else for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (b[i][j] == 0) { r = i; c = j; } } } } int main() { int board[3][3] = { 0 }; int player = 1, row, col, winner=0, who; //Representing empty spaces by 0, X by 1 and O by 2 for (int i = 1; i <= 9 && winner==0; i++) { if (player == 1) { calculate(board, row, col); board[row][col] = 1; player = 2; } else { cout << "Player 2 Enter Location (Row and Column, respectively)\n"; cin >> row >> col; while (board[row][col] != 0) { cout << "Re-enter Location\n"; cin >> row >> col; } board[row][col] = 2; player = 1; } winner = checkWinner(board, 3, who); printBoard(board, 3); cout << endl; if (winner == 1) cout << "Player " << who << " is the Winner\n"; } system("pause"); return 0; }