tic tac toe source code

56
// main.c #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include"mouse.h" int ch=0; int key; int client=0; int movec,moveu,u; int x[9]={130,190,250,130,190,250,130,190,250}; int y[9]={230,230,230,290,290,290,350,350,350}; char p[2],r[2]; int x2[9]={100,160,220,100,160,220,100,160,220}; int y2[9]={200,200,200,260,260,260,320,320,320}; int c[9]={0,0,0,0,0,0,0,0,0}; int d[9]={20273,20530,20787,19252,19509,19766,18231,18488,18745}; int x1[9]={160,220,280,160,220,280,160,220,280}; int y1[9]={260,260,260,320,320,320,380,380,380}; int b,xx,yy;

Upload: prashant-kaushik

Post on 07-Apr-2015

124 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tic Tac Toe Source Code

// main.c

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include"mouse.h"

int ch=0;

int key;

int client=0;

int movec,moveu,u;

int x[9]={130,190,250,130,190,250,130,190,250};

int y[9]={230,230,230,290,290,290,350,350,350};

char p[2],r[2];

int x2[9]={100,160,220,100,160,220,100,160,220};

int y2[9]={200,200,200,260,260,260,320,320,320};

int c[9]={0,0,0,0,0,0,0,0,0};

int d[9]={20273,20530,20787,19252,19509,19766,18231,18488,18745};

int x1[9]={160,220,280,160,220,280,160,220,280};

int y1[9]={260,260,260,320,320,320,380,380,380};

int b,xx,yy;

char msg[31][50]={{"How to play:"},{"You can place only one"},{"symbol

at a time followed"},{"by computer's turn,thinking"},{"wisely and go ahead

Page 2: Tic Tac Toe Source Code

to"},{"place your symbol or color"},{"in any stright line or"},{"stright

line along the"},{"diagonal.if u succed you"},{"win,if computer succed

in"},{"placing its symbol along"},{"in any stright line or"},{"diagonal

then you lose."},{"If both doesn't win then"},{"it's the match"},{"-> TRY

AGAIN you may win!."},{"How to play:"},{"The topleft corner of "},{"grid

is 1 and bottom"},{"right ia 9.Press any"},{"number (FROM

NUMPAD)"},{"suitably to place your"},{"symbol in appropriate"},{"place you

like"}

,{"copyright:"},{"----------"},{"M.K.jayendranath"},{"J.izzathulla"},{"Sen

d Comments

to:"},{"[email protected]"},{"[email protected]"}}; int f=0;

int main(void)

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode,i;

int j,g,h=0,x11,y11;

char msg[35];

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "d:\\tc");

m();hidemp();

/* read result of initialization */

errorcode = graphresult();

Page 3: Tic Tac Toe Source Code

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

} yy:

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

c[i]=0;

setfillstyle(1,0);

bar(0,0,600,480);

setcolor(15);

rectangle(100,200,280,380);

line(160,200,160,380);

line(220,200,220,380);

line(100,260,280,260);

line(100,320,280,320);

settextstyle(0,0,1);

outtextxy(108,208,"1");

outtextxy(168,208,"2");

outtextxy(228,208,"3");

outtextxy(108,268,"4");

outtextxy(168,268,"5");

outtextxy(228,268,"6");

outtextxy(108,328,"7");

Page 4: Tic Tac Toe Source Code

outtextxy(168,328,"8");

outtextxy(228,328,"9");

settextstyle(4,0,0);

outtextxy(50,50,"Welcome to the Bonsiv game");

settextstyle(3,0,1);

setcolor(RED);

outtextxy(50,110,"Enter position :");

settextstyle(1,0,1);

setcolor(BLUE);

outtextxy(50,90,"Who starts (c ->computer u -> user) :");

outtextxy(50,420,"Help");

setcolor(YELLOW);

outtextxy(50,420,"H");

//setcolor(BLUE);

//outtextxy(50,440,"Restart");

//setcolor(YELLOW);

//outtextxy(50,440,"R");

setcolor(BLUE);

outtextxy(50,460,"Exit");

setcolor(YELLOW);

outtextxy(50,460,"E");

showmp();

bb:

showmp();

while(bioskey(1)==0)

Page 5: Tic Tac Toe Source Code

{

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,460,"Exit");

setcolor(BLUE);

outtextxy(50,460,"E");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

getmstat(&b,&xx,&yy);

if(b==1)

exitit();

}

hidemp();

setcolor(BLUE);

outtextxy(50,460,"Exit");

setcolor(YELLOW);

outtextxy(50,460,"E");

showmp();

}

while(xx>50 && xx<84 && yy>425 && yy<440)

Page 6: Tic Tac Toe Source Code

{

hidemp();

setcolor(YELLOW);

outtextxy(50,420,"Help");

setcolor(BLUE);

outtextxy(50,420,"H");

showmp();

//delay(100);

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>425 && yy<440)

{

getmstat(&b,&xx,&yy);

if(b==1)

{

help();

}

}

hidemp();

setcolor(BLUE);

outtextxy(50,420,"Help");

setcolor(YELLOW);

outtextxy(50,420,"H");

showmp();

}

Page 7: Tic Tac Toe Source Code

/*while(xx>50 && xx<95 && yy>445 && yy<460)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,440,"Restart");

setcolor(BLUE);

outtextxy(50,440,"R");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<95 && yy>445 && yy<460)

{

getmstat(&b,&xx,&yy);

if(b==1)

goto yy;

}

hidemp();

setcolor(BLUE);

outtextxy(50,440,"Restart");

setcolor(YELLOW);

outtextxy(50,440,"R");

showmp();

} */

while(xx>165 && xx<300 && yy>95 && yy<110)

{

Page 8: Tic Tac Toe Source Code

hidemp();

setcolor(YELLOW);

outtextxy(50,90," (c ->computer ");

showmp();

getmstat(&b,&xx,&yy);

while(xx>165 && xx<300 && yy>95 && yy<110)

{

getmstat(&b,&xx,&yy);

if(b==1)

goto selcomputer;

}

hidemp();

setcolor(BLUE);

outtextxy(50,90," (c ->computer ");

showmp();

}

while(xx>305 && xx<400 && yy>95 && yy<110)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,90," u -> user) :");

showmp();

getmstat(&b,&xx,&yy);

Page 9: Tic Tac Toe Source Code

while(xx>305 && xx<400 && yy>95 && yy<110)

{

getmstat(&b,&xx,&yy);

if(b==1)

goto seluser;

}

hidemp();

setcolor(BLUE);

outtextxy(50,90," u -> user) :");

showmp();

}

}

key=bioskey(0);

if(key==11875 || key==11843)

{

selcomputer: setcolor(CYAN);

hidemp();

outtextxy(450,90,"Computer");

showmp();

rr:

if( draw()==0)

Page 10: Tic Tac Toe Source Code

goto rr;

else goto dd;

}else if(key==5749 || key==5717)

{setcolor(CYAN);

seluser:

hidemp();

outtextxy(450,90,"User");

client=1;

showmp();

}else if(key==4978)

goto yy;

else if (key==4709 || key==4677)

{ exitit();exit(0);}

else if(key==9064||key==9032)

helpbox: help();

else

goto bb;

dd:

showmp();

setcolor(BLUE);

outtextxy(50,440,"Restart");

setcolor(YELLOW);

outtextxy(50,440,"R");

Page 11: Tic Tac Toe Source Code

while(bioskey(1)==0)

{

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,460,"Exit");

setcolor(BLUE);

outtextxy(50,460,"E");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

getmstat(&b,&xx,&yy);

if(b==1)

exitit();

}

hidemp();

setcolor(BLUE);

outtextxy(50,460,"Exit");

setcolor(YELLOW);

outtextxy(50,460,"E");

showmp();

Page 12: Tic Tac Toe Source Code

}

while(xx>50 && xx<84 && yy>425 && yy<440)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,420,"Help");

setcolor(BLUE);

outtextxy(50,420,"H");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>425 && yy<440)

{

getmstat(&b,&xx,&yy);

if(b==1)

{

help();

goto out;

}

}

hidemp();

setcolor(BLUE);

outtextxy(50,420,"Help");

setcolor(YELLOW);

outtextxy(50,420,"H");

showmp();

Page 13: Tic Tac Toe Source Code

}

while(xx>50 && xx<95 && yy>445 && yy<460)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,440,"Restart");

setcolor(BLUE);

outtextxy(50,440,"R");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<95 && yy>445 && yy<460)

{ getmstat(&b,&xx,&yy);

if(b==1)

{ setfillstyle(1,BLACK);

hidemp();

bar(50,440,250,460);

goto yy;

}

}

hidemp();

setcolor(BLUE);

outtextxy(50,440,"Restart");

setcolor(YELLOW);

outtextxy(50,440,"R");

showmp();

Page 14: Tic Tac Toe Source Code

}

if(xx>100 && xx<160 && yy>200 && yy<260 && b==1)

{

key=d[0];

goto jj;

}

if(xx>160 && xx<220 && yy>200 && yy<260 && b==1)

{

key=d[1];

goto jj;

}

if(xx>220 && xx<280 && yy>200 && yy<260 && b==1)

{

key=d[2];

goto jj;

}

if(xx>100 && xx<160 && yy>260 && yy<320 && b==1)

{

key=d[3];

goto jj;

}

if(xx>160 && xx<220 && yy>260 && yy<320 && b==1)

{

key=d[4];

goto jj;

Page 15: Tic Tac Toe Source Code

}

if(xx>220 && xx<280 && yy>260 && yy<320 && b==1)

{

key=d[5];

goto jj;

}

if(xx>100 && xx<160 && yy>320 && yy<380 && b==1)

{

key=d[6];

goto jj;

}

if(xx>160 && xx<220 && yy>320 && yy<380 && b==1)

{

key=d[7];

goto jj;

}

if(xx>220 && xx<280 && yy>320 && yy<380 && b==1)

{

key=d[8];

goto jj;

}

}

out:

key=bioskey(0);

jj:

Page 16: Tic Tac Toe Source Code

while(ch==1)

{

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,460,"Exit");

setcolor(BLUE);

outtextxy(50,460,"E");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<84 && yy>465 && yy<480)

{

getmstat(&b,&xx,&yy);

if(b==1)

exitit();

}

hidemp();

setcolor(BLUE);

outtextxy(50,460,"Exit");

setcolor(YELLOW);

outtextxy(50,460,"E");

showmp();

Page 17: Tic Tac Toe Source Code

}

while(xx>50 && xx<95 && yy>445 && yy<460)

{

hidemp();

setcolor(YELLOW);

outtextxy(50,440,"Restart");

setcolor(BLUE);

outtextxy(50,440,"R");

showmp();

getmstat(&b,&xx,&yy);

while(xx>50 && xx<95 && yy>445 && yy<460)

{

getmstat(&b,&xx,&yy);

if(b==1)

{ ch=0;

goto yy;

}

}

hidemp();

setcolor(BLUE);

outtextxy(50,440,"Restart");

setcolor(YELLOW);

outtextxy(50,440,"R");

showmp();

}

Page 18: Tic Tac Toe Source Code

if(key==4978)

{ ch=0;

goto yy;

}

if (key==4709 || key==4677)

{ ch=0;

exitit();

} while(bioskey(1)!=0)

{

key=bioskey(0);

}

}

if(key==4978)

{ goto yy;

}

if(key==7181)

{ if (cheet()==3)

ch=1;

}

if (key==4709 || key==4677)

{ exitit();

}

Page 19: Tic Tac Toe Source Code

if(key==5749 || key==5717)

{ hidemp();

setfillstyle(1,0);

bar(x2[movec]+2,y2[movec]+2,x1[movec]-2,y1[movec]-2);

bar(x2[moveu]+2,y2[moveu]+2,x1[moveu]-2,y1[moveu]-2);

c[movec]=0;

c[moveu]=0;

sprintf(p,"%d",movec+1);

sprintf(r,"%d",moveu+1);

settextstyle(0,0,1);

outtextxy(x2[movec]+8,y2[movec]+8,p);

outtextxy(x2[moveu]+8,y2[moveu]+8,r);

settextstyle(1,0,1);

showmp();

goto dd;

}

if(check(key)==1)

{ch=1;

goto dd;

Page 20: Tic Tac Toe Source Code

}

if(result1()==1)

{ ch=1;

goto dd;

}

goto dd;

getch();

closegraph();

return 0;

}

drawbut(int a)

{

int i;

if(c[a]==0)

{ setfillstyle(1,GREEN);

fillellipse(x[a],y[a],20,20);

c[a]=2;

movec=a;

return 1;

}

else

{

/* for(i=0;i<9;i++)

Page 21: Tic Tac Toe Source Code

if(c[i]==0)

{ drawbut(i);

return;

}*/

return 0;

}

}

check(int key)

{

int i,j,g,h=0;

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

{ if(key==d[i] && c[i]!=0)

{ return 0;

}

}

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

{

if(key==d[i])

{ if(c[i]==0)

{ setfillstyle(1,RED);

hidemp();

fillellipse(x[i],y[i],20,20);

showmp();

Page 22: Tic Tac Toe Source Code

c[i]=1;

moveu=i;

}

}

}

if(client==1)

{ client=0;

if(drawbut(4)==1)

{

c[4]=2;

return 0;

}

}

if(result()==1)

{ return 1;

}

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

{

g=0;

if(c[j]==2) g++;

else

h=j;

Page 23: Tic Tac Toe Source Code

if(c[j+1]==2) g++;

else

h=j+1;

if(c[j+2]==2) g++;

else

h=j+2;

if(g==2 )

{ if(drawbut(h)==1)

return 0;

}

}

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

{

g=0;

if(c[j]==2) g++;

else

h=j;

Page 24: Tic Tac Toe Source Code

if(c[j+3]==2) g++;

else

h=j+3;

if(c[j+6]==2) g++;

else

h=j+6;

if(g==2)

{

if(drawbut(h)==1)

return 0;

}

}

g=0;

if(c[0]==2) g++; else h=0;

if(c[4]==2) g++; else h=4;

if(c[8]==2) g++; else h=8;

if(g==2)

{ if(drawbut(h)==1)

return 0;

}

Page 25: Tic Tac Toe Source Code

g=0;

if(c[2]==2) g++; else h=2;

if(c[4]==2) g++; else h=4;

if(c[6]==2) g++; else h=6;

if(g==2)

{ if(drawbut(h)==1)

return 0;

}

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

{

g=0;

if(c[j]==1) g++;

else

h=j;

if(c[j+1]==1) g++;

Page 26: Tic Tac Toe Source Code

else

h=j+1;

if(c[j+2]==1) g++;

else

h=j+2;

if(g==2 )

{ if(drawbut(h)==1)

return 0;

}

}

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

{

g=0;

if(c[j]==1) g++;

else

h=j;

if(c[j+3]==1) g++;

Page 27: Tic Tac Toe Source Code

else

h=j+3;

if(c[j+6]==1) g++;

else

h=j+6;

if(g==2)

{

if(drawbut(h)==1)

return 0;

}

}

g=0;

if(c[0]==1) g++; else h=0;

if(c[4]==1) g++; else h=4;

if(c[8]==1) g++; else h=8;

if(g==2)

{ if(drawbut(h)==1)

return 0;

}

g=0;

Page 28: Tic Tac Toe Source Code

if(c[2]==1) g++; else h=2;

if(c[4]==1) g++; else h=4;

if(c[6]==1) g++; else h=6;

if(g==2)

{ if(drawbut(h)==1)

return 0;

}

if(c[0]==1) {if(drawbut(8)==1) return 0;}

if(c[8]==1) {if(drawbut(0)==1) return 0;}

if(c[6]==1) {if(drawbut(2)==1) return 0;}

if(c[2]==1) {if(drawbut(6)==1) return 0;}

if(drawbut(4)==1)

return 0;

Page 29: Tic Tac Toe Source Code

else

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

{

if(c[i]==0)

{ if(drawbut(i)==1)

return 0;

}

}

}

draw()

{ int y;

//y=random(8);

//if(c[y]==0)

//{

if(drawbut(4)==1)

return 1;

// }

else

{ return 0;

}

}

int result()

{

Page 30: Tic Tac Toe Source Code

if((c[0]==1 && c[1]==1 && c[2]==1) ||(c[3]==1 && c[4]==1 && c[5]==1)

||(c[6]==1 && c[7]==1 && c[8]==1) ||(c[0]==1 && c[3]==1 && c[6]==1)

||(c[1]==1 && c[4]==1 && c[7]==1) ||(c[2]==1 && c[5]==1 &&

c[8]==1)||(c[0]==1 && c[4]==1 && c[8]==1) || (c[2]==1 && c[4]==1 &&

c[6]==1) )

{

outtextxy(400,300,"You win!!");

return 1;

}

}

int result1()

{

if((c[0]==2 && c[1]==2 && c[2]==2) ||(c[3]==2 && c[4]==2 && c[5]==2)

||(c[6]==2 && c[7]==2 && c[8]==2) ||(c[0]==2 && c[3]==2 && c[6]==2)

||(c[1]==2 && c[4]==2 && c[7]==2) ||(c[2]==2 && c[5]==2 &&

c[8]==2)||(c[0]==2 && c[4]==2 && c[8]==2) || (c[2]==2 && c[4]==2 &&

c[6]==2) )

{

outtextxy(400,300,"Computer win!!");

return 1;

}

}

help()

{//setfillstyle(1,BLUE);

//bar( 320,190,540,390);

Page 31: Tic Tac Toe Source Code

int i;

f=0;

setfillstyle(1,7);

bar( 330,120,600,380);

setlinestyle(0,0,1);

setcolor(15);

line(330,120,600,120);

line(330,120,330,380);

setcolor(0);

line(600,120,600,380);

line(330,380,600,380);

setfillstyle(1,15);

bar( 340,150,590,370);

setfillstyle(1,8);

bar(590,370,575,150);

setcolor(15);

line(590,370,590,150);

line(340,370,589,370);

setcolor(0);

line(340,150,340,370);

line(590,150,340,150);

setfillstyle(1,BLUE);

Page 32: Tic Tac Toe Source Code

bar( 332,122,597,137);

setcolor(15);

settextstyle(0,0,0);

outtextxy(337,126,"Help");

setfillstyle(1,7);

bar(595,135,584,124);

setcolor(15);

line(584,124,595,124);

line(584,124,584,135);

setcolor(0);

line(595,124,595,135);

line(595,135,584,135);

settextstyle(0,0,0);

outtextxy(586,127,"X");

scroll();

setlinestyle(0,0,2);

setcolor(8);

line(594,379,599,374);

line(587,379,599,367);

setcolor(15);

line(592,379,599,372);

line(585,379,599,365);

//setlinestyle(0,0,0);

setcolor(0);

Page 33: Tic Tac Toe Source Code

settextstyle(0,0,0);

for(i=0;i<26;i=i+2)

outtextxy(345,155+(i*8),msg[(i/2)]);

rop:

while(bioskey(1)!=0){

key=bioskey(0);}

getmstat(&b,&xx,&yy);

if(xx>576 && xx<589 && yy>151+(f*5.5) && yy<173+(f*5.5) && b==1)

{

getmstat(&b,&xx,&yy);

u=yy;

while(xx>576 && xx<589 && yy>151+(f*5.5) && yy<173+(f*5.5) && b==1)

{

getmstat(&b,&xx,&yy);

if(f<36)

{hidemp();

f=f+((((yy+11)-(151+(f*5.5)))/11)*2);

refresh();

showmp();

}

}

}

Page 34: Tic Tac Toe Source Code

if(xx>584 && xx<595 && yy>124 && yy<135 && b==1)

{ redrawbut();

while(xx>584 && xx<595 && yy>124 && yy<135 )

{ getmstat(&b,&xx,&yy);

if(b==0)

{hidemp();

setfillstyle(1,0);

bar( 320,120,610,390);

showmp();

return;

}

}

hidemp();

setfillstyle(1,7);

bar(595,135,584,124);

setcolor(15);

line(584,124,595,124);

line(584,124,584,135);

setcolor(0);

line(595,124,595,135);

line(595,135,584,135);

settextstyle(0,0,0);

Page 35: Tic Tac Toe Source Code

outtextxy(586,127,"X");

settextstyle(1,0,1);

showmp();

}

if(key==0x11b)

{

setfillstyle(1,0);

bar( 320,120,610,390);

return;

}

if(key==0x4800)

{if(f>0)

{ f=f-2;

refresh();

redo();

}

key=0;

}

if(key==0x5000)

Page 36: Tic Tac Toe Source Code

{ if(f<36)

{

f=f+2;

refresh();

}

key=0;

}

goto rop;

}

refresh()

{int i,j;

settextstyle(0,0,0);

setfillstyle(1,15);

bar( 341,151,574,369);

for(j=0,i=f;i<26+f;i=i+2,j=j+2)

{

if(i>=48 && i<57)

setcolor(BLUE);

else if(i>=58) setcolor(RED);

Page 37: Tic Tac Toe Source Code

else

setcolor(0);

if(i==50)

outtextxy(345,155+((j-1)*8),msg[(i/2)]);

else

outtextxy(345,155+(j*8),msg[(i/2)]);

}

scroll();

}

scroll()

{ int g=0;

if(f==36)

g=2;

if(f==0)

{

setfillstyle(1,7);

bar(589,173,576,151);

setcolor(WHITE);

line(576,151,576,173);

line(576,151,589,151);

setcolor(0);

line(589,151,589,173);

line(589,173,576,173);

Page 38: Tic Tac Toe Source Code

}

else

{

setfillstyle(1,8);

bar(589,173+((f-1)* 5.5),576,151);

setfillstyle(1,7);

bar(589,173-g+(f*5.5),576,151+(f*5.5));

setcolor(WHITE);

line(576,151+(f*5.5),576,173-g+(f*5.5));

line(576,151+(f*5.5),589,151+(f*5.5));

setcolor(0);

line(589,151+(f*5.5),589,173-g+(f*5.5));

line(589,173-g+(f*5.5),576,173-g+(f*5.5));

}

}

redo()

{setfillstyle(1,8);

bar(576,369-((36-f)*5.5),589,369);

}

redrawbut()

{ hidemp();

setfillstyle(1,7);

bar(595,135,584,124);

Page 39: Tic Tac Toe Source Code

setcolor(0);

line(584,124,595,124);

line(584,124,584,135);

setcolor(15);

line(595,125,595,135);

line(595,135,585,135);

settextstyle(0,0,0);

setcolor(0);

outtextxy(587,128,"X");

settextstyle(1,0,1);

showmp();

}

int exitit()

{ unsigned imgsize;

void far *img;

hidemp();

imgsize=imagesize(190,190,460,310);

Page 40: Tic Tac Toe Source Code

if((img=malloc(imgsize))==NULL)

{ printf("Error");

exit(0);

}

getimage(190,190,460,310,img);

setfillstyle(1,7);

bar(200,200,450,300);

setlinestyle(0,0,1);

setcolor(15);

line(200,200,200,300);

line(200,200,450,200);

setcolor(0);

line(450,300,200,300);

line(450,300,450,200);

setcolor(8);

line(199,199,199,301);

line(199,199,451,199);

line(451,301,199,301);

line(451,301,451,199);

setfillstyle(1,BLUE);

Page 41: Tic Tac Toe Source Code

bar(202,202,448,214);

setcolor(15);

settextstyle(0,0,0);

outtextxy(204,206,"Exit");

setcolor(0);

settextstyle(2,0,0);

outtextxy(240,230,"Are you sure want to quit ?");

but(240,260,300,280,"Yes");

sel(240,260,300,280);

but(340,260,400,280,"No");

checkmp(img);

showmp();

return 0;

}

but(int x1,int y1,int x2,int y2,char ap[])

{ hidemp();

setfillstyle(1,7);

bar(x1,y1,x2,y2);

setlinestyle(0,0,0);

setcolor(0);

line(x2,y2,x2,y1);

line(x2,y2,x1,y2);

setcolor(15);

Page 42: Tic Tac Toe Source Code

line(x1,y1,x2,y1);

line(x1,y1,x1,y2);

setcolor(0);

settextstyle(2,0,0);

if(strcmp(ap,"Yes")==0)

outtextxy(x1+22,y1+4,ap);

if(strcmp(ap,"No")==0)

outtextxy(x1+26,y1+4,ap);

showmp();

return 0;

}

checkmp(void far *img)

{ int b,xx,yy;int mo=0;int no=0,yes=1,nosel=0,ysel=0;

int key;

ddd:

while(bioskey(1)!=0)

{

key=bioskey(0);

}

if(key==3849)

{ if(no==0)

{

no=1;

Page 43: Tic Tac Toe Source Code

hidemp();

sel(340,260,400,280);

but(240,260,300,280,"Yes");

showmp();

yes=0;

key=0;

goto jjj;

}

if(yes==0)

{

no=0;

hidemp();

sel(240,260,300,280);

but(340,260,400,280,"No");

showmp();

yes=1;

key=0;

goto jjj;

}

Page 44: Tic Tac Toe Source Code

} jjj:

if(key==7181)

{

while(key==7181)

{

if(yes==1)

{

press(240,260,300,280,"Yes");

exit(0);

}

if(no==1 && nosel==0)

{ nosel=1;

press(340,260,400,280,"No");

hidemp();

putimage(190,190,img,COPY_PUT);

free(img);

showmp();

settextstyle(1,0,1);

return;

}

while(bioskey(1)!=0)

key=bioskey(0);

Page 45: Tic Tac Toe Source Code

}

if(nosel==1)

{

but(340,260,400,280,"No");

}

}

showmp();

getmstat(&b,&xx,&yy);

if(xx>240 && yy>260 && xx< 300 && yy<280)

{ sel(240,260,300,280);

while(xx>240 && yy>260 && xx< 300 && yy<280)

{ if(b==1 && mo==0)

{ mo=1;

press(240,260,300,280,"Yes");

}

if(b==0 && mo==1)

{

mo=0;exit(0);

}

getmstat(&b,&xx,&yy);

Page 46: Tic Tac Toe Source Code

}

mo=0;

but(240,260,300,280,"Yes");

}

if(xx>340 && yy>260 && xx< 400 && yy<280)

{ sel(340,260,400,280);

while(xx>340 && yy>260 && xx< 400 && yy<280)

{

if(b==1 && mo==0)

{ mo=1;

press(340,260,400,280,"No");

}

if(b==0 && mo==1)

{

mo=0;

hidemp();

putimage(190,190,img,COPY_PUT);

free(img);

showmp();

settextstyle(1,0,1);

return;

}

Page 47: Tic Tac Toe Source Code

getmstat(&b,&xx,&yy);

}

mo=0;

but(340,260,400,280,"No");

}

goto ddd;

//but(340,260,400,280,"No");

return 0;

}

press(int x1,int y1,int x2,int y2,char msg[])

{

hidemp();

setfillstyle(1,7);

bar(x1,y1,x2,y2);

setlinestyle(0,0,0);

setcolor(15);

line(x2,y2,x2,y1);

line(x2,y2,x1,y2);

setcolor(0);

Page 48: Tic Tac Toe Source Code

line(x1,y1,x2,y1);

line(x1,y1,x1,y2);

setcolor(0);

settextstyle(2,0,0);

if(strcmp(msg,"Yes")==0)

outtextxy(x1+22,y1+5,msg);

if(strcmp(msg,"No")==0)

outtextxy(x1+26,y1+5,msg);

showmp();

return 0;

}

sel(int x1,int y1,int x2,int y2 )

{ hidemp();

setcolor(0);

setlinestyle(1,1,0);

line(x1+5,y1+3,x2-5,y1+3);

line(x1+5,y1+3,x1+5,y2-3);

line(x1+5,y2-3,x2-5,y2-3);

line(x2-5,y1+3,x2-5,y2-3);

showmp();

return 0;

}

Page 49: Tic Tac Toe Source Code

int cheet()

{

char *a;

scanf("%s",a);

if(strcmp(a,"win")==0)

{

hidemp();

outtextxy(400,300,"You Win!");

showmp();

return 3;

}

return 0;

}

//mouse.h

#ifndef __mouse_h__

#define __mouse_h__

#include<dos.h>

Page 50: Tic Tac Toe Source Code

#include<graphics.h>

int ox,oy;

m()

{

union REGS i,o;

i.x.ax=0;

int86(51,&i,&o);

return(o.h.al);

}

showmp()

{

union REGS i,o;

i.x.ax=1;

int86(51,&i,&o);

return 0;

}

hidemp()

{

union REGS i,o;

i.x.ax=2;

int86(51,&i,&o);

return 0;

}

restrictm(int x1,int y1,int x2,int y2)

{

Page 51: Tic Tac Toe Source Code

union REGS i,o;

i.x.ax=7;

i.x.cx=x1;

i.x.dx=x2;

int86(51,&i,&o);

i.x.ax=8;

i.x.cx=y1;

i.x.dx=y2;

int86(51,&i,&o);

return 0;

}

getmstat(int *b,int *xx,int *yy)

{

union REGS i,o;

char msg[10];

i.x.ax=3;

int86(51,&i,&o);

*b=o.x.bx;

*xx=o.x.cx;

*yy=o.x.dx;

if(*xx!=ox || *yy!=oy)

{

setfillstyle(0,0);

bar(540,440,630,460);

Page 52: Tic Tac Toe Source Code

setcolor(WHITE);

ox=*xx;

oy=*yy;

settextstyle(0,0,1);

sprintf(msg,"(%3d,%3d)",*xx,*yy);

outtextxy(550,450,msg);

settextstyle(1,0,1);

}

return 0;

}

int rclick(void)

{

int b,xx,yy;

getmstat(&b,&xx,&yy);

if(b==2) return 0;

else return 1;

}

int lclick(void)

{

int b,xx,yy;

getmstat(&b,&xx,&yy);

if(b==1) return 0;

else return 1;

}

int mclick(void)

Page 53: Tic Tac Toe Source Code

{

int b,xx,yy;

getmstat(&b,&xx,&yy);

if(b==3) return 0;

else return 1;

}

void setmpt(int x,int y)

{

union REGS i,o;

i.x.ax=4;

i.x.cx=x;

i.x.dx=y;

int86(51,&i,&o);

return;

}

#endif