Tic-Tac-Toe program in C++

Tic-Tac-Toe program in C++

Tic Tac Toe is a game where two players play using X and O. The player who first fills the box with 3X’s or 3O’s in a horizontal, vertical, or diagonal way wins the game. If no one fills then its a draw.

Output:

tic tac toe output

Code:

#include<iostream>
#include<array>
using namespace std;
char arr[10] = {'o','1','2','3','4','5','6','7','8','9'};
int win();
display(){
    system("CLS");
    cout<<"\n\tTIC - TAC - TOE!\n";
    cout<<"===============================\n\n";
    cout<<"\t     |     |   \n";
    cout<<"\t   "<<arr[1]<<" |  "<<arr[2]<<"  | "<<arr[3]<<" \n";
    cout<<"\t ____|_____|____\n";
    cout<<"\t     |     |   \n";
    cout<<"\t   "<<arr[4]<<" |  "<<arr[5]<<"  | "<<arr[6]<<" \n";
    cout<<"\t ____|_____|____\n";
    cout<<"\t     |     |   \n";
    cout<<"\t   "<<arr[7]<<" |  "<<arr[8]<<"  | "<<arr[9]<<" \n";
    cout<<"\t     |     |   \n";
    cout<<"\n--------------------------------\n";
    return 0;
    }
int main(){
    int p = 1,i,c;

    char mark;
    do
    {
        display();
        p=(p%2)?1:2;
        cout << "Player  " << p << ", Enter your choice:  ";
        cin >> c;
        mark=(p == 1) ? 'X' : 'O';
        if (c == 1 && arr[1] == '1')
            arr[1] = mark;
        else if (c == 2 && arr[2] == '2')
            arr[2] = mark;
        else if (c == 3 && arr[3] == '3')
            arr[3] = mark;
        else if (c == 4 && arr[4] == '4')
            arr[4] = mark;
        else if (c == 5 && arr[5] == '5')
            arr[5] = mark;
        else if (c == 6 && arr[6] == '6')
            arr[6] = mark;
        else if (c == 7 && arr[7] == '7')
            arr[7] = mark;
        else if (c == 8 && arr[8] == '8')
            arr[8] = mark;
        else if (c == 9 && arr[9] == '9')
            arr[9] = mark;
        else
        {
            p--;
            cin.clear();
            cin.get();
        }
        i=win();
        p++;
    }while(i==-1);
    display();
    if(i==1)
        cout<<"\n Player "<<--p<<" wins!";
    else
        cout<<"\n Draw!";
    cin.ignore();
    cin.get();
    return 0;
}
int win()
{
    if (arr[1] == arr[2] && arr[2] == arr[3])
        return 1;
    else if (arr[4] == arr[5] && arr[5] == arr[6])
        return 1;
    else if (arr[7] == arr[8] && arr[8] == arr[9])
        return 1;
    else if (arr[1] == arr[4] && arr[4] == arr[7])
        return 1;
    else if (arr[2] == arr[5] && arr[5] == arr[8])
        return 1;
    else if (arr[3] == arr[6] && arr[6] == arr[9])
        return 1;
    else if (arr[1] == arr[5] && arr[5] == arr[9])
        return 1;
    else if (arr[3] == arr[5] && arr[5] == arr[7])
        return 1;
    else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3'
                    && arr[4] != '4' && arr[5] != '5' && arr[6] != '6'
                  && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
        return 0;
    else
        return -1;
}

Explanation-

The logic behind this is to display a tic-tac-toe game design with array elements denoting numbers to each box. We have to enter value from available numbers only. As this game is of 2 players hence X and O are marked differently checking which player’s turn it is. When a number is entered it checks its array location and replaces with X or O respectively. The game’s rule to mark 3X’s or 3O’s in a horizontal, vertical, or diagonal way is converted in if…else conditions. After each player’s turn the function win() checks if any of the required winning condition is fulfilled and returns 1 if yes. The game continues till the value of variable doesn’t become 1. Then it displays result as which player won.

Conclusion

So those were the steps to make tic tac toe game in C++. Share it with your programming buddies and friends. If you have any doubt, mention in the comment section. And for daily updates follow us on Instagram.

Visit again for more such programs!

ErrorFreeProgram.

2 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *