Code:
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
void init_mouse();
void show_mouse();
int lb_prs();
void get_coord(int *,int *);
void hidemouse();
int chk_coord(int *,int *);
int board_fill[3][3]={0,0,0,0,0,0,0,0,0};
int main(void)
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "c:\\TUrboC3\\BGI" );
int x1,y1,x2,y2,i,j,but1=0,but2=0,p,q,turn=0;
outtext("Hello! And welcome to Tic Tac Toe. Press Enter to continue");
char ch;
getch();
cleardevice();
x1=0;
y1=0;
x2=0;
y2=0;
i=0;
j=0;
cleardevice();
setlinestyle(0,3,3);
line(210,200,390,200);
line(210,260,390,260);
line(270,140,270,320);
line(330,140,330,320);
init_mouse();
show_mouse();
setcolor(4);
void getmousepos(int *button, int *x1, int *y1)
{
i.x.ax = 3;
int86(0X33,&i,&o);
*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}
while(turn<9)
{
while(!kbhit())
{
getmousepos(&button,&x1,&y1);
i=chk_coord(&x1,&y1);
if(i==1)
if( button == 1 )
{
hidemouse();
settextstyle(0,0,3);
outtextxy(x1,y1,"X");
show_mouse();
turn++;
button = -1;
cleardevice();
}
else if( button == 2 )
{
hidemouse();
settextstyle(0,0,3);
outtextxy(x1,y1,"O");
show_mouse();
turn++;
button = -1;
cleardevice();
}
else
{
outtextxy(0,0,"Wrong input..");}
getch();
closegraph();
return 0;
}
void init_mouse()
{
union REGS in,out;
in.x.ax=0;
int86(51,&in,&out);
if(out.x.ax==0)
{
printf("Mouse driver failed!!!!");
exit(1);
}
}
void show_mouse()
{
union REGS in,out;
in.x.ax=1;
int86(51,&in,&out);
}
int lb_prs()
{
union REGS in,out;
in.x.ax=3;
int86(51,&in,&out);
return (out.x.bx & 1);
}
void get_coord(int *x,int *y)
{
union REGS in,out;
in.x.ax=0;
in.x.ax=3;
int86(51,&in,&out);
*x=out.x.cx;
*y=out.x.dx;
}
void hidemouse()
{
union REGS in,out;
in.x.ax=2;
int86(51,&in,&out);
}
int chk_coord(int *x1,int *y1)
{
int i=0;
int x=0,y=0;
x=*x1;
y=*y1;
if((x>=210)&&(y>=140)&&(x<=270)&&(y<=200))
{
*x1=230;
*y1=160;
if(board_fill[0][0]==0)
{
board_fill[0][0]=1;
return 1;
}
}
if((x>=270)&&(y>=140)&&(x<=330)&&(y<=200))
{
*x1=290;
*y1=160;
if(board_fill[0][1]==0)
{
board_fill[0][1]=1;
return 1;
}
}
if((x>=330)&&(y>=140)&&(x<=390)&&(y<=200))
{
*x1=350;
*y1=160;
if(board_fill[0][2]==0)
{
board_fill[0][2]=1;
return 1;
}
}
if((x>=210)&&(y>=200)&&(x<=270)&&(y<=260))
{
*x1=230;
*y1=220;
if(board_fill[1][0]==0)
{
board_fill[1][0]=1;
return 1;
}
}
if((x>=270)&&(y>=200)&&(x<=330)&&(y<=260))
{
*x1=290;
*y1=220;
if(board_fill[1][1]==0)
{
board_fill[1][1]=1;
return 1;
}
}
if((x>=330)&&(y>=200)&&(x<=390)&&(y<=260))
{
*x1=350;
*y1=220;
if(board_fill[1][2]==0)
{
board_fill[1][2]=1;
return 1;
}
}
if((x>=210)&&(y>=260)&&(x<=270)&&(y<=320))
{
*x1=230;
*y1=280;
if(board_fill[2][0]==0)
{
board_fill[2][0]=1;
return 1;
}
}
if((x>=270)&&(y>=260)&&(x<=330)&&(y<=320))
{
*x1=290;
*y1=280;
if(board_fill[2][1]==0)
{
board_fill[2][1]=1;
return 1;
}
}
if((x>=330)&&(y>=260)&&(x<=390)&&(y<=320))
{
*x1=350;
*y1=280;
if(board_fill[2][2]==0)
{
board_fill[2][2]=1;
return 1;
}
}
return 0;
}