c-plus-plus
  1. c-plus-plus-tic-tac-toe

C++ Programs - Tic-Tac-Toe

Tic-Tac-Toe is a simple game that can be played by two players on a 3x3 grid. Players take turns placing their symbol (X or O) on the grid, and the first player to get three in a row wins.

Syntax

void displayBoard();
bool isGameOver();
bool isValidMove(int row, int col);
bool makeMove(int row, int col, char symbol);
void playGame();

Example

#include <iostream>
using namespace std;

char board[3][3];

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

bool isGameOver() {
    // row-wise check
    for(int i=0; i<3; i++) {
        if(board[i][0]!=' ' && board[i][0]==board[i][1] && board[i][1]==board[i][2]) {
            return true;
        }
    }
    // column-wise check
    for(int j=0; j<3; j++) {
        if(board[0][j]!=' ' && board[0][j]==board[1][j] && board[1][j]==board[2][j]) {
            return true;
        }
    }
    // diagonal check
    if(board[0][0]!=' ' && board[0][0]==board[1][1] && board[1][1]==board[2][2]) {
        return true;
    }
    if(board[2][0]!=' ' && board[2][0]==board[1][1] && board[1][1]==board[0][2]) {
        return true;
    }
    // game is not over yet
    return false;
}

bool isValidMove(int row, int col) {
    if(row>=0 && row<3 && col>=0 && col<3 && board[row][col]==' ') {
        return true;
    }
    return false;
}

bool makeMove(int row, int col, char symbol) {
    if(isValidMove(row, col)) {
        board[row][col] = symbol;
        return true;
    }
    return false;
}

void playGame() {
    char player1 = 'X';
    char player2 = 'O';
    char currentPlayer = player1;
    int row, col;

    while(!isGameOver()) {
        displayBoard();
        cout << "Player " << currentPlayer << ", enter row and column: ";
        cin >> row >> col;

        if(makeMove(row, col, currentPlayer)) {
            if(currentPlayer == player1) {
                currentPlayer = player2;
            } else {
                currentPlayer = player1;
            }
        } else {
            cout << "Invalid move! Try again..." << endl;
        }
    }

    displayBoard();
    cout << "Player " << (currentPlayer==player1?player2:player1) << " wins!" << endl;
}

int main() {
    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            board[i][j] = ' ';
        }
    }

    playGame();

    return 0;
}

Output

  X         
           
           
Player O, enter row and column: 0 1
  X O       
           
           
Player X, enter row and column: 1 1
  X O       
   X       
           
Player O, enter row and column: 2 1
  X O       
   X       
  O        
Player X, enter row and column: 0 0
X X O       
   X       
  O        
Player O, enter row and column: 0 2
X X O       
   X       
O O        
Player X, enter row and column: 1 0
X X O       
X  X       
O O        
Player O, enter row and column: 1 2
X X O       
X  X O     
O O        
Player X enter row and column: 2 0
X X O       
X  X O     
X O        
Player X wins!

Explanation

In the above example, we have defined a Tic-Tac-Toe game using a 2D char array to represent the game board. We have also defined several functions to handle different aspects of the game, such as displaying the board, checking if the game is over, validating user input, and making a move.

The main function initializes the game board and calls the playGame function, which starts the game loop. In each iteration of the loop, the current player is asked to enter a row and column for their move. The move is validated using the isValidMove function, and if it is valid, the move is made using the makeMove function.

After each move, the game board is displayed and the current player switches to the other player. This continues until the isGameOver function returns true, at which point the game is complete and the winner is displayed.

Use

Tic-Tac-Toe is a simple yet effective example of a game that can be implemented in C++. This code can be used to learn basic programming concepts such as arrays, functions, loops, and user input. It can also be extended to add more features, such as graphical display and AI opponents.

Important Points

  • Tic-Tac-Toe is a simple game that can be implemented using a 2D array in C++.
  • The game logic can be organized using functions such as displayBoard, isGameOver, isValidMove, and makeMove.
  • The game loop can be implemented using a while loop that continues until the game is over.
  • User input can be handled using the cin function, with validation using the isValidMove function.
  • The game can be extended with additional features, such as graphical display and AI opponents.

SummaryTic-Tac-Toe is a classic game can be implemented in C++ using a 2D array and a series of functions to handle different aspects the game. This example can be used to teach basic programming concepts and can be extended to add more features complexity.

Published on: