Code:
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=This program is for the game "Tic Tac Toe". -
//-here the two players plays the game using the navigation key =
//=i.e. the arrow keys "up, down, right, left" to navigate through the panel -
//-and selects a particular box by pressing the enter key. =
//= -
//-if a particular user wins the game the program waits for key to be pressed=
//=and after the key is press it returns back to the main function -
//-if at all if a user wants to exit then he need to press the Esc key =
//=to get out of the program. -
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this program runs perfectly on turbo c++ compiler version 3
//provided graphic library is enabled and the bgi folder is at the location
//c:\tc\bgi
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
void operation(int left,int top); //u need to just implement this in class of
//c++ if ur intrested in making c++ class for this
int check(int a[3][3]);
void main(void)
{
int gdriver = DETECT, gmode, errorcode;
int left,top ,i,j,x,y;
initgraph(&gdriver, &gmode, "C:\Program Files\Metrowerks\CodeWarrior\Win32-x86 Support\Libraries\Win32 SDK"); //here u need to specify the location of bgi folder for drivers help
// if u dont specify this ur program will not work
errorcode = graphresult();
if (errorcode != grOk) /* an error
occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error
code */
}
left=x = 160;
top=y = 89 ;
setfillstyle(1,12);
for( i=0;i<3;i++)
{
for( j=0;j<3;j++)
{
bar(left,top,left+100, top+100);
left=left+106;
}
top=top+106;
left=x;
}
top=y;
setcolor(12);
rectangle(left-3,top-3,left+315,top+315);
rectangle(left-5,top-5,left+317,top+317);
line(left+103,top-3,left+103,top+315);
line(left+209,top-3,left+209,top+315);
line(left-3,top+103,left+315,top+103);
line(left-3,top+209,left+315,top+209);
setcolor(15);
settextstyle(3,0,4);
outtextxy(205,10,"\"TIC TAC TOE\"");
setcolor(15);
settextstyle(3,0,2);
outtextxy(515,155,"PLAYER2:");
setcolor(14);
outtextxy(20,155,"PLAYER1:");
setcolor(8);
settextstyle(0,0,4);
outtextxy(45,190,"O");
outtextxy(545,190,"X");
operation(left,top);
getch();
/* clean up */
closegraph();
}
void rect(int left,int top)
{
rectangle(left-1,top-1,left+101,top+101);
}
void operation(int left,int top)
{
int i,j,p=0,x=left;
int a[3][3],y=top,key,r=0,c=0;
setcolor(14);
rect(left,top);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=-1;
while(1)
{
key=getch();
if (key==13)
{ setcolor(0);
settextstyle(0,0,6);
if(p==1&&(a[r][c]!=0&&a[r][c]!=1))
{
outtextxy(left+25,top+25,"X");
p=a[r][c]=0;
setcolor(14);
settextstyle(3,0,2);
outtextxy(20,155,"PLAYER1:");
setcolor(15);
outtextxy(515,155,"PLAYER2:");
}
else if(p==0&&(a[r][c]!=0&&a[r][c]!=1))
{
outtextxy(left+25,top+25,"O");
p=a[r][c]=1;
setcolor(15);
settextstyle(3,0,2);
outtextxy(20,155,"PLAYER1:");
setcolor(14);
outtextxy(515,155,"PLAYER2:");
}
else
{
setcolor(15);
settextstyle(0,0,2);
outtextxy(255,420,"Sorry...");
delay(500);
setfillstyle(1,0);
bar(255,420,375,435);
}
}
else if(key==27)
exit(0);
else if(key==72)
{
if(top==y)
top=y;
else{
setcolor(0);
rect(left,top);
top=top-106;
r=r-1;
setcolor(14);
rect(left,top);}
}
else if(key==80)
{
if(top==301)
top=301;
else{
setcolor(0);
rect(left,top);
top=top+106;
r=r+1;
setcolor(14);
rect(left,top);}
}
else if(key==77)
{
if(left==372)
left=372;
else{
setcolor(0);
rect(left,top);
left=left+106;
c=c+1;
setcolor(14);
rect(left,top);}
}
else if(key==75)
{
if(left==x)
left=x;
else{setcolor(0);
rect(left,top);
left=left-106;
c=c-1;
setcolor(14);
rect(left,top);}
}
key=check(a);
if(key!=0)
{
getch(); // waits for one keystroke
main(); /// for returning back to main function after somebody wins
}
}
}
int check (int a[3][3]) /// these function cheks whether any one plyers has win the game or not
{
int i,j,count=0;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if(a[i][j]==1)
count=count+1;
}
if (count==3)
{
setfillstyle(1,0);
bar(45,190,75,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(20,190,"WINS !");
return (1);
} count=0;
for (j=0;j<3;j++)
{
if(a[i][j]==0)
count=count+1;
}
if (count==3)
{
setfillstyle(1,0);
bar(545,190,575,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(510,190,"WINS! ");
return (1);
}
count=0;
}
for (j=0;j<3;j++)
{
for (i=0;i<3;i++)
{
if(a[i][j]==1)
count=count+1;
}
if (count==3)
{setfillstyle(1,0);
bar(45,190,75,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(20,190,"WINS !");
return(1);
}
count=0;
for (i=0;i<3;i++)
{
if(a[i][j]==0)
count=count+1;
}
if (count==3)
{
setfillstyle(1,0);
bar(545,190,575,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(510,190,"WINS! ");
return(1);
}
count=0;
}
for(i=0;i<3;i++)
{
if(a[i][i]==1)
count=count+1;
if (count==3)
{
setfillstyle(1,0);
bar(45,190,75,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(20,190,"WINS !");
return(1);
}
}
count=0;
for(i=0;i<3;i++)
{
if(a[i][i]==0)
count=count+1;
if (count==3)
{
setfillstyle(1,0);
bar(545,190,575,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(510,190,"WINS! ");
return(1);
}
}
count=0;
for(i=0,j=2;i<3,j>=0;i++,j--)
{
if(a[i][j]==1)
count=count+1;
if (count==3)
{
setfillstyle(1,0);
bar(45,190,75,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(20,190,"WINS !");
return(1);
}
}
count=0;
for(i=0,j=2;i<3,j>=0;i++,j--)
{
if(a[i][j]==0)
count=count+1;
if (count==3)
{
setfillstyle(1,0);
bar(545,190,575,220);
setcolor(14);
settextstyle(8,0,4);
outtextxy(510,190,"WINS! ");
return(1);
}
}
count=0;
return 0;
}