online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <iostream> #include <vector> using namespace std; // Function prototypes void printBoard(const vector<vector<char>> &board); bool isMoveValid(const vector<vector<char>> &board, int row, int col); bool isGameOver(const vector<vector<char>> &board, char &winner); void makeMove(vector<vector<char>> &board, int row, int col, char currentPlayer); int main() { vector<vector<char>> board(3, vector<char>(3, ' ')); // 3x3 game board char currentPlayer = 'X'; char winner = ' '; cout << "Welcome to Tic-Tac-Toe!\n"; do { printBoard(board); int row, col; cout << "Player " << currentPlayer << ", enter your move (row and column): "; cin >> row >> col; if (isMoveValid(board, row, col)) { makeMove(board, row, col, currentPlayer); if (isGameOver(board, winner)) { break; } currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; // Switch player } else { cout << "Invalid move. Try again.\n"; } } while (true); printBoard(board); if (winner != ' ') { cout << "Player " << winner << " wins!\n"; } else { cout << "It's a draw!\n"; } return 0; } void printBoard(const vector<vector<char>> &board) { cout << " 0 1 2\n"; for (int i = 0; i < 3; ++i) { cout << i << " "; for (int j = 0; j < 3; ++j) { cout << board[i][j] << " "; } cout << endl; } } bool isMoveValid(const vector<vector<char>> &board, int row, int col) { return (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' '); } bool isGameOver(const vector<vector<char>> &board, char &winner) { // Check rows, columns, and diagonals for a winner for (int i = 0; i < 3; ++i) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { winner = board[i][0]; return true; } if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { winner = board[0][i]; return true; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { winner = board[0][0]; return true; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') { winner = board[0][2]; return true; } // Check for a draw for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (board[i][j] == ' ') { return false; // The game is not over yet } } } // If no winner and no empty spaces, it's a draw return true; } void makeMove(vector<vector<char>> &board, int row, int col, char currentPlayer) { board[row][col] = currentPlayer; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue