You are on page 1of 3

//Tic-Tac-Toe Game

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void title()
// Title for game
{
cout<<"\n\n\t\t\t\tTic-Tac-Toe\n\n\n";
}
void rules()
// Game rules
{
cout<<" Rules:\n\t";
cout<<"This Is a Simple 2 Player Game.\n\t";
cout<<"In this Game your target is to complete one row, column or diagonal with
\t the letter given. \n\t";
cout<<"First Player will get the letter X and second Player will get letter O.\
n\n\n";
}
// MAIN FUNCTION
main()
{
clrscr();
int q,k,i,j,flag;
char p[1],pname[3][10],c[9],a[3][3];
title();
rules();
q='a';
for(i=0;i<3;i++)
{
cout<<"\t\t\t\t ";
for(j=0;j<3;j++)
{
a[i][j]=q;
cout<<a[i][j]<<" ";
q+=1;
}
cout<<"\n";
}
cout<<"\n\n\n";

cout<<" Type the Name of the first Player ";


gets(pname[0]);
cout<<" Type the Name of the second Player ";
gets(pname[1]);
p[0]='X',p[1]='O';
cout<<"\n\n\t\t\t\t Let The Game Begin \n\n\n";

for(k=0;k<9;k++)
{
cout<<" "<<pname[k%2]<<" It's your turn\n\n";
here:
cout<<" Type the place value, To play ";
cin>>c[k];
flag=0;
for(i=0;i<3;i++)
{
cout<<"\t\t\t\t ";
for( j=0; j<3; j++)
{
if( c[k] == a[i][j] )
{
a[i][j] = p[k%2] ;
cout<< a[i][j]<< " ";
flag=1;
}
else
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
if( flag!=1 )
{
cout<<"Enter the correct place value. \n\n";
goto here;
}
cout<<"\n\n\n";

if(a[0][0]==a[1][1] && a[0][0]==a[2][2] || a[0][2]==a[1][1] && a[1][1]==a[2][


0] || a[0][0]==a[1][0] && a[0][0]==a[2][0] || a[0][0]==a[0][1] && a[0][2]==a[0][
0] || a[2][2]==a[1][2] && a[2][2]==a[0][2] || a[2][2]==a[2][1] && a[2][2]==a[2][
0] || a[1][1]==a[0][1] && a[2][1]==a[1][1] || a[1][1]==a[1][0] && a[1][2]==a[1][
1])
{
if(a[0][0]==a[1][1] && a[0][0]==a[2][2] || a[0][2]==a[1][1] && a[1][1]==a[2
][0])
{
if(a[1][1]=='X')
{
puts(pname[0]);
cout<<"wins";
}
if(a[1][1]=='O')
{
puts(pname[1]);
cout<<"wins";
}
break;
}
if( a[0][0]==a[1][0] && a[0][0]==a[2][0] || a[0][0]==a[0][1] && a[0][2]==a[
0][0])
{

if(a[0][0]=='X')
{
puts(pname[0]);
cout<<"wins";
}
if(a[0][0]=='O')
{
puts(pname[1]);
cout<<"wins";
}
break;
}
if( a[2][2]==a[1][2] && a[2][2]==a[0][2] || a[2][2]==a[2][1] && a[2][2]==a[
2][0])
{
if(a[2][2]=='X')
{
puts(pname[0]);
cout<<"wins";
}
if(a[2][2]=='O')
{
puts(pname[1]);
cout<<"wins";
}
break;
}
if( a[1][1]==a[0][1] && a[2][1]==a[1][1] || a[1][1]==a[1][0] && a[1][2]==a[
1][1])
{
if(a[1][1]=='X')
{
puts(pname[0]);
cout<<"wins";
}
if(a[1][1]=='O')
{
puts(pname[1]);
cout<<"wins";
}
break;
}
}
}
getch();
return 0;
}

You might also like