Creating an Advanced Tic-Tac-Tac game using C++

Creating an Advanced Tic-Tac-Toe game using C++

Creating an Advanced Tic-Tac-Toe game using C++

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create an Advanced Tic-Tac-Toe game using C++.

I’m sure you all must have played this game in your childhood but for those who might have not – Let us understand What exactly is the Tic Tac Toe game?

Tic Tac Toe is a two-player game in which the objective is to take turns and mark the correct spaces in a 3×3 (or larger) grid. It’s a fun game in which the two players combat each other using Xs and Os.

How to win the game?

Tic-tac-toe is a game in which two players, X and O, alternately fill the hash (#) shaped box (consisting of two vertical lines crossing two horizontal lines). The first person to fill the box with 3Xs or 3Os in a horizontal, vertical, or diagonal pattern wins the game. When none of the players succeeds in filling the boxes horizontally, vertically, or diagonally with 3Xs or 3Os, the game is declared a draw.

Wine Shop Management System using C++ (With Source Code)

=> In this project we will define functions, arrays and use control statements in C++ to create this fun project.

This is how Tic-tac-toe game looks in general-

Creating an Advanced Tic-Tac-Toe game using C++

We will be creating this kind of structure in C++ to play the Tic-Tac-Toe game

Creating an Advanced Tic-Tac-Tac game using C++ Source Code:-

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

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

void add(char a[3][3],char inp,char ch)
{
    int num=inp-49,row,col;
    row=num/3,col=num%3;
    a[row][col]=ch;
}

void disp(char a[3][3])
{
    cout<<"\n\t\tPress Esc anytime to quit the game\n\n\n\n";
    int i,j;
    for(i=0;i<3;i++)
    {
        cout<<"\t\t\t\t-------------\n\t\t\t\t";
        for(j=0;j<3;j++)
        {
            if(a[i][j]=='a') cout<<"|   ";
            else
                cout<<"| "<<a[i][j]<<" ";
        }
        cout<<"|"<<endl;
    }
    cout<<"\t\t\t\t-------------\n";
}

int check(char a[3][3],char inp)
{
    int num=inp-48,row,col;
    if(num<=0 || num>=10) return 0;
    num--;
    row=num/3;
    col=num%3;
    if(a[row][col]=='a') return 1;
    else return 0;
}

char gameover(char a[3][3])
{
    char winner='a';
    if(a[0][0]==a[0][1] && a[0][0]==a[0][2] && a[0][0]!='a') winner=a[0][0];
    if(a[1][0]==a[1][1] && a[1][0]==a[1][2] && a[1][0]!='a') winner=a[1][0];
    if(a[2][0]==a[2][1] && a[2][0]==a[2][2] && a[2][0]!='a') winner=a[2][0];
    if(a[0][0]==a[1][0] && a[0][0]==a[2][0] && a[0][0]!='a') winner=a[0][0];
    if(a[0][1]==a[1][1] && a[0][1]==a[2][1] && a[0][1]!='a') winner=a[0][1];
    if(a[0][2]==a[1][2] && a[0][2]==a[2][2] && a[0][2]!='a') winner=a[0][2];
    if(a[0][0]==a[1][1] && a[0][0]==a[2][2] && a[0][0]!='a') winner=a[0][0];
    if(a[0][2]==a[1][1] && a[0][2]==a[2][0] && a[0][2]!='a') winner=a[0][2];
    return winner;
}

int draw(char a[3][3])
{
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            if(a[i][j]=='a')
                return 0;
    return 1;
}

int main()
{
    cout<<"\n\n\n\n\t\t\tTic Tac Toe\n\n\n\t\tPress any key to continue";
    getch();
    char a[3][3],turn[2]={'X','O'},ch='X',inp,winner,res;
    do
    {
        a[0][0]=a[0][1]=a[0][2]=a[1][0]=a[1][1]=a[1][2]=a[2][0]=a[2][1]=a[2][2]='a';
        system("cls");
        disp(a);
        cout<<"\n\n\t\t\t"<<ch<<"'s Turn\n\n";
        int count=0;
        while(1)
        {
            inp=getch();
            system("cls");
            if(inp<=48 || inp>=58 || !check(a,inp))
            {
                disp(a);
                cout<<"\n\n\t\t\t"<<ch<<"'s Turn\n\n";
                cout<<"INVALID MOVE!!\n\n";
            }
            else
            {
                add(a,inp,ch);
                disp(a);
                winner=gameover(a);
                if(winner=='a')
                {
                    if(draw(a))
                    {
                        cout<<"\n\n\t\t\tMatch Drawn !!\n";
                        break;
                    }
                    ch=turn[(++count)%2];
                    cout<<"\n\n\t\t\t"<<ch<<"'s Turn\n\n";
                }
                else
                {
                    cout<<"\n\n\t\t\t"<<winner<<" Won !!\n";
                    break;
                }
            }		
        }
        getch();
        system("cls");
        cout<<"\n\n\n\n\t\t\tWant to play more ? (Y/N) : ";
        cin>>res;
    }while(res=='y' || res=='Y');
}

Now let us understand the code:-

  • After writing the header of the code with the required libraries, we will create the following functions- add(), disp(), check(), gameover(), draw() and the main() function.
  • Now we will define the functions.
  • The add() function with void as data type as it doesn’t return a value. It takes a character-type 3*3 array for the tic-tac-toe, which will be created inside the main function, 2 other char type variables as arguments to define the functionality.
  • The disp() function contains the functionality to displays the (#) like structure to display the tic-tac-toe game structure. It uses a nested for-loop with upto 3 iterations each. It takes the array as an argument.
  • We will now create a function called check() with int as data type which will check if a box for the X or O insertion is empty or not.
  • The gamover() function returns a character type value, that is why char data-type is used for this function. It describes the functionality to see if any of the player has won the game or not,if won then it ends the game and declare the winner. Hence it returns winner=’a’.
  • The draw() function checks if the game has come out to be a draw, meaning no player has won the game.
  • Now finally the main() function which lays out how all the functions will be called upon users interaction with the game. It starts off by printing the game name “Tic Tac Toe” and “Press any key to continue”.
  • Inside main() function we will declare the arrays for the game structure and turns for the players as well as other variables which will be further required.
  • system(“cls”); – Used to clear the screen
  • We will use the do-while loop along with nested if-else control statements inside the main function.
  • At last the program will ask the user “Want to play more ? (Y/N) : “.
  • The user will type in the result – Y or N to either contine playing or declining respectively.
  •  This sums up our project of Tic Tac Toe game using C++.

Creating a Casino Game using C++

 

Output:

Creating an Advanced Tic-Tac-Tac game using C++

Upon pressing any key the game structure will be visible on the console.

Creating an Advanced Tic-Tac-Tac game using C++

The game will be played using the number keys. The X is shown at position 1, entered using number key 1. The next turn for O.

Creating an Advanced Tic-Tac-Tac game using C++

Enter O by pressing the number key 2 for it to be placed at 2nd position on the grid. For example- the user wants to place the O in 2nd column and 2nd row,i.e 5th position on the grid, then user will click on number key 5.

Creating an Advanced Tic-Tac-Tac game using C++

Upon further playing the game – O won.

Creating an Advanced Tic-Tac-Tac game using C++

Conclusion

We have reached the end of this article and by now you would have known how to create and play the tic tac toe game in C++. We have a lot more projects in C++ coming up for you all so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Simple Clinic Management System using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

ADVERTISEMENT

follow: CodeWithRandom

ADVERTISEMENT



Leave a Reply