You are on page 1of 14

//TIC TAC TOE

//by R.K. Dupron

/*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

play it as it is better than you could ever imagine it to be.

I hope you enjoy this game. Good luck!*/

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

char tic[3][3]; //global matrix declerations

int s,k,l,f,z,c[3],d[3]; //global variable declarations

void user(); //function for user's move

void inst(); //function for displaying instructions

void newdisp(); //function for displaying matrix after user's move

void pc(); //function for pc's move

void pcsmart(); //function for smart move by pc

void neconex(); //function to exit or restart the game

int count(); //function to count number of boxes filled

int check(); //function for finding out the winner

void main()

int i,j,m;

randomize(); //random number generator started

clrscr();

for(i=0;i<3;i++)

{ for(j=0;j<3;j++)

tic[i][j]=' '; //assigning space ' ' to all elements of matrix

for(i=0,j=0,m=2;j<3;i++,j++,m--)

{
c[i]=j;

d[i]=m;

cout<<"\n\n\t\t\t\t TIC TAC TOE\n\n\t\t\t\t\tby R.K. Dupron\n\nABOUT GAME:\n\n...A


simple program, yet so smart...\n\nHi there! This is just a small game I coded. The
unique thing about this\nprogram is that it has artificial intelligence. That's
right, it can counter\nevery one of your moves and irritates you in its way to
victory. So do\nyou have the brains to match this brainless opponent? Let's find
out!\n\nGood luck!\n\n\n\n(PRESS ANY KEY TO CONTINUE)";

getch();

inst();

void user()

int x,y;

char p,q;

clrscr();

cout<<"\n\nWHAT WILL YOUR MOVE BE?\n\n";

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();

if((x<0)||(x>2)||(y<0)||(y>2)) //check for valid co-ordinates

cout<<"\n\nENTER THE CORRECT CO-ORDINATES:\n\n";

newdisp();
cout<<"\n(PRESS ANY KEY TO CONTINUE)";

getch();

user();

else if(tic[x][y]==' ') //check for space at entered co-ordinates

tic[x][y]='X'; //assigning user 'X' to the co-ordinates

newdisp();

else

cout<<"\n\nTHIS POSITION IS ALREADY FILLED. CHOOSE SOME OTHER CO-ORDINATES:\n\n";

newdisp();

cout<<"\n(PRESS ANY KEY TO CONTINUE)";

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();

cout<<"\n(PRESS ANY KEY TO CONTINUE)";

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();

cout<<"\n\nTHIS IS MY MOVE. HOW DO YOU LIKE ME NOW?\n\n";

for(s=0;s<3;s++) //checks whether pcsmart should be activated

for(l=0;l<3;l++)

if(tic[s][c[0]]==tic[s][c[1]]&&tic[s][c[1]]!=' '&&tic[s][c[2]]==' '||tic[c[0]]


[s]==tic[c[1]][s]&&tic[c[1]][s]!=' '&&tic[c[2]][s]==' '||tic[c[2]][c[2]]==tic[c[0]]
[c[0]]&&tic[c[0]][c[0]]!=' '&&tic[c[1]][c[1]]==' '||tic[c[2]][d[2]]==tic[c[0]]
[d[0]]&&tic[c[0]][d[0]]!=' '&&tic[c[1]][d[1]]==' ')

pcsmart();
k=c[0]; //swapping values...

c[0]=c[1];

c[1]=c[2];

c[2]=k;

k=d[0]; //swapping another set of values

d[0]=d[1];

d[1]=d[2];

d[2]=k;

while(1)

f=random(3);

z=random(3);

if(count()==9)

goto x;

if(tic[f][z]==' ') //check for space

tic[f][z]='*'; //assigning pc '*' to the co-ordinates

goto x; //exiting for loop to display new tictactoe

x:newdisp();

tic[f][z]='O'; //converting to O

if(count()==9)

cout<<"\nLOOKS LIKE A DRAW! TRY AGAIN!\n(PRESS ANY KEY TO CONTINUE)";

getch();

neconex();
}

cout<<"\n(PRESS ANY KEY TO CONTINUE)\n";

getch();

if(check()==0)

user();

void pcsmart()

for(s=0;s<3;s++) //for 3 rows or columns

for(l=0;l<3;l++) //first priority for finishing the game

switch(0)

case 0: //horizontal tic

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;

case 1: //vertical tic

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;

case 3: //diagonal 2 tic

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(s=0;s<3;s++) //for 3 rows or columns

{
for(l=0;l<3;l++) //second priority for blocking user's move

switch(0)

case 0: //horizontal tic

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;

case 1: //vertical tic

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;

case 2: //diagonal 1 tic

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;

case 3: //diagonal 2 tic

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)

cout<<"\nLOOKS LIKE A DRAW! TRY AGAIN!\n(PRESS ANY KEY TO CONTINUE)";

getch();

neconex();

cout<<"\n(PRESS ANY KEY TO CONTINUE)\n";

getch();

if(check()==0)

user();
else

clrscr();

cout<<"\n\nI WIN, OF COURSE, AS ALWAYS...\n\n";

newdisp();

cout<<"\n(PRESS ANY KEY TO CONTINUE)";

getch();

neconex();

void neconex() //to exit, new game, or continue

char g;

clrscr();

cout<<"\n\nFOR NEW GAME TYPE N, TO CONTINUE TYPE C, TO EXIT TYPE X:\n\n";

newdisp();

cout<<"\nAND YOUR CHOICE IS: ";

cin>>g;

if(g=='x'||g=='X')

clrscr();

int x;

x=random(2);

switch(x)

case 0:cout<<"\n\nFEELING TOO BORED?..\n\t\t\tOR JUST TOO SCARED?..\n\nCYA


LATER...";

break;

case 1:cout<<"\n\nRUNNING AWAY ALREADY?..\n\t\t\t\tWHAT A DISAPPOINTMENT...";

break;
}

getch();

exit(0);

else if(g=='n'||g=='N')

clrscr();

if(count()!=9&&check()==0)

cout<<"\n\nTHAT'S RIGHT, FEAR ME...\n\n\t\t\t\t...AS YOU CAN NEVER EVEN DREAM TO


DEFEAT\n\t\t\t\t\t\tTHE KING OF TIC TAC TOE!\n\t\t\t\t\t\t (THAT'S ME, BY THE
WAY)";

getch();

main();

else if(g=='c'||g=='C')

{ //checks if the game is over

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

cout<<"\nENTER EITHER 2,1 OR 0\n\n(PRESS ANY KEY TO CONTINUE)";


getch();

neconex();

void inst() //to display instructions

int k,c;

long i;

clrscr();

cout<<"\n\n\t\t\t\t INSTRUCTIONS\n\nCO-ORDINATES (DON'T TYPE COMMAS)\n\n";

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";

cout<<"\nX REPRESENTS YOUR MOVE\nO REPRESENTS COMPUTER'S MOVE\n* REPRESENTS


COMPUTER'S NEW MOVE\n\n\t\t\t\t TO RESTART OR EXIT, TYPE N AND THEN X\n\t\t\t\tTO
SEE INSTRUCTIONS AGAIN, TYPE I AND THEN S\n\n(PRESS ANY KEY TO CONTINUE)";

getch();

clrscr();

if(count()==0) //for starting countdown

{
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++);

clrscr(); //long for loops for time delay

cout<<"\n\n\n\n\n\n\n\t\t\t\t 22222222222\n\t\t\t\t 22222222222\n\t\t\t\t


2222\n\t\t\t\t 2222\n\t\t\t\t 22222222222\n\t\t\t\t 22222222222\n\t\t\t\t
2222\n\t\t\t\t 2222\n\t\t\t\t 22222222222\n\t\t\t\t 22222222222";

for(i=0;i<60000000;i++);

clrscr();

cout<<"\n\n\n\n\n\n\n\t\t\t\t 11 \n\t\t\t\t 1111 \n\t\t\t\t 111111 \n\t\t\t\t


1111 \n\t\t\t\t 1111 \n\t\t\t\t 1111 \n\t\t\t\t 1111 \n\t\t\t\t 1111 \n\t\t\t\t
1111 \n\t\t\t\t 1111111111111 \n\t\t\t\t 1111111111111 ";

for(i=0;i<60000000;i++);

clrscr();

randomize();

if(count()==0)

((int)random(2))==0?user():pc();

else //for either user or pc to start

user();

int count()

int m,p,q;

m=0;

for(p=0;p<3;p++) //counts the number of filled boxes, i.e. the value of m.

for(q=0;q<3;q++)

if(tic[p][q]!=' ')

m++;

}
return m;

int check()

int d=0;

for(s=0;s<3;s++) //checks each element of a horizontal line to

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]!=' ')

d=1; //returns 1 if all 3 elements of horizontal are same

break;

return d;

You might also like