#include #include #include using namespace std; void printBoard(const vector>& board) { system("cls"); // Clear the console cout << " 0 1 2\n"; cout << " -------------\n"; for (int i = 0; i < 3; i++) { cout << i << " | "; for (int j = 0; j < 3; j++) { cout << board[i][j] << " | "; } cout << "\n -------------\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; } int main() { vector> board(3, vector(3, ' ')); char turn = 'X'; int x = 0, y = 0; bool gameEnded = false; while (!gameEnded) { printBoard(board); cout << "Player " << turn << ", use arrow keys to move, space to place. Current position: (" << y << ", " << x << ")" << endl; int ch = _getch(); // Use _getch() to get keypress switch (ch) { case 0: case 224: // Handle arrow keys switch (_getch()) { // Read the second character of arrow key sequence case 72: y = max(0, y - 1); break; // Up case 80: y = min(2, y + 1); break; // Down case 75: x = max(0, x - 1); break; // Left case 77: x = min(2, x + 1); break; // Right } break; case ' ': // Place a mark on the board with space if (board[y][x] == ' ') { board[y][x] = turn; if (isWin(board, turn)) { printBoard(board); cout << "Player " << turn << " wins!" << endl; gameEnded = true; } else if (isBoardFull(board)) { printBoard(board); cout << "It's a draw!" << endl; gameEnded = true; } turn = (turn == 'X') ? 'O' : 'X'; } break; } } return 0; }