Tic Tac Toe Game in C++ code
The paper-and-pencil game of Tic Tac Toe Game in C++ code is now simple to use and understand in the language of C++. The code determines all input, output, and basic functions. The two players are given instructions to follow on which side of the grid they should begin marking following that specific player’s turn.
One player begins by choosing either an “X” or “0” and the other player takes a turn marking the opposite symbol on the board starting from any spot on the board until there are three symbols marked in a row, column, or diagonal. Tic Tac Toe is one of our favorite games as it teaches us how to logically think through problems because sometimes you may feel like you're going nowhere but you have to make sure not to lose sight of what's coming next - so stay alert!
Tic Tac Toe Game Code :
/* BROUGHT TO YOU BY code-projects.org*/
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
char mat[3][3]={'1','2','3','4','5','6','7','8','9'};
char player='X';
int n;
class game
{
public:
//FUNCTION 0
void clear()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat[i][j]=5;//If you want you can change this to the above one '1','2' by manually placing these values at each index od array
}
}
player='X';
n=0;
}
//FUNCTION 1
void draw()
{
//system("cls");
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
//FUNCTION 2
void input()
{
int a;
cout<<"It's "<<player<<" Turn !\nPress Any Number Where You want To Mark: ";
cin>>a;
cout<<"\n";
//fOR 1
if(a==1)
{
if(mat[0][0]!='X'&&mat[0][0]!='O')
{
mat[0][0]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 2
if(a==2)
{
if(mat[0][1]!='X'&&mat[0][1]!='O')
{
mat[0][1]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 3
if(a==3)
{
if(mat[0][2]!='X'&&mat[0][2]!='O')
{
mat[0][2]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 4
if(a==4)
{
if(mat[1][0]!='X'&&mat[1][0]!='O')
{
mat[1][0]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 5
if(a==5)
{
if(mat[1][1]!='X'&&mat[1][1]!='O')
{
mat[1][1]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 6
if(a==6)
{
if(mat[1][2]!='X'&&mat[1][2]!='O')
{
mat[1][2]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 7
if(a==7)
{
if(mat[2][0]!='X'&&mat[2][0]!='O')
{
mat[2][0]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 8
if(a==8)
{
if(mat[2][1]!='X'&&mat[2][1]!='O')
{
mat[2][1]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
//FOR 9
if(a==9)
{
if(mat[2][2]!='X'&&mat[2][2]!='O')
{
mat[2][2]=player;
}
else
{
cout<<"Try Again!\n";
input();
}
}
}
//FUNCTION 3
void player2()
{
if(player=='X')
player='O';
else
player='X';
}
//FUNCTION 4
char win()
{
if(mat[0][0]=='X' && mat[0][1]=='X' && mat[0][2]=='X' )
return 'X';
if(mat[1][0]=='X' && mat[1][1]=='X' && mat[1][2]=='X' )
return 'X';
if(mat[2][0]=='X' && mat[2][1]=='X' && mat[2][2]=='X' )
return 'X';
if(mat[0][0]=='X' && mat[1][0]=='X' && mat[2][0]=='X' )
return 'X';
if(mat[0][1]=='X' && mat[1][1]=='X' && mat[2][1]=='X' )
return 'X';
if(mat[0][2]=='X' && mat[1][2]=='X' && mat[2][2]=='X' )
return 'X';
if(mat[0][0]=='X' && mat[1][1]=='X' && mat[2][2]=='X' )
return 'X';
if(mat[2][0]=='X' && mat[1][1]=='X' && mat[0][2]=='X' )
return 'X';
if(mat[0][0]=='O' && mat[0][1]=='O' && mat[0][2]=='O' )
return 'O';
if(mat[1][0]=='O' && mat[1][1]=='O' && mat[1][2]=='O' )
return 'O';
if(mat[2][0]=='O' && mat[2][1]=='O' && mat[2][2]=='O' )
return 'O';
if(mat[0][0]=='O' && mat[1][0]=='O' && mat[2][0]=='O' )
return 'O';
if(mat[0][1]=='O' && mat[1][1]=='O' && mat[2][1]=='O' )
return 'O';
if(mat[0][2]=='O' && mat[1][2]=='O' && mat[2][2]=='O' )
return 'O';
if(mat[0][0]=='O' && mat[1][1]=='O' && mat[2][2]=='O' )
return 'O';
if(mat[2][0]=='O' && mat[1][1]=='O' && mat[0][2]=='O' )
return 'O';
return ('/');
}
};
int main()
{
game g1;
do{
cout<<" X-O-X-O-X \n";
cout<<"TIC TAC TOE\n";
n=0;
g1.clear();
g1.draw();
while(1)
{
n++;
g1.input();
g1.draw();
if(g1.win()=='X')
{
cout<<"Player 1 Won!\n"<<endl;
break;
}
else if(g1.win()=='O')
{
cout<<"Player 2 Won!\n"<<endl;
break;
}
else if(g1.win()=='/' && n==9)
{
cout<<"Draw!\n"<<endl;
break;
}
g1.player2();
}
cout<<"Do You Want To Play Again? press (y/n)";
//system("pause");
}
while(getche()=='y');
}
Tic Tac Toe Game in C++ code Installation:
- Extract the zip file.
- Open the extracted folder
- Locate the c file.
- Then open the file via codeblocks or any IDE's that can run C++ file.
- Then click run and build.
FREE DOWNLOAD Tic Tac Toe Game in C++ code :
CLICK THE BUTTON BELOW
Related Project
Complete Number System Conversion Application in c with free Source Code Bakery Management System using C++ with full source code Hangman Game Project in C++ with Source Code Airline Reservation System Project in c++ with source code DVD Listing System in C with Source Code Electric Bill System Generator using C++ with Full Source Code Personal Diary Management System in c++ Car Parking Management System Project in c++ Gym Management System Project in C++ Student Record System Project in c Source Code 🔥Pharmacy Management System in C++ with Full Source Code School Billing System project In C With Source Code Library Management System in C download Hospital Patient Record System using C++ with Full Source Code Simple Telecom Billing System Source Code in C
Post A Comment:
0 comments: