CPP

Developing a Simple Tic-Tac-Toe Game in C++


In this guide, we'll create a simple console-based Tic-Tac-Toe game in C++. Tic-Tac-Toe is a classic two-player game where the objective is to get three of your marks (either 'X' or 'O') in a row, column, or diagonal on a 3x3 grid. We'll provide step-by-step instructions and sample code to help you build your own game.

Step 1: Define the Game Board

Start by defining a 3x3 game board to represent the Tic-Tac-Toe grid. You can use a two-dimensional array to accomplish this:

#include <iostream>
using namespace std;
char board[3][3] = {
    {' ', ' ', ' '},
    {' ', ' ', ' '},
    {' ', ' ', ' '}
};
    

Step 2: Display the Game Board

Create a function to display the current state of the game board. You can use nested loops to iterate through the array and print the grid:

void displayBoard() {
    cout << `  1 2 3` << endl;
    for (int i = 0; i < 3; i++) {
        cout << i + 1;
        for (int j = 0; j < 3; j++) {
            cout << `|` << board[i][j];
        }
        cout << `|` << endl;
    }
}
    

Step 3: Implement Player Moves

Create functions to handle player moves. You can alternately assign 'X' and 'O' to the grid based on the player's input:

void makeMove(int row, int col, char player) {
    if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
        board[row][col] = player;
    }
}
char switchPlayer(char currentPlayer) {
    return (currentPlayer == 'X') ? 'O' : 'X';
}
    

Step 4: Check for a Win

Create a function to check if a player has won the game by getting three marks in a row, column, or diagonal:

bool checkWin(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) ||
        (board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
        return true;
    }
    return false;
}
    

Step 5: Implement the Game Loop

Create the main game loop, where players take turns to make moves and the game continues until there's a win or a tie:

int main() {
    char currentPlayer = 'X';
    int row, col;
    bool gameWon = false;
    int totalMoves = 0;
    while (!gameWon && totalMoves < 9) {
        displayBoard();
        cout << `Player ` << currentPlayer << `, enter your move (row and column): `;
        cin >> row >> col;
        row--;
        col--;
        if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
            makeMove(row, col, currentPlayer);
            gameWon = checkWin(currentPlayer);
            currentPlayer = switchPlayer(currentPlayer);
            totalMoves++;
        } else {
            cout << `Invalid move. Try again.` << endl;
        }
    }
    displayBoard();
    if (gameWon) {
        cout << `Player ` << currentPlayer << ` wins!` << endl;
    } else {
        cout << `It's a tie!` << endl;
    }
    return 0;
}
    

Step 6: Build and Play the Game

Compile your C++ program and run the Tic-Tac-Toe game. Players can take turns to make moves, and the game will announce the winner or declare a tie when the game ends.

Conclusion

Developing a simple Tic-Tac-Toe game in C++ is a great way to practice programming logic and array manipulation. You can expand on this project by adding features like a replay option, an improved user interface, and a more sophisticated win-checking algorithm.

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.