Professional Documents
Culture Documents
/*I present to you the greatest Tic Tac Toe of all times. The one and
only intelligent TTT. It'll bring tears to your eyes every time you
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
int i,j,m;
clrscr();
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
for(i=0,j=0,m=2;j<3;i++,j++,m--)
{
c[i]=j;
d[i]=m;
getch();
inst();
void user()
int x,y;
char p,q;
clrscr();
newdisp();
cout<<"\nENTER THE CO-ORDINATES WHERE YOU WANT TO PUT YOUR 'X' i.e. 0,1,2:\n";
cin>>q>>p;
y=q-48;
x=50-p;
if((q=='n'||q=='N')&&(p=='x'||p=='X'))
neconex();
if((q=='i'||q=='I')&(p=='s'||p=='S'))
inst();
clrscr();
newdisp();
cout<<"\n(PRESS ANY KEY TO CONTINUE)";
getch();
user();
newdisp();
else
newdisp();
getch();
user();
clrscr();
if(check()==0)
pc();
else
cout<<"\n\nSMART MOVE, HUMAN. YOU MAY HAVE WON THIS TIME, BUT YOU WON'T WIN
AGAIN!\n\n";
newdisp();
getch();
neconex();
}
void newdisp() //displays new tictactoe after user or pc turn
int t,r;
for(t=0;t<3;t++)
cout<<"\t\t\t\t ";
for(r=0;r<3;r++)
cout<<tic[t][r];
if(r!=2)
cout<<" | ";
if(t!=2)
cout<<"\n\t\t\t\t --|---|--";
cout<<"\n";
void pc()
int h;
randomize();
for(l=0;l<3;l++)
pcsmart();
k=c[0]; //swapping values...
c[0]=c[1];
c[1]=c[2];
c[2]=k;
d[0]=d[1];
d[1]=d[2];
d[2]=k;
while(1)
f=random(3);
z=random(3);
if(count()==9)
goto x;
x:newdisp();
tic[f][z]='O'; //converting to O
if(count()==9)
getch();
neconex();
}
getch();
if(check()==0)
user();
void pcsmart()
switch(0)
if(tic[s][c[0]]==tic[s][c[1]]&&tic[s][c[1]]=='O'&&tic[s][c[2]]==' ')
tic[s][c[2]]='*';
f=s;
z=c[2];
goto y;
if(tic[c[0]][s]==tic[c[1]][s]&&tic[c[1]][s]=='O'&&tic[c[2]][s]==' ')
tic[c[2]][s]='*';
f=c[2];
z=s;
goto y;
}
case 2: //diagonal 1 tic
if(tic[c[2]][c[2]]==tic[c[0]][c[0]]&&tic[c[0]][c[0]]=='O'&&tic[c[1]][c[1]]==' ')
tic[c[1]][c[1]]='*';
f=c[1];
z=c[1];
goto y;
if(tic[c[2]][d[2]]==tic[c[0]][d[0]]&&tic[c[0]][d[0]]=='O'&&tic[c[1]][d[1]]==' ')
tic[c[1]][d[1]]='*';
f=c[1];
z=d[1];
goto y;
k=c[0];
c[0]=c[1];
c[1]=c[2];
c[2]=k;
k=d[0];
d[0]=d[1];
d[1]=d[2];
d[2]=k;
{
for(l=0;l<3;l++) //second priority for blocking user's move
switch(0)
if(tic[s][c[0]]==tic[s][c[1]]&&tic[s][c[1]]=='X'&&tic[s][c[2]]==' ')
tic[s][c[2]]='*';
f=s;
z=c[2];
goto y;
if(tic[c[0]][s]==tic[c[1]][s]&&tic[c[1]][s]=='X'&&tic[c[2]][s]==' ')
tic[c[2]][s]='*';
f=c[2];
z=s;
goto y;
if(tic[c[2]][c[2]]==tic[c[0]][c[0]]&&tic[c[0]][c[0]]=='X'&&tic[c[1]][c[1]]==' ')
tic[c[1]][c[1]]='*';
f=c[1];
z=c[1];
goto y;
if(tic[c[2]][d[2]]==tic[c[0]][d[0]]&&tic[c[0]][d[0]]=='X'&&tic[c[1]][d[1]]==' ')
{
tic[c[1]][d[1]]='*';
f=c[1];
z=d[1];
goto y;
k=c[0];
c[0]=c[1];
c[1]=c[2];
c[2]=k;
k=d[0];
d[0]=d[1];
d[1]=d[2];
d[2]=k;
y:newdisp();
tic[f][z]='O'; //converting to O
if(count()==9)
getch();
neconex();
getch();
if(check()==0)
user();
else
clrscr();
newdisp();
getch();
neconex();
char g;
clrscr();
newdisp();
cin>>g;
if(g=='x'||g=='X')
clrscr();
int x;
x=random(2);
switch(x)
break;
break;
}
getch();
exit(0);
else if(g=='n'||g=='N')
clrscr();
if(count()!=9&&check()==0)
getch();
main();
else if(g=='c'||g=='C')
if(count()!=9&&check()==0)
user();
else
cout<<"\nSORRY, BUT THE GAME IS CLEARLY OVER. TYPE EITHER N OR X\n\n(PRESS ANY KEY
TO CONTINUE)\n";
getch();
neconex();
else
neconex();
int k,c;
long i;
clrscr();
for(k=2;k>=0;k--)
cout<<"\t\t\t ";
for(c=0;c<3;c++)
cout<<"("<<c<<","<<k<<")";
//example formation
if(c!=2)
cout<<" | ";
cout<<"\n";
if(k!=0)
cout<<"\t\t\t ------|-------|------\n";
getch();
clrscr();
{
cout<<"\n\n\n\n\n\n\n\t\t\t\t 33333333333\n\t\t\t\t 33333333333\n\t\t\t\t
3333\n\t\t\t\t 3333\n\t\t\t\t 33333333\n\t\t\t\t 33333333\n\t\t\t\t 3333\n\t\t\t\t
3333\n\t\t\t\t 33333333333\n\t\t\t\t 33333333333";
for(i=0;i<60000000;i++);
for(i=0;i<60000000;i++);
clrscr();
for(i=0;i<60000000;i++);
clrscr();
randomize();
if(count()==0)
((int)random(2))==0?user():pc();
user();
int count()
int m,p,q;
m=0;
for(q=0;q<3;q++)
if(tic[p][q]!=' ')
m++;
}
return m;
int check()
int d=0;
if(tic[s][0]==tic[s][1]&&tic[s][1]==tic[s][2]&&tic[s][1]!=' '||tic[0][s]==tic[1]
[s]&&tic[1][s]==tic[2][s]&&tic[0][s]!=' '||tic[0][0]==tic[1][1]&&tic[1][1]==tic[2]
[2]&&tic[0][0]!=' '||tic[0][2]==tic[1][1]&&tic[1][1]==tic[2][0]&&tic[1][1]!=' ')
break;
return d;