Thread: Tic-tac-toe using mouse

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    Tic-tac-toe using mouse

    Hi,
    I am new to this forum.I was just trying to do a tic tac toe game using mouse,but i am facing some wierd problem.I was trying to do like this,on a left button click i should get "x",then on the next click i should get "o",but the combination is not right.Here is the code:

    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()
    {
    int gdriver=DETECT,gmode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    int x1,y1,x2,y2,i,j,but1=0,but2=0,p,q,turn=0;
    char ch;
    
    choose_menu();
    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(3);
    
    while(turn<9)
    {
    
    a:
    x1=0;
    y1=0;
    i=0;
    
    if(lb_prs())
    	{
    	get_coord(&x1,&y1);
    	i=chk_coord(&x1,&y1);
    	while(lb_prs());
    	if(i==1)
    		{
    		hidemouse();
    		settextstyle(0,0,3);
    		outtextxy(x1,y1,"X");
    		show_mouse();
    		turn++;
    		}
    	else
    		{
    		outtextxy(0,0,"Wrong input..");
    		goto a;
    		}
    }
    
    b:
    x1=0;
    y1=0;
    i=0;
    
    if(lb_prs())
    	{
    	get_coord(&x1,&y1);
    	i=chk_coord(&x1,&y1);
    	while(lb_prs());
    	if(i==1)
    		{
    		hidemouse();
    		settextstyle(0,0,3);
    		outtextxy(x1,y1,"O");
    		show_mouse();
    		turn++;
    		}
    	else
    		{
    		outtextxy(0,0,"Wrong input..");
    		goto b;
    		}
    	}
    
    }
    
    getch();
    closegraph();
    return 0;
    }
    
    
    void init_mouse()
    {
    union REGS in,out;
    
    in.x.ax=0;
    int86(0x33,&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(0x33,&in,&out);
    }
    
    
    int lb_prs()
    {
    union REGS in,out;
    in.x.ax=3;
    
    int86(0x33,&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(0x33,&in,&out);
    
    *x=out.x.cx;
    *y=out.x.dx;
    }
    
    void hidemouse()
    {
    union REGS in,out;
    
    in.x.ax=2;
    int86(0x33,&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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lemme guess, another TurboC user trying to do something more than "hello world" on top of XP?

    Your compiler was rendered OBSOLETE 20 YEARS AGO, why are you still using it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Tic Tac Toe... so close...
    By SlayerBlade in forum C Programming
    Replies: 14
    Last Post: 10-10-2005, 08:58 PM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM