#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;
void printBoard(const vector<vector<char>>& 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<vector<char>>& 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<vector<char>>& board) {
for (const auto& row : board) {
for (char cell : row) {
if (cell == ' ') return false;
}
}
return true;
}
int main() {
vector<vector<char>> board(3, vector<char>(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;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}