Tic Tac Toe Game in C++ Using Array

Create Tic Tac Toe C++ Code Using Arrays

Create Tic Tac Toe C++ Code Using Arrays

Tic Tac Toe C++ Code Using Arrays
Build Tic Tac Toe Game in C++ Using Array

Tic Tac Toe Game in C++

Hey, fellow coder. Welcome to Codewithrandom’s world of coding. Today we will learn how to make a Tic Tac Toe Game Using C++ With Arrray. Every coder, be it an amateur or a professional in any language would have always wanted to make a Tic Tac Toe Game in C++.

Before we start with our project, let’s understand what a tic-tac-toe game is in 2D and what the importance of building this type of project is.

What is a Tic-Tac-Toe game?

A tic-tac-toe is a multiple-player game in which two people play the game at the same time, competing with each other. In tic-tac-Toe, there will be two symbols, “0” and “X”—one for each player—and a board made up of three-by-three table squares. Then, in the 3×3 square matrix, the player can add their symbol inside the square, and whichever player marks all three symbols in the same direction, whether horizontal, vertical, or diagonal, whoever completes it first wins the game.

What is the Importance of Tic-Tac-Toe Game?

Tic-Tac-Toe is an analytical game that uses different strategies and helps increase the user’s critical thinking power. It helps the user think differently and provides a solution for the problem. Working on this type of project lets users enhance their thinking abilities and helps in creating unique projects.

Well, today is your lucky day. I will be teaching you how to build the tic-tac-toe game from scratch using the very basics of C++ and nothing else.

Build a Hangman Game in C++(Source Code)

Let’s get into it.

Code byn/a
Project Downloadn/a
Language usedC++
External link / DependenciesNo
ResponsiveNo
Tic Tac Toe Game in C++ Table

Electricity Bill Generator Project In C++(Source Code)

How to win?

A player is said to have won the game, if,

  • A row has the same letters in all three spaces of that column.
  • A column has the same letter in all three spaces of that column.
  • A diagonal has the same letter in all three spaces of that column.

The first player to achieve at least one of the above conditions is the winner of the game.

Tic Tac Toe Game in C++ Using Array
Tic Tac Toe Game in C++ Using Array

NOTE

  • A player cannot undo his last move.
  • A player cannot place the letter in an already placed space.

Using these conditions, we will design the working of the program. 

Concepts to be known

Before starting a project, we have to see if we know the core concepts we use to build it. So, let’s take a look at them.

ADVERTISEMENT

  • Basic conditional statements and loops
  • Arrays
  • Functions
  • Operations 
  • Conversion of char to number.

ADVERTISEMENT

 

ADVERTISEMENT

ADVERTISEMENT

Creating a Fast Food Ordering System using C++ (Source Code)

ADVERTISEMENT

 Tic Tac Toe C++ Code:-

1:    
2:  #include<bits/stdc++.h>  
3:  #include<conio.h>  
4:  using namespace std;  
5:    
6:  void add(char a[3][3],char inp,char ch)  
7:  {  
8:       int num=inp-49,row,col;  
9:       row=num/3,col=num%3;  
10:       a[row][col]=ch;  
11:  }  
12:    
13:  void disp(char a[3][3])  
14:  {  
15:       cout<<"nttPress Esc anytime to quit the gamennnn";  
16:       int i,j;  
17:       for(i=0;i<3;i++)  
18:       {  
19:            cout<<"tttt-------------ntttt";  
20:            for(j=0;j<3;j++)  
21:            {  
22:                 if(a[i][j]=='a') cout<<"|  ";  
23:                 else  
24:                      cout<<"| "<<a[i][j]<<" ";  
25:            }  
26:            cout<<"|"<<endl;  
27:       }  
28:       cout<<"tttt-------------n";  
29:  }  
30:    
31:  int check(char a[3][3],char inp)  
32:  {  
33:       int num=inp-48,row,col;  
34:       if(num<=0 || num>=10) return 0;  
35:       num--;  
36:       row=num/3;  
37:       col=num%3;  
38:       if(a[row][col]=='a') return 1;  
39:       else return 0;  
40:  }  
41:    
42:  char gameover(char a[3][3])  
43:  {  
44:       char winner='a';  
45:       if(a[0][0]==a[0][1] && a[0][0]==a[0][2] && a[0][0]!='a') winner=a[0][0];  
46:       if(a[1][0]==a[1][1] && a[1][0]==a[1][2] && a[1][0]!='a') winner=a[1][0];  
47:       if(a[2][0]==a[2][1] && a[2][0]==a[2][2] && a[2][0]!='a') winner=a[2][0];  
48:       if(a[0][0]==a[1][0] && a[0][0]==a[2][0] && a[0][0]!='a') winner=a[0][0];  
49:       if(a[0][1]==a[1][1] && a[0][1]==a[2][1] && a[0][1]!='a') winner=a[0][1];  
50:       if(a[0][2]==a[1][2] && a[0][2]==a[2][2] && a[0][2]!='a') winner=a[0][2];  
51:       if(a[0][0]==a[1][1] && a[0][0]==a[2][2] && a[0][0]!='a') winner=a[0][0];  
52:       if(a[0][2]==a[1][1] && a[0][2]==a[2][0] && a[0][2]!='a') winner=a[0][2];  
53:       return winner;  
54:  }  
55:    
56:  int draw(char a[3][3])  
57:  {  
58:       for(int i=0;i<3;i++)  
59:            for(int j=0;j<3;j++)  
60:                 if(a[i][j]=='a')  
61:                      return 0;  
62:       return 1;  
63:  }  
64:    
65:  int main()  
66:  {  
67:       cout<<"nnnntttTic Tac ToennnttPress any key to continue";  
68:       getch();  
69:       char a[3][3],turn[2]={'X','O'},ch='X',inp,winner,res;  
70:       do  
71:       {  
72:            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';  
73:            system("cls");  
74:            disp(a);  
75:            cout<<"nnttt"<<ch<<"'s Turnnn";  
76:            int count=0;  
77:            while(1)  
78:            {  
79:                 inp=getch();  
80:                 system("cls");  
81:                 if(inp<=48 || inp>=58 || !check(a,inp))  
82:                 {  
83:                      disp(a);  
84:                      cout<<"nnttt"<<ch<<"'s Turnnn";  
85:                      cout<<"INVALID MOVE!!nn";  
86:                 }  
87:                 else  
88:                 {  
89:                      add(a,inp,ch);  
90:                      disp(a);  
91:                      winner=gameover(a);  
92:                      if(winner=='a')  
93:                      {  
94:                           if(draw(a))  
95:                           {  
96:                                cout<<"nntttMatch Drawn !!n";  
97:                                break;  
98:                           }  
99:                           ch=turn[(++count)%2];  
100:                           cout<<"nnttt"<<ch<<"'s Turnnn";  
101:                      }  
102:                      else  
103:                      {  
104:                           cout<<"nnttt"<<winner<<" Won !!n";  
105:                           break;  
106:                      }  
107:                 }            
108:            }  
109:            getch();  
110:            system("cls");  
111:            cout<<"nnnntttWant to play more ? (Y/N) : ";  
112:            cin>>res;  
113:       }while(res=='y' || res=='Y');  
114:  }  

Code Explanation:-
 
From the code above let’s break it down for better understanding. For starters, we have a few functions defined that we will use in the main function when needed.

Some functions for declaring the winner, checking the grid, etc are present before the main function.

Tic Tac Toe Game in C++ Using Array
Tic Tac Toe Game in C++ Using Array

The add() function

 We use add function to add the character at the spot we need on the grid. So we pass the array i.e, the grid, the char which will be switching automatically.

We calculate the row and column using the input we take as the parameter.

Clinic Management System in C++

 The disp() function

 The disp() function is used to display the value inside the grid. We use if-else to fill the grid cell either with the space or character we assigned. The grid updates every time we give an input.

 The check() function

In the check() function our task is to see that the user only enters the numbers between 1-9 so we take the input as a number which is then used to calculate the row and column value. We reject any other value by saying “INVALID MOVE”.

Creating a Snake Game using C++ (With Source Code)

The gameover() function 

As we discussed in the start the conditions for winning, we implement it here. We mainly see that the cell in a row or column is not empty and are equal values (X or O). 

Like this for every row and column and the diagonals too.

Then winner variable is assigned the value of the cell value we compare with other cells in that row.

The draw() function 

So, what happens when the conditions we mentioned don’t take place. For that in the gameover function, we assign the variable “winner” value as “a” which is a placeholder used at different parts of program. That value is used here and if the winner variable stays the same, i.e “a” then in the main function we print a statement for DRAW status.

Conclusion:

We have reached the end of the article but, we have a lot more projects in C++ coming. This is just to start an interesting and fun project.

If you have enjoyed the article and learned something new today, then let us know in the comments. Check out some more articles here.  

Visit our Instagram Page for more amazing content on Web development.

Thank you.

Happy Reading 🙂

Which code editor do you use for this Tic Tac Toe Game?

I personally recommend using VS Code Studio, it’s straightforward and easy to use.
 

What is a Tic-Tac-Toe game?

A tic-tac-toe is a multiple-player game in which two people play the game at the same time, competing with each other. In tic-tac-Toe, there will be two symbols, “0” and “X”—one for each player—and a board made up of three-by-three table squares. Then, in the 3×3 square matrix, the player can add their symbol inside the square, and whichever player marks all three symbols in the same direction, whether horizontal, vertical, or diagonal, whoever completes it first wins the game.

What is the Importance of Tic-Tac-Toe Game?

Tic-Tac-Toe is an analytical game that uses different strategies and helps increase the user’s critical thinking power. It helps the user think differently and provides a solution for the problem. Working on this type of project lets users enhance their thinking abilities and helps in creating unique projects.



Leave a Reply