Thread: Problem with using the mouse in TurboC++

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Problem with using the mouse in TurboC++

    Hello,
    I have been trying lately to make a simple 2 player TicTacToe program in TurboC++ using the graphics library ! I want to implement the program using mouse.
    I have written the getmpos() function to get the mouse position on clicking ! However,the mouse gets polled only for the first time,and for the rest of the times the previous input is taken. Hence I am experirncing a problem. Please help me !
    (Note:The TicTacToe logic is incomplete.)

    Code:
    #include <graphics.h>
    #include <stdlib.h>
    #include <conio.h>
    #include<iostream.h>
    
    class TTT
    {
    	int board[3][3];
    	int origx,origy,player1,player2,move;
    	public:
    	void init();
    	void initboard();
    	int win();
    	void drawsym(int,int,int);
    	void drawcross(int,int);
    	void drawnought(int,int);
    	void setmouse();
    	void getmpos(int*,int*);
    	void getblock(int*,int*);
    	void play();
    };
    
    void TTT::init()
    {
       clrscr();
       /* request auto detection */
       int gdriver = DETECT, gmode, errorcode;
       /* initialize graphics and local variables */
       initgraph(&gdriver, &gmode, "C:\\tc\\bgi");
       for(int i=0;i<3;i++)
    	for(int j=0;j<3;j++)
    		board[i][j]=0;
       move=0;
    }
    
    void TTT::initboard()
    {
    	int midx=getmaxx()/2;
    	int midy=getmaxy()/2;
    	origx=midx-180;origy=midy-180;
    	/*Draw 4 lines for board*/
    	line(midx-180,midy-60,midx+180,midy-60);
    	line(midx-180,midy+60,midx+180,midy+60);
    	line(midx-60,midy-180,midx-60,midy+180);
    	line(midx+60,midy-180,midx+60,midy+180);
    	/*board[0][0]='X';
    	if(board[0][0]=='X')
    	{
    		drawcross(1,1);
    	}
    	drawnought(0,0);*/
    };
    
    void TTT::drawcross(int m,int n)
    {
    	line(origx+m*120+10,origy+n*120+10,origx+m*120+110,origy+n*120+110);
    	line(origx+m*120+110,origy+n*120+10,origx+m*120+10,origy+n*120+110);
    }
    
    void TTT::drawnought(int m,int n)
    {
    	circle(origx+m*120+60,origy+n*120+60,50);
    }
    
    void TTT::drawsym(int sym,int m,int n)
    {
    	if(sym==1)
    		drawcross(m,n);
    	else if(sym==2)
    		drawnought(m,n);
    
    }
    
    int TTT::win()
    {
    	if((board[0][0]==1 && board[0][1]==1 && board[0][2]==1)||
    	   (board[0][0]==1 && board[1][1]==1 && board[2][2]==1)||
    	   (board[0][0]==1 && board[1][0]==1 && board[2][0]==1)||
    	   (board[0][1]==1 && board[1][1]==1 && board[2][1]==1)||
    	   (board[0][2]==1 && board[1][2]==1 && board[2][2]==1)||
    	   (board[0][2]==1 && board[1][1]==1 && board[2][0]==1)||
    	   (board[1][0]==1 && board[1][1]==1 && board[1][2]==1)||
    	   (board[2][0]==1 && board[2][1]==1 && board[2][2]==1))
    		return 1;                      //Computer Wins
    	if((board[0][0]==2 && board[0][1]==2 && board[0][2]==2)||
    	   (board[0][0]==2 && board[1][1]==2 && board[2][2]==2)||
    	   (board[0][0]==2 && board[1][0]==2 && board[2][0]==2)||
    	   (board[0][1]==2 && board[1][1]==2 && board[2][1]==2)||
    	   (board[0][2]==2 && board[1][2]==2 && board[2][2]==2)||
    	   (board[0][2]==2 && board[1][1]==2 && board[2][0]==2)||
    	   (board[1][0]==2 && board[1][1]==2 && board[1][2]==2)||
    	   (board[2][0]==2 && board[2][1]==2 && board[2][2]==2))
    		return 2;                     //Player Wins
    	else
    	{
    		int flag=1;
    		for(int i=0;i<3;i++)
    			for(int j=0;j<3;j++)
    				if(board[i][j]!=0)
    					flag=0;
    		if(flag==1)
    			return 0;			//Still Playing
    		return 3;				//Draw
    	}
    }
    
    void TTT::setmouse()
    {
    	asm mov ax,0
    	asm int 0x33
    	asm mov ax,1
    	asm int 0x33
    }
    
    void TTT::getmpos(int *x,int *y)
    {
    	int a=*x,b=*y;
    	asm xor bx,bx
    	asm xor cx,cx
    	asm xor dx,dx
    	asm xor ax,ax
    	asm mov ax,2
    	asm int 0x33
    	asm mov ax,1
    	asm int 0x33
    lp:	asm mov ax,3
    	asm int 0x33
    	asm and bx,1
    	asm cmp bx,1
    	asm jnz lp
    	asm mov a,cx
    	asm mov b,dx
    	cout<<"X = "<<a<<" Y = "<<b;
    	*x=a;*y=b;
    }
    
    void TTT::getblock(int *m,int *n)
    {
    	int x,y;
    	getmpos(&x,&y);
    	if(x>origx && x< origx+120 && y>origy && y<origy+120)
    	{
    		*m=0;*n=0;
    	}
    	else if(x>origx+120 && x<origx+240 && y>origy && y<origy+120)
    	{
    		*m=0;*n=1;
    	}
    	else if(x>origx+240 && x< origx+360 && y>origy && y<origy+120)
    	{
    		*m=0;*n=2;
    	}
    	else if(x>origx && x< origx+120 && y>origy+120 && y<origy+240)
    	{
    		*m=1;*n=0;
    	}
    	else if(x>origx+120 && x< origx+240 && y>origy+120 && y<origy+240)
    	{
    		*m=1;*n=1;
    	}
    	else if(x>origx+240 && x< origx+360 && y>origy+120 && y<origy+240)
    	{
    		*m=1;*n=2;
    	}
    
    }
    
    
    void TTT::play()
    {
    	int m,n;
    	init();
    	initboard();
    	setmouse();
    	player1=1;
    	player2=2;
    	int players;
    	while(move<9)
    	{
    		if(move%2==0)
    			players=player1;
    		else
    			players=player2;
    		getblock(&m,&n);
    		drawsym(players,n,m);
    		board[m][n]=players;
    		if(win()==2 || win()==1)
    			break;
    		move++;
    	}
    
    }
    
    int main(void)
    {
       TTT game;
       game.play();
       getch();
       closegraph();
       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,660
    Put the TurbidCrap++ in the bin, it's over 20 years old and half a dozen versions of windows have passed it by since then.
    Think about it, people who used this in the past stopped using it long ago. Few people remain who could actually answer your questions, and fewer still can be bothered with TC any more.

    Get a compiler which is compatible with your actual real OS, and not some hacky emulation of fossilised DOS.

    Examples
    codeblocks - Downloads
    smorgasbordet - Pelles C
    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.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by Salem View Post
    Put the TurbidCrap++ in the bin, it's over 20 years old and half a dozen versions of windows have passed it by since then.
    Think about it, people who used this in the past stopped using it long ago. Few people remain who could actually answer your questions, and fewer still can be bothered with TC any more.

    Get a compiler which is compatible with your actual real OS, and not some hacky emulation of fossilised DOS.

    Examples
    codeblocks - Downloads
    smorgasbordet - Pelles C

    All thats okay ! But it is mandatory to use TurboC++ in my college ! So please help me !

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If I remember correctly, you need to use subfunction 0x03 or 0x05 to poll the mouse. Something also tells me that I had issues with subfunction 0x05. I think 0x03 is the function you want to use. Keep in mind that the fact that any of this still works is a miracle since interrupts and real mode programming went the way of the dodo long long ago.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But it is mandatory to use TurboC++ in my college.
    You might want to ask them why this is so.
    In particular, ask them to justify how knowing this old stuff is going to prepare you for the outside world (because it isn't).
    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.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by VirtualAce View Post
    If I remember correctly, you need to use subfunction 0x03 or 0x05 to poll the mouse. Something also tells me that I had issues with subfunction 0x05. I think 0x03 is the function you want to use. Keep in mind that the fact that any of this still works is a miracle since interrupts and real mode programming went the way of the dodo long long ago.
    Thanks ! I have used the same function for polling the mouse ! However,it polls the mouse the first time the function getmpos() is called,but after that when the function getmpos() is called in the program,it does not poll the mouse but instead takes the previous result(that is previous click status and position) ! As a result,I am getting the crosses and noughts over the same position. Can you please check if anythings wrong with the getmpos() function,or whether I need to reset the mouse or do anything else ? Thanks !

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I looked this up in my Turbo C/C++ (ver. 1.01), and it doesn't have this function, at all. Instead, you apparently use the CP (current position), to maneuver as you want -- oh, wait a minute!

    Just remembered - I have a hella old reference book on this kind of stuff - let me dig it out and see what it has to say. Give me a few hours to find it and research it. I've used Turbo C for decades and never programmed anything using a mouse - weird eh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WHY IS GCC NOT AS GOOD AS OLD TurboC??
    By suryap.kv1bbsr in forum C Programming
    Replies: 11
    Last Post: 03-25-2011, 06:56 AM
  2. about turboc
    By kasun007 in forum C Programming
    Replies: 6
    Last Post: 03-07-2011, 12:56 PM
  3. problem installing TURBOC in my system
    By ss12 in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2005, 08:57 AM
  4. turboC++/Borland builder5 help!!!!
    By pete777 in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 09:04 PM
  5. Borland TurboC++ compiler
    By plivermo in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2001, 09:00 PM