Tic-tac-toe.

This is a discussion on Tic-tac-toe. within the Game Programming forums, part of the General Programming Boards category; Is it true that tic-tac-toe is very simple to make if so please point me in the direction to do ...

  1. #1
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27

    Tic-tac-toe.

    Is it true that tic-tac-toe is very simple to make if so please point me in the direction to do so.
    "Must of been high on something"

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    yes, go that way *points*

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Start by doing a search on this board, tic tac toe has had LOTS of questions asked about it.

  4. #4
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27
    Well would could I use 2-D arrays to display this. Cause I would know how to do this.
    "Must of been high on something"

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Jigga wha?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yes, a two dimensional array would be perfect.

  7. #7
    Registered User yodoblec's Avatar
    Join Date
    Jun 2003
    Posts
    27
    Thanks I'll let you know when it's done.
    "Must of been high on something"

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Not to take over this thread or anything, but what's wrong with this tic tac toe game? A draw game is impossible, and sometimes the winner isn't really the winner.

    Code:
    #include < iostream >
    #include < sstream  >
    #include <  string  >
    #include < conio.h  >
    using namespace std;
    
    char grid[ 3 ][ 3 ];  
    char check                 ( void );
    void init_grid             ( void );
    void get_player_move       ( void );
    void get_AI_move           ( void );
    void get_second_player_move( void );
    void disp_grid             ( void );
    void disp_help             ( void );
    void player_vs_AI          ( void );
    void player_vs_player      ( void );
    
    int main()
    {
    	char c_op;
    	char p_op; 
    	char play_again;
    	char s = putchar ( 1 );
    	int sm;
    	int b;
    	int t;
    	
                do
    	{
    		system ( "cls" );
    		cout << endl;
    		cout << "\t\t\t";
    		for ( sm = 0; sm < 31; sm++)
    			cout << s;
    		puts ( "\n\t\t\tTHIS IS THE GAME OF TIC TAC TOE" );
    		cout << "\t\t\t";
    		for ( sm = 0; sm < 31; sm++)
    			cout << s;	     
    		cout << endl;   
    	
    		do
    		{
    			puts ( "\nUse color codes to set the color you want" );	  
    			cout << "Background color:  ";
    			cin  >> b;
    	  
    			while ( b < 0 || b > 9 )
    			{
    				cin.clear  ();
    				cin.ignore ( INT_MAX, '\n' );
    		  
    				cout << "Enter a number from 0-9:  ";
    				cin  >> b;
    			}
    	  
    			cout << "Text color:  ";
    			cin  >> t;
    	  
    			while ( t < 0 || t > 9 )
    			{
    				cin.clear  ();
    				cin.ignore ( INT_MAX, '\n' );
    		  
    				cout << "Enter a number from 0-9:  ";
    				cin  >> t;
    			}
    	  
    			stringstream ( ( ( ss ) ) ); 	  
    			ss << "color " << b << t;
    			system( ss.str ().c_str () );
    
    			puts ( "\nAre you satisfied with your color choice? (Y/N)" );
    			cin  >> c_op;
    
    			while ( toupper ( c_op ) != 'Y' && toupper ( c_op ) != 'N' )
    			{
    				cin.clear  ();
    				cin.ignore ( INT_MAX, '\n' );
    		  
    				puts ( "\nEnter y for yes or n for no" );
    				cin  >> c_op;
    			}
    	
    		} while ( toupper ( c_op ) == 'N' );
    
    		puts ( "\nWould you like to play against the computer? (Y/N)" );
    		cin  >> p_op;
    	
    		while ( toupper ( p_op ) != 'Y' && toupper ( p_op ) != 'N' )
    		{
    			cin.clear  ();
    			cin.ignore ( INT_MAX, '\n' );
    
    			puts ( "\nEnter y for yes or n for no" );
    			cin  >> p_op;
    		}
    	
    		if ( toupper ( p_op ) == 'Y')
    			player_vs_AI ();
    	
    		else
    			player_vs_player   ();
    	
    		puts ( "\nWould you like to play again? (Y/N)" );
    		cin  >> play_again;
    
    		while ( toupper ( play_again ) != 'Y' && toupper ( play_again ) != 'N' )
    		{
    			cin.clear  ();
    			cin.ignore ( INT_MAX, '\n' );
    
    			puts ( "\nEnter y for yes or n for no" );
    			cin  >> play_again;
    		}
    	
    	} while ( toupper ( play_again ) == 'Y' );
    	
    	return 0;
    }
    
    void init_grid( void )
    {
    	int i;
    	int j;
    
    	for ( i = 0; i < 3; i++ )
    	{
    		for ( j = 0; j < 3; j++ ) 
    			grid[ i ][ j ] =  ' ';
    	}
    }
    
    void get_player_move( void )
    {
    	int x;	
    	int y;
    
    	cout << endl;
    	cout << "Enter the coordinates for your move: ";
    	cin  >> x >> y;
    
    	x--;
    	y--;
    
    	if ( x < 0 || x >= 3 || y < 0 || y >= 3 )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    	  
    		cout << "\nInvalid move, try again";
    		get_player_move ();
    	}
    	else 
    		grid[ x ] [y ] = 'X';
    }
    
    void get_AI_move( void )
    {
    	int i;
    	int j;
    
    	for ( i = 0; i < 3; i++ )
    	{
    		for ( j = 0; j < 3; j++ )
    			if ( grid[ i ][ j ] == ' ') 
    				break;
    			
    			if ( grid[ i ][ j ] == ' ' ) 
    				break;
    
    	}
    
    	if ( i * j == 9 )  
    	{
    		cout << "draw" << endl;
    		exit ( EXIT_SUCCESS );
    	}
      else
        grid[ i ][ j ] = 'O';
    }
    
    void get_second_player_move( void )
    {
    	int x;
    	int y;
    
    	cout << endl;
    	cout << "Enter the coordinates for your move: ";
    	cin  >> x >> y;
    
    	x--;
    	y--;
    
    	if ( x < 0 || x >= 3 || y < 0 || y >= 3 )
    	{
    		cin.clear  ();
    		cin.ignore ( INT_MAX, '\n' );
    	  
    		cout << "\nInvalid move, try again";
    		get_player_move ();
    	}
    	else if ( x * y == 4 )  
    	{
    		cout << "draw" << endl;
    		exit ( EXIT_SUCCESS );
    	}
    	else
    		grid[ x ][ y ] = 'O';
    }
    void disp_grid( void )
    {
    	int t;
    
    	cout << endl << endl;
     
    	for ( t = 0; t < 3; t++ ) 
    	{
    	  cout << " \t\t\t\t " << grid[t][0] << " | " 
    	                       << grid[t][1] << " | " 
    		                   << grid[t][2]; 
    	  if ( t != 2 ) 
    		  cout << "\n\n\t\t\t\t--- --- ---\n" << endl; 
    	}
    	cout << endl;
    }
    
    char check( void )
    {
    	/* Check rows across */
    	if ( grid[ 0 ][ 0 ] != ' ' 
    	  && grid[ 0 ][ 0 ] == grid[ 0 ][ 1 ] 
    	  && grid[ 0 ][ 1 ] == grid[ 0 ][ 2 ] )  
    	  return grid[ 0 ][ 0 ];
    
        if ( grid[ 1 ][ 0 ] != ' ' 
    	  && grid[ 1 ][ 0 ] == grid[ 1 ][ 1 ] 
          && grid[ 1 ][ 1 ] == grid[ 1 ][ 2 ] )
    	  return grid[ 1 ][ 0 ];
    
        if ( grid[ 2 ][ 0 ] != ' ' 
    	  && grid[ 2 ][ 0 ] == grid[ 2 ][ 1 ] 
    	  && grid[ 2 ][ 1 ] == grid[ 2 ][ 2 ] )
    	  return grid[ 2 ][ 0 ];
    
        /* Check rows down */
    	if ( grid[ 0 ][ 0 ] != ' ' 
    	  && grid[ 0 ][ 0 ] == grid[ 1 ][ 0 ] 
    	  && grid[ 1 ][ 0 ] == grid[ 2 ][ 0 ] )
    	  return grid[ 0 ][ 0 ];
    
        if ( grid[ 0 ][ 1 ] != ' ' 
    	  && grid[ 0 ][ 1 ] == grid[ 1 ][ 1 ] 
    	  && grid[ 1 ][ 1 ] == grid[ 2 ][ 1 ] )
    	  return grid[ 0 ][ 1 ];
    
        if ( grid[ 0 ][ 2 ] != ' ' 
    	  && grid[ 0 ][ 2 ] == grid[ 1 ][ 2 ] 
    	  && grid[  1][ 2 ] == grid[ 2 ][ 2 ] )
    	  return grid[ 0 ][ 2 ];
    
        /* Check diagonals */
    	if ( grid[ 0 ][ 0 ] != ' ' 
    	  && grid[ 0 ][ 0 ] == grid[ 1 ][ 1 ] 
    	  && grid[ 1 ][ 1 ] == grid[ 2 ][ 2 ] )
    	  return grid[0][0];
    
        if ( grid[ 2 ][ 0 ] != ' ' 
    	  && grid[ 2 ][ 0 ] == grid[ 1 ][ 1 ] 
    	  && grid[ 1 ][ 1 ] == grid[ 0 ][ 2 ] )
    	  return grid[ 2 ][ 0 ];
       
        return ' ';
    }
    
    void disp_help( void )
    {
    	cout << endl << endl;
    
    	cout << "1--1 | 1--2 | 1--3" << endl;
    	cout << "_____|______|_____" << endl;
    	cout << "2--1 | 2--2 | 2--3" << endl;
    	cout << "_____|______|_____" << endl;
    	cout << "3--1 | 3--2 | 3--3" << endl;
    }
    
    void player_vs_AI( void )
    {
    	char done;
    	char a = putchar ( 4 );
    	
    	done =  ' ';
      
    	init_grid ();
      
    	do
    	{
    		disp_grid ();
    		disp_help ();
    		get_player_move ();
    		done = check (); /* see if winner */
        
    		if( done != ' ' ) 
    			break; /* winner!*/
        
    		get_AI_move ();
        
    		done = check (); /* see if winner */
    
    		system ( "cls" );
    	
    	} while ( done == ' ' );
    	
    	system ( "cls" );
    
    	disp_grid ();
      
    	if( done == 'X' ) 
    		printf ( "\n\t\t\t\t%c%c WINNER %c%c\n", a, a, a, a );
    	
    	else 
    		printf ( "\n\t\t\t\t%c%c LOSER %c%c\n", a, a, a, a  ); 
    }
    
    void player_vs_player( void )
    {
    	char done;	
    	char a = putchar ( 4 );
    	
    	done =  ' ';
     
    	init_grid ();
      
    	do
    	{
    		disp_grid ();
    		disp_help ();
    		get_player_move ();
    
    		system ( "cls" );
    		disp_grid ();
    		disp_help ();
        
    		done = check (); /* see if winner */
        
    		if( done != ' ' ) 
    			break; /* winner!*/
        
    		get_second_player_move ();
        
    		done = check (); /* see if winner */
    
    		system ( "cls" );
    	
    	} while ( done == ' ' );
    	
    	system ( "cls" );
    	disp_grid ();
       
    	if( done == 'X' ) 
    		cout << "\n\t\t\t\t" << a << a << "Player 1 WINS" << a << a << endl;
    	
    	else 
    		cout << "\n\t\t\t\t" << a << a << "Player 2 WINS" << a << a << endl;
    
    }
    Last edited by volk; 06-08-2003 at 06:42 PM.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Global variables are naughty Volk.

    Other than that... get rid of the double spacing in your code and I'll examine it in more detail later.
    Away.

  10. #10
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Oh c'mon, there's just one global variable.

  11. #11
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The global variable hurts my eyes. It's that ugly.
    Away.

  12. #12
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I tried making it a constant and that gave me errors, then by locally declaring it in every function, I received some warnings.

    *sigh* I give up

  13. #13
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by volk
    I tried making it a constant and that gave me errors, then by locally declaring it in every function, I received some warnings.

    *sigh* I give up
    why not declare it in main and pass it to the funtions that need it.

  14. #14
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    There was nothing wrong with declaring a global variable in my program. The only reason you guys are afraid of it is because every function has access to it and might accidentally change it, causing errors that are difficult to debug.

    In my case, it caused no errors ( I think...), and the program is not even that big. It's just a fun little game. Of course I would never use global variables in a program that has hundreds of lines of code.

    Saying you should never use global variables is like saying you should never use goto statements. You should avoid them, of course, but you can use them here and there occasionally if you know what you're doing.

    So nah

    Ok, who am I kidding. I only wrote that because I didn't want to change my program. Too lazy...


  15. #15
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Volk, what is this block in the get_second_player_move() function supposed to do?

    Code:
    else if ( x * y == 4 )  
    {
    	cout << "draw" << endl;
    	exit ( EXIT_SUCCESS );
    }
    I know I helped you with the if statement before, but I'm not sure what you're hoping to accomplish with the code inside the block. I think it might be the source of your problem - can you explain it a bit?
    Away.

Page 1 of 2 12 LastLast
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
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21