Limited Period Offer : 20% Discount on online/offline courses, for more details call/whatsapp

Tic-Tac-Toe Game

1 min read
1 year ago By Aniket Prajapati

Here is the tic-tac-toe game using c++ language the code is optimized and this article will help you to get logic about how to run functions without any confusion.

#include <bits/stdc++.h>

using namespace std;


bool isGameOver(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;


    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            if (board[i][j] != 'X' && board[i][j] != 'O') return false;
        }
    }
    return true;
}


void printBoard(const vector<vector<char>>& board) {
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << board[i][j];
            if (j < 2) cout << " | ";
        }
        cout << endl;
        if (i < 2) cout << "---------" << endl;
    }
}

int main() {
    vector<vector<char>> board(3, vector<char>(3, ' '));
    bool player1Turn = true;

    cout << "Welcome to Tic-Tac-Toe! Game" << endl;

    while (true) {

        printBoard(board);


        char currentPlayer = (player1Turn) ? 'X' : 'O';

        cout << "Player " << currentPlayer << ", Enter your move : ";
        int row, col;
        cin >> row >> col;


        if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') {
            cout << "Invalid move. Try again." << endl;
            continue;
        }

        board[row][col] = currentPlayer;

        if (isGameOver(board, currentPlayer)) {
            printBoard(board);
            cout << "Player " << currentPlayer << " wins!" << endl;
            break;
        }

        player1Turn = !player1Turn; // Switch to the other player's turn.
    }

    cout << "Game over." << endl;

    return 0;
}
Sep 15, 2023 21:57 Back to Articles

Other Articles

Real DOM VS Virtual DOM

In this article, we will discuss the differences between the real DOM and the virtual DOM in the context of web development. We'll explore how these concepts impact the performance and efficiency of web applications, and how they contribute to the overall user experience. By the end of this article, you'll have a clear understanding of the distinctions between these two approaches to managing and updating user interfaces on the web.

1 year ago By Mitali Gupta
The Importance of UX Design in Software Development The Importance of UX Design in Software Development

UX design is vital for user engagement, satisfaction, and the success of software applications in competitive markets.

2 years ago By Mitali Gupta
EdKool.com Launches: A New Destination for All Things Education & Technology

We're delighted to announce the launch of EdKool.com, a unique platform designed to bridge the gap between education and technology.

2 years ago By Santosh Kshirsagar
Binary Search top interview questrions.

As we know binary search itself is a imp topic to be learned in Data Structures and Algorithm so, This article contains all the important questions that can be asked in coding interview or online assessment(OA) . Here the questions clear all the concepts and the variety in questions helps to understands the logic behind the concepts .

1 year ago By Aniket Prajapati