C++ Code Game Tic Tac Toe

7 min read Jul 01, 2024
C++ Code Game Tic Tac Toe

C++ Code for a Tic Tac Toe Game

This article provides a basic C++ implementation of a Tic Tac Toe game. This implementation focuses on the core logic of the game and allows players to take turns marking the board until a winner is determined or a draw occurs.

Game Logic and Structure

The Tic Tac Toe game logic is implemented using a 2D array to represent the game board. Each cell in the array corresponds to a square on the board. The values in the array represent the state of each square:

  • 0: Empty square
  • 1: Player 1's mark (X)
  • 2: Player 2's mark (O)

The game loop iterates through turns, allowing each player to mark a square on the board. After each turn, the game checks for a win condition or a draw.

C++ Code

#include 

using namespace std;

// Function to display the Tic Tac Toe board
void displayBoard(char board[][3]) {
    cout << "-------------" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "| ";
        for (int j = 0; j < 3; j++) {
            cout << board[i][j] << " | ";
        }
        cout << endl << "-------------" << endl;
    }
}

// Function to check for a win condition
bool checkWin(char board[][3], int player) {
    // Check rows
    for (int i = 0; i < 3; i++) {
        if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
            return true;
        }
    }

    // Check columns
    for (int j = 0; j < 3; j++) {
        if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
            return true;
        }
    }

    // Check diagonals
    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;
}

// Function to check if the board is full
bool isBoardFull(char board[][3]) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == ' ') {
                return false;
            }
        }
    }
    return true;
}

int main() {
    char board[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
    int player = 1; // Start with player 1

    while (true) {
        displayBoard(board);

        int row, col;
        cout << "Player " << player << " (";
        if (player == 1) {
            cout << "X";
        } else {
            cout << "O";
        }
        cout << "): Enter row (1-3) and column (1-3): ";
        cin >> row >> col;

        // Adjust input to array indices
        row--;
        col--;

        // Check if the selected square is empty
        if (board[row][col] == ' ') {
            board[row][col] = (player == 1) ? 'X' : 'O';

            // Check for win condition
            if (checkWin(board, player)) {
                displayBoard(board);
                cout << "Player " << player << " wins!" << endl;
                break;
            }

            // Check for a draw
            if (isBoardFull(board)) {
                displayBoard(board);
                cout << "It's a draw!" << endl;
                break;
            }

            // Switch players
            player = (player == 1) ? 2 : 1;
        } else {
            cout << "Invalid move. Please choose an empty square." << endl;
        }
    }

    return 0;
}

Explanation

  • displayBoard(char board[][3]): This function takes the game board as input and prints it to the console in a visually appealing format.
  • checkWin(char board[][3], int player): This function checks the board for win conditions (three consecutive marks in a row, column, or diagonal) for the specified player.
  • isBoardFull(char board[][3]): This function checks if all squares on the board are filled. If so, it indicates a draw.
  • main(): This is the main function where the game loop is implemented. It initializes the board, manages player turns, updates the board based on player input, checks for win conditions and draws, and prints the final result.

Further Improvements

This basic implementation can be expanded upon by adding features like:

  • Player names: Allow players to enter their names for a more personalized experience.
  • Computer AI: Implement an AI opponent to play against.
  • Graphical interface: Create a more visually appealing graphical user interface (GUI) instead of using console output.
  • Multiplayer: Allow two human players to compete against each other over a network.

This simple C++ Tic Tac Toe code provides a solid foundation for creating a fun and engaging game. You can customize and extend it further to create a more complex and feature-rich version.

Latest Posts