Thread: Need Help TicTacToe 'C'

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    4

    Need Help TicTacToe 'C'

    I need help with a tic tac toe game.
    I picked this up from a site, edited it and got it to work but I am stuck. The Game is TicTacToe and there needs to be sequential turns but there is an error in it. The turns are not sequential, there are some 'X' and then 'O'. Please look at the piece of the code.


    Code:
    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();
        return0;
    }
    
    
     

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    1. Replace the goto's with while loops.

    3. Replace the copy/paste code with a single function, along the lines of
    Code:
    while ( turn < 9 ) {
      player('X');
      player('0');
    }
    Now make the copy/pasted code a function which substitutes a parameter in the place where the code is actually different.

    2. if( lb_prs())
    These need to be while loops, to cause the program to wait until the respective player has made a choice.

    3. Replace the copy/paste code with a single function, along the lines of
    Code:
    while ( turn < 9 ) {
      while ( !lb_prs() );  // wait for button
      player('X');  // do stuff for the 'X' player
      turn++;
      if ( turn < 9 ) {
        while ( !lb_prs() );  // wait for button
        player('0');
        turn++;
      }
    }
    Now make the copy/pasted code a function which substitutes a parameter in the place where the code is actually different.
    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
    Jun 2017
    Posts
    4
    Wow! Thanks, man.
    Sorry, but you see the problem is that I was taught TurboC++ instead of C. So, I am not very good at this. I only know a handful of generic terms. So, I am not very sure what you are saying. It will be really helpful if you could be more layman.

    Sorry.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    I didn't say anything that wasn't useful regardless of whether you're using C or C++.

    Using goto's and copy/pasting code are universally bad ideas.

    I thought it smelt bad -> Tic-tac-toe using mouse

    How about showing us what code you can actually write yourself for a change?
    Because showing you how to put a bandage on broken code isn't going to teach you anything worthwhile in the long run.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    4
    I sorry If I somehow led you astray.
    What I was taught was pretty basic and elementary but the teacher gave us very difficult projects.
    I did mention that I copied it.
    The code that I made is
    Code:
    
    
    Code:
    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..");}
    For this, since the loop wasn't working for me no matter what, I tried making it so that the mouse buttons produce x and y when pressed.
    I found it more easier than creating difficult loops.
    But it is not working. Even more so than the original code.
    Again, I copied the code because I haven't been taught to code very well, the most we did in our class was to count number of letters by increment and to move a circle on screen with mouse(I learned this one because that is how we do things here).
    Last edited by Maheshvara; 06-04-2017 at 06:43 AM.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    4
    While the whole copied and edited code is

    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(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;
    }


    ​Thanks Again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    > I sorry If I somehow led you astray.
    > What I was taught was pretty basic and elementary but the teacher gave us very difficult projects.
    And if you suddenly hand in code which is way beyond what you've been taught, and what they know you're capable of, then what?

    Like I said, you need to write your own code for your own assignment, not take the first piece of crappy software google finds for you, then wonder why it doesn't work.

    I've absolutely no interest in helping people pass homework / exams they're obviously incapable of passing through their own efforts.
    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. TicTacToe
    By Loic in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2007, 01:09 AM
  2. TicTacToe AI
    By Loic in forum C++ Programming
    Replies: 9
    Last Post: 05-28-2007, 01:37 PM
  3. C++ Tictactoe
    By vinter in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2007, 09:56 PM
  4. TicTacToe OOP
    By NickESP in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2003, 11:47 AM
  5. TicTacToe
    By neoground73 in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2002, 05:32 PM

Tags for this Thread