Tic Tac Toe Game In C++ | How to Use Function in C++

Tic Tac Toe Game In C++  | How to Use Function in C++.


Tic Tac Toe :

Tic tac toe is Children's educational game actually and helps in the mental growth of children. There are two players in this game who can play this game against each other.
Generally, it also is known as cross and notes games. It is usually played on the paper.

Tic Tac Toe in C++ :


we will make the Tic Tac toe game in C++ using Functions which will help us to understand the Functions also. We will also use the 2d arrays in making the Tic Tac Toe Game in C++.

Tic Tac Toe Game In C++  | How to Use Function in C++
Tic Tac Toe Game In C++  | How to Use Function in C++

Functions In C++:

There are two kinds of functions in C++ :
  1. User-defined Functions.
  2. Defined Functions.

User-defined Functions:

The Functions which are made by the user in the compiler and then wright the algorithm by its own are called the user-defined functions.

Defined Functions:

The Functions which are built-in functions which is available in the C++ library and you don't to write the algorithm of Functions. You just need to call the Functions.

C++ Code of Tic Tac Toe:

/*
#include<iostream>
using namespace std;
char table[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int user_input = 0, rows = 0, cols = 0;
char turn = 'X';
bool draw = false;
void display_table()
{
system("cls");
cout << endl;
cout << "***********************************************  T I C K   T A C   T O E  G A M E **************************************" << endl;
cout << endl;
cout << endl;
cout << "***********************************************THIS GAME WILL BE PLAYED BY TWO PERSNOS**********************************\n";
cout << endl;
cout << endl;
cout << "          The  two Player will be 'X'   and   'Y' \n";
cout << endl;
cout << endl;
cout << "                                                      " << "     |     |     \t" << endl;
cout << "                                                      " << table[0][0] << "    |  " << table[0][1] << "  |  " << table[0][2] << endl;
cout << "                                                      " << "_____|_____|_____\t" << endl;
cout << "                                                      " << "     |     |     \t" << endl;
cout << "                                                      " << table[1][0] << "    |  " << table[1][1] << "  |  " << table[1][2] << endl;
cout << "                                                      " << "_____|_____|_____\t" << endl;
cout << "                                                      " << "     |     |     \t" << endl;
cout << "                                                      " << table[2][0] << "    |  " << table[2][1] << "  |  " << table[2][2] << endl;
cout << "                                                      " << "     |     |     \t" << endl;
}
void player_turn()
{
if (turn == 'X')
cout << "          Player1[X] Turns    :";
if (turn == 'Y')
cout << "          Player2[Y] Turns    :";
cin >> user_input;
switch (user_input)
{
case 1:
rows = 0; cols = 0;
break;
case 2:
rows = 0; cols = 1;
break;
case 3:
rows = 0; cols = 2;
break;
case 4:
rows = 1; cols = 0;
break;
case 5:
rows = 1; cols = 1;
break;
case 6:
rows = 1; cols = 2;
break;
case 7:
rows = 2; cols = 0;
break;
case 8:
rows = 2; cols = 1;
break;
case 9:
rows = 2; cols = 2;
break;
default:
cout << "Invalid Input" << endl;
break;
}
if (turn == 'X'&&table[rows][cols] != 'X'&&table[rows][cols]!='Y')
{
table[rows][cols] = { 'X' };
turn = 'Y';
}
else if (turn == 'Y'&&table[rows][cols] != 'X'&&table[rows][cols] != 'Y')
{
table[rows][cols] = { 'Y' };
turn = 'X';
}
else
{
cout << "THIS BOX IS ALREADY TAKEN!!! please Try Again" << endl;
player_turn();
}
display_table();
}
bool game_over()
{
for (int i = 0; i < 3; i++)
{
if (table[i][0] == table[i][1] && table[i][0] == table[i][2] || table[0][i] == table[1][i] && table[0][i] == table[2][i])
{
return false;
}
}
if (table[0][0] == table[1][1] && table[0][0] == table[2][2] || table[0][2] == table[1][1] && table[0][2] == table[2][0])
{
return false;

}
for (int i = 0; i<3; i++)
{
for (int j = 0; j < 3; j++)
{
if (table[i][j] != 'X'&&table[i][j] != 'Y')
return true;
}
}
draw = true;
return false;
}
int main()
{
while (game_over())
{
display_table();
player_turn();
game_over();
cout << endl;
cout << endl;
}
if (turn == 'X'&&draw==false)
{
cout << "CONGRATULATIONS !!! PALYER1[X] WINS" << endl;
}
else if (turn == 'Y'&&draw == false)
{
cout << "CONGRATULATIONS !!!  PLAYER2[Y] WINS" << endl;
}
else
{
cout << " THE GAME IS DRAWN" << endl;
}
system("pause");
return 0;
}
*/

Tic Tac Toe Game In C++  | How to Use Function in C++
Tic Tac Toe Game In C++  | How to Use Function in C++

Conclusion:

This is a very basic and simple game in C++ which can also be used in the project also. So by using functions and 2d arrays, you can make many simple basic projects


So if you have ant suggestions related to this project then please let me know in the comment sections.
With best regards,
Bilal Ahmad.

1 comment:

Please tell me if you have any suggestions related to these projects.
thanks

Powered by Blogger.