Let’s make a simple tic-tac-toe game in C++ | Tic Tac Toe Game In C++ Using Array







Let’s make a simple tic-tac-toe game in C++ | Tic Tac Toe Game In C++ Using Array

Let’s Make a Tic-Tac-Toe game using C++

Introduction

Hey, fellow coder. Welcome to the codewithrandom’s world of coding. Today we will learn how to make a tic-tac-toe game using C++. Every coder, be an amateur or a professional in any language would have always wanted to make a game in C++.

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.

Let’s get into it.


What is tic-tac-toe?

Tic-tac-toe is a two-player game where both players place a symbol of their own in a 3*3 grid. Usually, the marks we choose are the letters X and O. If one player takes X, the other player has to take O. This goes till the end of the game.


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.


Let's make a simple tic-tac-toe game in C++ | 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.

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


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.


The main function


In the main function, initially we implement a start screen which is proceeded with entering any key on the keyboard. 

Next, we declare the needed variables that include a multidimensional array for the grid and an array with characters X and O.

Then a do-while loop that has a while loop to make the game run unless we end it manually.


The main() function aims to use if-else statements to call the functions we defined above main() at suitable conditions to proceed at each step of the game.

Let’s see the functions we defined above the main().

   

  • void add(char a[3][3],char inp,char ch)
  • void disp(char a[3][3])
  • int check(char a[3][3],char inp)
  • char gameover(char a[3][3])
  • int draw(char a[3][3])

Let's make a simple tic-tac-toe game in C++ | 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.


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”.


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. 


1)Typing with Typo correction and pause in CSS 

2) Home50 project JavaScript Pairs Game


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

Thank you.

Happy Reading 🙂

Share on:

Leave a Comment