Help me with my simple Tic tac toe prog

This is a discussion on Help me with my simple Tic tac toe prog within the C Programming forums, part of the General Programming Boards category; BTW this is not my homework...this is purely for learning....i started this tictactoe program and am almost done....thing is i ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Post Help me with my simple Tic tac toe prog

    BTW this is not my homework...this is purely for learning....i started this tictactoe program and am almost done....thing is i dont know what the hell is wrong with it....my scanf does not pass my variable correctly to my singleplayerturn function....im not sure im using the scanf function improperly or if my pointers are used incorrectly... thx for the help!!! dont change anything else i wanna do the rest just help me fix my problem
    Code:
    //Author: 
    //Description: Simple Tic Tac Toe game
    //
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAXNUMBERS 2
    
    
    int singlePlayer(char *,char *,char [3][3]);                                         //prototype for singleplayer
    void singleplayerTurn(int *,char *,char *,char [3][3]);                              // prototype for singleplayer turn picking function
    //void multiPlayer(char,int);                                                        //prototype for multiplayer
    
    int main()
    	{
    		char board[3][3]= { '-','-','-',                                     //tic tac toe board
      				            '-','-','-',
    				            '-','-','-'};
    		char player1;
    		char player2;
    		char computer;
    		
    		int playerFirst;                                                     //1 = player goes first    2 = computer goes first
    		
    		int menuselect;                                                      
    
            	int holdScreen;                                                      //for testing
    		char dummykey;                                      
    		
    		printf("\nWelcome to Tic Tac Toe");                   
    		printf("\n----------------------");
    		printf("\n1.) SinglePlayer      ");
    		printf("\n2.) Multiplayer       ");
    		printf("\n3.) Exit             \n");
    
            
            scanf("%d",&menuselect);
    		
            switch(menuselect)     
            {                                     
    			case 1:
    				printf("\nSinglePlayer Mode Selected.....LOADING");
    				printf("\n--------------------------------------");
    				playerFirst = singlePlayer(&player1,&computer,board);                       //single player function return value assigned to playerFirst
    				singleplayerTurn(&playerFirst,&player1,&computer,board);                    //depending on player turn make a rotation for it...let them input 'x' or 'o'....check to see if winner is found
                                    break;
    			case 2:
    				printf("\nMultiplayer Mode Selected.....LOADING");                          //not yet implemented
    				printf("");
             		      //multiPlayer(player1,player2,board[][]);
                                    break;
    			case 3:
    				printf("\nExiting....");                                                    //exit program
    				break;
    		}
          
    		return (0);
    	}
    
    //---------------------------------------------------
    //SinglePlayer Function of tic tac toe game
    //---------------------------------------------------
    int singlePlayer(char *playerOne,char *computerOne,char board[3][3])                                             //single player function to see who's 'x' and 'o'.....also check who goes first
    	{
    		int randomnumber;                       
    		int randvalue;   
            char dummy;                              
    		srand(time(NULL));			
    		
    		int playerFirst;				 
    	
    		printf("\nWhat would you like to be X's or O's?\n");
            scanf("%c%c",&dummy,&playerOne);
                                                          //allow user to input value 'x' or 'o'....dummykey eats up ENTER key
    
    		if (*playerOne == 'x')                                                                         //logical if to assign a computer 'x' or 'o'
    			{					 
    			*computerOne = 'o';				 
                }
    		else
                { 
    			*computerOne = 'x';
    			}
    		printf("\nNow lets decide who gets to go first!");
    		printf("\n-------------------------------------");
    		printf("\nPick a number between 1-2         \n");
    		scanf("%d",&randomnumber);
    		
    		randvalue = 1 + (int)rand() % MAXNUMBERS;    	        
    		
    		if (randomnumber == randvalue)
    			{
    				printf("\nCongratulations you guessed right on the dot! u get to go first!");
    				playerFirst = 1;
    			}
    		else
    			{
    				printf("\nBetter luck next time! Computer gets to go first :( .");
    				playerFirst = 0;
    			}
    		printf("\nThe number was....%d",randvalue);
    		printf("\nLet the Singleplayer game begin!!!!");
    		return (playerFirst);
    	}
    
    
    //---------------------------------------------------
    //SinglePlayer Turn Function 
    //---------------------------------------------------
    void singleplayerTurn(int *playerFirst,char *playerOneagain,char *computerOneagain,char board[3][3])
    	{
    
        int x,i;
        int delay;
        int holdScreen;                                  // count thru loop must = 9 
        int row;
        int column;
        int randomnumber;                                // user picked random number for deciding who goes first!
        int randvalue;                                   // program generated random number
                        
                  for (x = 0;x >= 0;x++)
                        {
    //
    //Players Turn rotation
    //	
          if (*playerFirst == 1)
          {          
                           printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                           printf("\n Where would you like to place your %c? ",*playerOneagain);
                           scanf("%d%d",&row,&column);
                           row -= 1;
                           column -=1;
                          
    		 if ( (row == 0 && column == 0) && (board[0][0] != 'x') && (board[0][0] != 'o') )
                              {
                                   board[0][0] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 0 && column == 1) && (board[0][1] != 'x') && (board[0][1] != 'o') )
                              {
                                   board[0][1] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 0 && column == 2) && (board[0][2] != 'x') && (board[0][2] != 'o') )
                              {
                                   board[0][2] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 1 && column == 0) && (board[1][0] != 'x') && (board[1][0] != 'o') )
                              {
                                   board[1][0] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 1 && column == 1) && (board[1][1] != 'x') && (board[1][1] != 'o') )
                              {
                                   board[1][1] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 1 && column == 2) && (board[1][2] != 'x') && (board[1][2] != 'o') )
                              {
                                   board[1][2] = *playerOneagain;
                                    *playerFirst = 0;
                                    continue;
                              }
                           if ( (row == 2 && column == 0) && (board[2][0] != 'x') && (board[2][0] != 'o') )
                              {
                                   board[2][0] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 2 && column == 1) && (board[2][1] != 'x') && (board[2][1] != 'o') )
                              {
                                   board[2][1] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           if ( (row == 2 && column == 2) && (board[2][2] != 'x') && (board[2][2] != 'o') )
                              {
                                   board[2][2] = *playerOneagain;
                                   *playerFirst = 0;
                                   continue;
                              }
                           else
                               {
                                   printf("\nInvalid row and column entered....or the spot is taken");
                                   printf("\n!please pick a new spot to put your %c",*playerOneagain);
                                   continue;
                               }
    	                   	
                         }
    //
    //Computers Turn rotation
    //	                   	
          if (*playerFirst == 0)
          {             
                           	srand(time(NULL));				 // RNG seed from time
    	                       
    	                   	for(i = 0; i <= 10000; i++) 
                                  {
    
                                  }
                                row = 0 + (int)rand() % 2;    	        // generates a random value between 0-2
    	                       	column = 0 + (int)rand() % 2;    	        // generates a random value between 0-2
    
    	                   	
    	                   	
    	                   if ( (row == 0 && column == 0) && (board[0][0] != 'x') && (board[0][0] != 'o') )
                              {
                                   board[0][0] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 0 && column == 1) && (board[0][1] != 'x') && (board[0][1] != 'o') )
                              {
                                   board[0][1] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 0 && column == 2) && (board[0][2] != 'x') && (board[0][2] != 'o') )
                              {
                                   board[0][2] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 1 && column == 0) && (board[1][0] != 'x') && (board[1][0] != 'o') )
                              {
                                   board[1][0] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 1 && column == 1) && (board[1][1] != 'x') && (board[1][1] != 'o') )
                              {
                                   board[1][1] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 1 && column == 2) && (board[1][2] != 'x') && (board[1][2] != 'o') )
                              {
                                   board[1][2] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 2 && column == 0) && (board[2][0] != 'x') && (board[2][0] != 'o') )
                              {
                                   board[2][0] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 2 && column == 1) && (board[2][1] != 'x') && (board[2][1] != 'o') )
                              {
                                   board[2][1] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           if ( (row == 2 && column == 2) && (board[2][2] != 'x') && (board[2][2] != 'o') )
                              {
                                   board[2][2] = *computerOneagain;
                                    printf("\n%c %c %c\n%c %c %c\n%c %c %c\n\n",board[0][0],board[0][1],board[0][2],board[1][0],board[1][1],board[1][2],board[2][0],board[2][1],board[2][2]);
                                   *playerFirst = 1;
                                   continue;
                              }
                           else
                               {
                                   continue;
                               }
                                 *playerFirst = 0;
    	                   	
          }
          }
    			
    }
    /*
    //---------------------------------------------------
    //Multiplayer Function of tic tac toe game
    //---------------------------------------------------
    void multiPlayer(char player1,char player2,char board[])
    	{
    
    		return (0);
    	}
    */

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    sorry for so much code but let me resay this
    Code:
    int singlePlayer(char *,char *,char [3][3]);                                         //prototype for singleplayer
    void singleplayerTurn(int *,char *,char *,char [3][3]);                              // prototype for singleplayer turn picking function
    
    
    
    
    
    
    int singlePlayer(char *playerOne,char *computerOne,char board[3][3])                                             //single player function to see who's 'x' and 'o'.....also check who goes first
    	{
    		int randomnumber;                       
    		int randvalue;   
                    char dummy;                              
    		srand(time(NULL));			
    		
    		int playerFirst;				 
    	
    		printf("\nWhat would you like to be X's or O's?\n");
            scanf("%c%c",&dummy,&playerOne);
                                                          //allow user to input value 'x' or 'o'....dummykey eats up ENTER key
    
    		if (*playerOne == 'x')                                                                         //logical if to assign a computer 'x' or 'o'
    			{					 
    			*computerOne = 'o';				 
                }
    		else
                { 
    			*computerOne = 'x';
    			}
    when i assign the player a value thru scanf it will know what to assign computer to x or o....for sum odd reason its either not carrying the values over to my next function or its not assigning playerOne and computerOne with the proper character values of x or o

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The "dummy" is pulling out any newline from left from a *previous* scanf(). It can't pull out the enter key for this scanf(), because the newline for it will be *behind* the char being entered.

    You may want to use Vart's trick of just putting a space before the
    scanf("%c", &variable), so that it skips right over the newline:

    scanf(" %c", &variable).

    Pretty slick.

    I believe you should try PlayerOne in your scanf(), since it is already a pointer. You can't assign a value to a pointer, so sending it one via scanf(), is a bug.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe program
    By muzihc in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 09:08 PM
  2. Making Tic Tac Toe Smarter
    By abh!shek in forum C Programming
    Replies: 15
    Last Post: 06-05-2008, 11:43 AM
  3. Tic Tac Toe AI help please...
    By Rune Hunter in forum Game Programming
    Replies: 12
    Last Post: 11-05-2004, 04:24 PM
  4. tic tac toe (arggg, I really need help!)
    By Leeman_s in forum Game Programming
    Replies: 3
    Last Post: 10-14-2001, 05:03 PM
  5. tic tac toe (arggg, I really need help!)
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 01:10 PM

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