tic tac toe c++

#include<bits/stdc++.h>
using namespace std;

static char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}, ch1;
string p1, p2;
int r, c;
int alt = 0;

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

void printBoard(){
    system("cls");
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(j == 0)
                cout << "\t\t\t";
            cout << " " << board[i][j] << " ";
            if(j != 2)
                cout << "|";
        }
        cout << "\n";
        if(i != 2)
            cout << "\t\t\t-----------\n";
    }
}

bool isWinner(char ch4){
    return ((board[0][0] == ch4 && board[0][1] == ch4 && board[0][2] == ch4) ||
     (board[1][0] == ch4 && board[1][1] == ch4 && board[1][2] == ch4) ||
     (board[2][0] == ch4 && board[2][1] == ch4 && board[2][2] == ch4) ||
     (board[0][0] == ch4 && board[1][0] == ch4 && board[2][0] == ch4) ||
     (board[0][1] == ch4 && board[1][1] == ch4 && board[2][1] == ch4) ||
     (board[0][2] == ch4 && board[1][2] == ch4 && board[2][2] == ch4) ||
     (board[0][0] == ch4 && board[1][1] == ch4 && board[2][2] == ch4) ||
     (board[0][2] == ch4 && board[1][1] == ch4 && board[2][0] == ch4));
}

bool Boardfull(){
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            if(board[i][j] == ' ')
                return false;
    return true;
}

void makeMove(char c1){
    if(board[r-1][c-1] == ' ')
        board[r-1][c-1] = c1;
    else{
        cout << "\nThis Position is Already Occupied!\n\n";
        alt--;
    }
}

void playGame(){
    do{
        if(alt % 2 == 0){
            cout << endl << "\n" << p1 << "'s Turn\n";
            ch1 = 'X';
        }
        else{
            cout << endl << "\n" << p2 << "'s Turn\n";
            ch1 = 'O';
        }
        while(1){
            cout << "Enter Column No. you want to make move: ";
            cin >> c;
            if(c < 1 || c > 3)
                cout << "Enter Coulmn in Range 1 to 3\n";
            else
                break;
        }
        while(1){
            cout << "Enter Row No. you want to make move: ";
            cin >> r;
            if(r < 1 || r > 3)
                cout << "Enter Row in Range 1 to 3\n";
            else
                break;
        }
        makeMove(ch1);
        alt++;
        printBoard();
    }while(!isWinner(ch1) && !Boardfull());
}

int main(){
    char ch;
    system("cls");
    do{
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout << "\t\t\tWELCOME TO THE TIC-TAC-TOE GAME\n";
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout << "\nEnter First Player's first Name: ";
        cin >> p1;
        cout << "Symbol Assigned to " << p1 << " is 'X'\n";
        cout << "\nEnter Second Player's first Name: ";
        cin >> p2;
        cout << "Symbol Assigned to " << p2 << " is 'O'\n\n";
        printBoard();
        playGame();
        cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout << "\t\t\t\tRESULT: ";
        if(Boardfull() && !(isWinner('X') || isWinner('O')))
            cout << "Game Tie!\n";
        else if(alt % 2 == 0)
            cout << p2 << " Wins!\n";
        else
            cout << p1 << " Wins!\n";
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";
        cout << "\nDo You Want to Play Again? (Y / N): ";
        boardReset();
        alt = 0;
        cin >> ch;
    }while(ch != 'n' && ch != 'N');
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
tic tac toe efficiency using stl c++ code tic tac toe game in c++ project tic tac toe game in cpp tic tac toe algorithm c++ making a tic tac toe game in c++ tic tac toe c++ table algorithm for tic tac toe in c++ Tic Tac Toe C++ code using classes Tic Tac Toe C++ program against computer tic tac toe game code in c++ tic tac toe algorithm in c++ nxn tic tac toe c++ tic tac toe vs computer c++ tic tac toe against computer c++ tic tac toe gui c++ Tic tac toe in C++ using loops c++ tic tac toe vs computer tic tac toe ++ tic tac toe using OOP c++ 3D Tic Tac Toe c++ tic tac toe c++ example tic tac toe win condition c++ tic tac toe c++ code chef tic tac toe c++ description tic tac toe c++ vs computer tic tac toe game in c++ against computer make tic tac toe in c++ tic tac toe game cpp TIC TAC TOE USING C++ MINI MAX Tic Tac Toe AI C++ tic tac toe game using c++ tic tac toe using c++ tic tac toe in c++ using classes c++ skills needed for tic tac toe c++ simple tic tac toe ai tic tac toe c++ making tic tac toe in c++ c++ tic tac toe assignment make a tic tac toe game in c++ tic tac toe pattern c++ c++ tic tac toe AI beginner c++ tic tac toe tic tac toe code c++ create a tic tac toe game in c++ can we build a tic tac toe in c++ tic tac toe game c++ how to make a tic tac toe game in c++ graphics how hard is it to create a tic tac toe game c++ tic tac toe project cpp tic tac toe c++ gaddis tic tac toe c++ using asterisks tic tac toe c++ using asterisk tic tac toe game c++ program tic tac toe c++ program with explanation code for tic tac toe game in c++ tic tac toe game c plus plus tic tac toe game using c++ with classes how to create Tic tac Toe in C++ how to make tic tac toe in c++ online ide toc tac toe c++ tic tac toe cpp Tic-tac-toe using cpp Tic-tac-toe using c++ tic tac toe pseudocode is it easy to program a tic tac toe tic tac toe thp how to code tic tac toe in c++ code of tic tac toe game in c++ c++ code for tic tac toe tic tac toe game source code in c++ tic tack game code how to make an tic tac toe with c++ tic tac toe source code in cpp tica tac toe cpp tic tac toe in java using c metode game tic tac toe c++ Tic tac toe game in C++ tic tac toe dynamic programming design tic tac toe gfg tic toc cpp program TicTacToe c c++ how to make a tic tac toe in c++ programming logic for tic tac toe tic tac toe c++ program 2 player c++ tic tac toe game tik tok game in c++ code how to make tic tac toe in c++ writing a easy tic tac toe in c++ how to create a tic tac toe board in c++ tic tac toe c++ program tic tac toe code tic tac toe in c++ tic tac toe in c++ tic tac toe c++ code c++ tic tac toe tic tac toe program in c++ tic tac toe c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source