#include <iostream>
#include <vector>
using namespace std;
void printBoard(const vector<vector<char>>& 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<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;
}
bool placeMarker(int row, int col, vector<vector<char>>& board, char marker) {
if (board[row][col] == ' ') {
board[row][col] = marker;
return true;
}
return false;
}
int main() {
vector<vector<char>> board(3, vector<char>(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;
}
{"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"}