#include #include using namespace std; void printBoard(const vector>& board) { cout << " 0 1 2\n"; cout << " -------------\n"; for (int i = 0; i < 3; i++) { cout << i << " | " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << " |" << endl; cout << " -------------\n"; } } bool isWin(const vector>& board, char player) { for (int i = 0; i < 3; i++) { if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return true; if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true; } if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true; if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return true; return false; } bool isBoardFull(const vector>& board) { for (const auto& row : board) { for (char cell : row) { if (cell == ' ') return false; } } return true; } bool placeMarker(int row, int col, vector>& board, char marker) { if (board[row][col] == ' ') { board[row][col] = marker; return true; } return false; } int main() { vector> board(3, vector(3, ' ')); char turn = 'X'; int row, col; bool gameEnded = false; while (!gameEnded) { printBoard(board); cout << "Player " << turn << ", enter row and column to place your marker: "; cin >> row >> col; if (row < 0 || row >= 3 || col < 0 || col >= 3) { cout << "Invalid position! Try again.\n"; continue; } if (!placeMarker(row, col, board, turn)) { cout << "This position is already occupied! Try again.\n"; continue; } if (isWin(board, turn)) { printBoard(board); cout << "Player " << turn << " wins!\n"; gameEnded = true; } else if (isBoardFull(board)) { printBoard(board); cout << "It's a draw!\n"; gameEnded = true; } else { turn = (turn == 'X') ? 'O' : 'X'; } } return 0; }