Professional Documents
Culture Documents
h>
#include<conio.h>
#include<process.h>
#include<string.h>
char b1[]={"|
|
|
|\n"};
char b2[]={"|
|
|
|\n"};
char b3[]={"|
|
|
|\n"};
char c1[]={"|1
|2
|3
|\n"};
char c2[]={"|4
|5
|6
|\n"};
char c3[]={"|7
|8
|9
|\n"};
void box(char[],char[]);
void chan_p1(int);
void chan_p2(int);
int checkwin();
int a=0,i=0;
void main()
{
clrscr();
char p1[20],p2[20];
int m1[5],m2[5];
cout<<"Enter name of player 1:";
cin>>p1;
cout<<"Enter name of player 2:";
cin>>p2;
box(p1,p2);
for(int i=1;i<=5;i++)
{
x:
cout<<p1<<" : Enter your move:";
cin>>m1[i];
chan_p1(m1[i]);
box(p1,p2);
if(a==1)
{
cout<<"Invalid move!!!!\n";
goto x;
}
if(i==5)
{
//
cout<<" Game draw ";
}
else
{
y:
cout<<p2<<" : Enter your move:";
cin>>m2[i];
chan_p2(m2[i]);
box(p1,p2);
if(a==1)
{
cout<<"Invalid move!!!!\n";
goto y;
}
}
}
getch();
}
void box(char p1[],char p2[])
{
clrscr();
cout<<"|-----|-----|-----|\n";
cout<<c1;
cout<<b1;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
cout<<c2;
cout<<b2;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
cout<<c3;
cout<<b3;
cout<<"|
|
|
|\n";
cout<<"-------------------\n";
char pl1[20]; strcpy(pl1,p1);
char pl2[20]; strcpy(pl2,p2);
i=checkwin();
if(i==1)
{
cout<<pl1<<":
WIN ";
getch();
exit(0);
}
else if(i==2)
{
cout<<pl2<<":
WIN ";
getch();
exit(0);
}
else
cout<<"Contine-->\n";
}
void chan_p1(int pos)
{
if(pos==1&&c1[2]==' ')
{b1[3]='X'; c1[2]=''; a=0; }
else if(pos==2&&c1[8]==' ')
{b1[9]='X'; c1[8]=''; a=0; }
else if(pos==3&&c1[14]==' ')
{b1[15]='X'; c1[14]=''; a=0; }
else if(pos==4&&c2[2]==' ')
{b2[3]='X'; c2[2]=''; a=0; }
else if(pos==5&&c2[8]==' ')
{b2[9]='X'; c2[8]=''; a=0; }
else if(pos==6&&c2[14]==' ')
{b2[15]='X'; c2[14]=''; a=0; }
else if(pos==7&&c3[2]==' ')
{b3[3]='X'; c3[2]=''; a=0; }
else if(pos==8&&c3[8]==' ')
{b3[9]='X'; c3[8]=''; a=0; }
else if(pos==9&&c3[14]==' ')
{b3[15]='X'; c3[14]=''; a=0; }
else
a=1;
}
void chan_p2(int pos)
{
if(pos==1&&c1[2]==' ')
{b1[3]='O'; c1[2]=''; a=0; }
else if(pos==2&&c1[8]==' ')
{b1[9]='O'; c1[8]=''; a=0; }
//
//
//
//
//
//
//
//
//
//
//
//
//
// //Simply copying p1 and p2
//
to pl1 and pl2. From string.h
// Function to check who won
//
//
//
//
// From process.h
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
}
//
//
}
//
//
}
//
//
}
//
//
}
//
//
}
//
//
//
//
//
int checkwin()
//
{
//
if(b1[3] == b1[9] && b1[9] == b1[15]) //
{ if(b1[9]=='X')
return 1;
//
else if(b1[9]=='O') return 2; }
//
else if(b2[3]==b2[9]&&b2[9]==b2[15]) //
{if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
else if(b3[3]==b3[9]&&b3[9]==b3[15]) //
{ if(b3[9]=='X')
return 1;
//
else if(b3[9]=='O') return 2; }
//
else if(b1[3]==b2[3]&&b2[3]==b3[3])
//
{ if(b2[3]=='X')
return 1;
//
else if(b2[3]=='O') return 2; }
//
else if(b1[9]==b2[9]&&b2[9]==b3[9])
//
{ if(b1[9]=='X')
return 1;
//
else if(b1[9]=='O') return 2; }
//
else if(b1[15]==b2[15]&&b2[15]==b3[15])//
{ if(b1[15]=='X')
return 1;
//
else if(b1[15]=='O') return 2; }
//
else if(b1[3]==b2[9]&&b2[9]==b3[15]) //
{ if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
else if(b1[15]==b2[9]&&b2[9]==b3[3]) //
{ if(b2[9]=='X')
return 1;
//
else if(b2[9]=='O') return 2; }
//
return 0;
}
//
}