Thread: Tic Tac Toe movement

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    hi I started making Tic Tac Toe, but I'm having a hard time making it move inside the box properly. it's like going out of bounds the box that I specifified. I hope you could help me with this code.. BTW I'm using Borland Turbo C++ 3.0. Language is C

    BTW, my boxes right there are ASCII codes so it's kinda messed right here..

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define LEFT 0x4B
    #define RIGHT 0X4D
    #define UP 0x48
    #define DOWN 0x50
    #define ENTER 0x0D
    #define ESC 0x1B
    
    void box( void );
    
    int main( void )
    {
       char board[ 3 ][ 3 ];
       char control;
       int colsPosition[] = { 31, 37, 43 };
       int rowPosition1[] = { 11, 14, 17 };
       int rowPosition2[] = { 12, 15, 18 };
       int travelRightToLeft = 0, travelLeftToRight = 0;
       int travelDown = 0, travelUp = 0;
    
       clrscr();
       box();
    
    
       do
       {
          control = getch();
          switch( control )
          {
    	  case LEFT:
    		      clrscr();
    		      box();
    
    		      if( travelRightToLeft > 0 )
    		      {
    			   travelRightToLeft--;
    			   textcolor( LIGHTGREEN );
    			   gotoxy( colsPosition[ travelRightToLeft ], rowPosition1[ travelDown ] );cprintf( "&#201;&#205;&#205;&#205;&#187;" );
    			   gotoxy( colsPosition[ travelRightToLeft ], rowPosition2[ travelDown ] );cprintf( "&#200;&#205;&#205;&#205;&#188;" );
    			   travelLeftToRight--;
    		      }
    		      else { }
    	  break;
    	  case RIGHT:
    		      clrscr();
    		      box();
    
    		      if( travelLeftToRight < 3 )
    		      {
    			   textcolor( LIGHTGREEN );
    			   gotoxy( colsPosition[ travelLeftToRight ], rowPosition1[ 0 ] );cprintf( "&#201;&#205;&#205;&#205;&#187;" );
    			   gotoxy( colsPosition[ travelLeftToRight ], rowPosition2[ 0 ] );cprintf( "&#200;&#205;&#205;&#205;&#188;" );
    			   travelLeftToRight++;
    			   travelRightToLeft++;
    		      }
    		      else { }
    	  break;
    	  case DOWN:
    		      clrscr();
    		      box();
    
    		      if( travelDown < 3 )
    		      {
    			   travelDown++;
    			   textcolor( LIGHTGREEN );
    			   gotoxy( colsPosition[ travelRightToLeft ], rowPosition1[ travelDown ] );cprintf( "&#201;&#205;&#205;&#205;&#187;" );
    			   gotoxy( colsPosition[ travelRightToLeft ], rowPosition2[ travelDown ] );cprintf( "&#200;&#205;&#205;&#205;&#188;" );
    			   travelUp++;
    		      }
    		      else { }
    	  break;
    	  case UP:
    		      clrscr();
    		      box();
    
    		      if( travelUp > 0 )
    		      {
    			   textcolor( LIGHTGREEN );
    			   travelUp--;
    			   gotoxy( colsPosition[ travelLeftToRight ], rowPosition1[ travelUp ] );cprintf( "&#201;&#205;&#205;&#205;&#187;" );
    			   gotoxy( colsPosition[ travelLeftToRight ], rowPosition2[ travelUp ] );cprintf( "&#200;&#205;&#205;&#205;&#188;" );
    			   travelDown--;
    		      }
    		      else { }
    	  break;
    
    
    	  default:
    	  break;
          }
       }while( control != ESC );
    
       return 0;
    }
    void box( void )
    {
       textcolor( CYAN );
       gotoxy( 30, 5 );cprintf(  "My Tic-Tac-Toe" );
    
       //board
       textcolor( BLUE );
       gotoxy( 30, 10 );cprintf( "&#201;&#205;&#205;&#205;&#205;&#205;&#203;&#205;&#205;&#205;&#205;&#205;&#203;&#205;&#205;&#205;&#205;&#205;&#187;" );
       gotoxy( 30, 11 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 12 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 13 );cprintf( "&#204;&#205;&#205;&#205;&#205;&#205;&#206;&#205;&#205;&#205;&#205;&#205;&#206;&#205;&#205;&#205;&#205;&#205;&#185;" );
       gotoxy( 30, 14 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 15 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 16 );cprintf( "&#204;&#205;&#205;&#205;&#205;&#205;&#206;&#205;&#205;&#205;&#205;&#205;&#206;&#205;&#205;&#205;&#205;&#205;&#185;" );
       gotoxy( 30, 17 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 18 );cprintf( "&#186;     &#186;     &#186;     &#186;" );
       gotoxy( 30, 19 );cprintf( "&#200;&#205;&#205;&#205;&#205;&#205;&#202;&#205;&#205;&#205;&#205;&#205;&#202;&#205;&#205;&#205;&#205;&#205;&#188;" );
    }
    I cannot make a dummy box right here.. but there is a box that has 9 windows. I have a little box inside the upper left window which should be the start position. Then that box is the box I wanted to move inside each of the windows.
    Last edited by $l4xklynx; 10-29-2008 at 11:36 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I still can't make it move.. I hope you could give me some idea.. thanks in advanced

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    thanks for the advice.

    I was able to make it move that way but the problem is it's going out of bound of the box. I just want to be moving inside the box as I wanted. Is there anything else you can advice to me?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add this header file:
    #include <dos.h>

    I really want you to try and solve this one.

    Put this in, right after the control = getch() line:
    gotoxy(20, 20);
    printf("control is: %c", control);
    sleep(3);

    You have a *very* Rube Goldberg way of controlling a simple box movement. Travel Left to Right, Travel Right to Left, Travel Up, Travel Down, etc. I was expecting "Travel Upside Down" any second, to appear.

    You don't want any of that. All you need is a simple box variable. When you go straight up, decrement box by 3, when you go down on the screen, you increment box by 3. When you go left, you decrement box by one, when you go right, you increment box by one. When you go down to 8 o'clock, you increment by 4, when you go up to 10 o'clock you decrement box by 4, etc.

    Now you use some if statements to set your row and col (y and x) values. It may not seem as elegant as what you've got, but it has one redeeming feature - you can easily debug it. What you have now is a PITA to debug, as you've no doubt seen for yourself.

    Later, after it's working, if you want to take the correct values for box row and col values, and put them in an array, fine. But do that AFTER you've got them working, or at least got the logic figured out in your head, clearly.

    Keep it simple and clear.

    A big problem with beginners is writing up lots of code, and *then* de-bugging it. Uh uh! You write up a little bit of the "spine" of your code for each function, and then you test it and make sure it compiles, links, and runs correctly so far. Then you add a bit more, and repeat.

    When I'm writing code for things like this, I use a define lm 20 (whatever), as my left margin. tm is my top margin.
    Now the top left box's coordinates are lm + (width of box/2) - (width of inner box)/2. I believe that will put your cursor right on the leftmost border of your inner box for that square, so you're ready to print your first char. It sounds complicated, but it's really rather simple once you get into it. lm puts you at the leftmost side of the leftmost box, + (width of box/2) will take your cursor to the center of the leftmost box. Now subtract the width of your inner box/2, and you're there.

    I use the same idea for the top, except I use tm instead of lm. Of course, the width of box can be a define, etc.

    To get to the highest char row in the second row of boxes, it's just tm + height of box in chars.

    This is an even simpler approach to printing up a TTT board:

    Code:
     
    printf("    %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
    printf("   ---|---|---\n");
    etc.

    I can't say it's from one of my programs, or it's original, but it's 100 X easier to do correctly, and debug if necessary, than what you have now. For each box, a '0' value could indicate an empty box, a 'X' would show an X and etc.

    We're not building piano's here, it's just a simple TTT board. If you want to spiff it up after you get it working right, then fine. Avoid building the piano first, and then wondering where the error in logic may be.
    Last edited by Adak; 11-05-2008 at 11:40 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    Talking

    Rube Goldberg hehehehe... this is not a project or whatsoever it's just that I wanted to practice myself more in programming. I like giving programming problems for myself .. I'll finish this, I'm just consolidating the ideas that I'm acquiring..

    Thanks for the advice.. I'll keep you updated..
    Last edited by $l4xklynx; 11-06-2008 at 07:28 PM.

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 Neural Network
    By glo in forum General AI Programming
    Replies: 4
    Last Post: 06-15-2008, 03:39 PM
  3. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  4. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  5. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM