Thread: Tic Tac Toe program problem

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    18

    Tic Tac Toe program problem

    I have to make a tic tac toe program and I am struggling. This is the function that I have currently. This function is suppose to determine if there is a winner or a Cats game. With this function, my program places a X or an O wherever the user has typed they want it. (This works) but it only runs once and that is where I am struggling. I need it to run until it finds a winner or if it a Cats game. Any help would be appreciated.


    Code:
    int checkForWin(int *piece, char *letter){
    	int r,c;
    	char board[3][3]={{'1','2','3'},
    				  {'4','5','6'},
    			      	  {'7','8','9'}};					  
    
    
    		if (&*letter=='x' || &*letter=='X')
    		{
    			printf("\nYou are player X.\n\n");
    		} 
    		else printf("\nYou are player O.\n\n");
    
    
    			if ((&*letter=='x' || &*letter=='X') && &*piece==1)
    			{
    				board[0][0]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==2)
    			{
    				board[0][1]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==3)
    			{
    				board[0][2]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==4)
    			{
    				board[1][0]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==5)
    			{
    				board[1][1]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==6)
    			{
    				board[1][2]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==7)
    			{
    				board[2][0]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='x' || &*letter=='X') && &*piece==8)
    			{
    				board[2][1]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			 
    			if ((&*letter=='x' || &*letter=='X') && &*piece==9)
    			{
    				board[2][2]='X';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==1)
    			{
    				board[0][0]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==2)
    			{
    				board[0][1]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==3)
    			{
    				board[0][2]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==4)
    			{
    				board[1][0]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==5)
    			{
    				board[1][1]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==6)
    			{
    				board[1][2]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==7)
    			{
    				board[2][0]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			
    			if ((&*letter=='o' || &*letter=='O') && &*piece==8)
    			{
    				board[2][1]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}
    			 
    			if ((&*letter=='o' || &*letter=='O') && &*piece==9)
    			{
    				board[2][2]='O';
    					for (r=0;r<3;r++)
    					{
    		
    						for (c=0;c<3;c++)
    						{
    						printf("%c ",board[r][c]);
    									
    						}					
    					if (r==0 || r==1)
    					{
    					printf("\n");	
    					}
    					}
    			}    
    		
    return 0;	
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Well, a few things:

    If you haven't enabled your compiler warnings you should do so - it will make your coding experience a lot easier. If you don't know how, try googling on your compiler version.


    Code:
    if ((&*letter=='o' || &*letter=='O') && &*piece==9)
    This code and your various versions of it are wrong. You probably wanted

    Code:
    if ((*letter=='o' || *letter=='O') && *piece==9)
    The function appears to be a player move function rather than one to check for the game end, and includes 18 occurences of the code:

    Code:
                            for (r=0;r<3;r++){
             
                                for (c=0;c<3;c++)
                                {
                                     printf("%c ",board[r][c]);
              
                                }                   
                                if (r==0 || r==1)
                                {
                                    printf("\n");   
    
                                }
                            }
    So your code could be greatly simplified by placing the board drawing code in its own function.

    That will give you a start to fixing some of the issues, but it would probably help you even more to forget the code for a moment and sit down with paper and pen and plan your program out by breaking it down into well defined functions.
    Last edited by gemera; 04-20-2015 at 12:03 PM.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by gemera View Post
    Well, a few things:

    If you haven't enabled your compiler warnings you should do so - it will make your coding experience a lot easier. If you don't know how, try googling on your compiler version.


    Code:
    if ((&*letter=='o' || &*letter=='O') && &*piece==9)
    This code and your various versions of it are wrong. You probably wanted

    Code:
    if ((*letter=='o' || *letter=='O') && *piece==9)
    
    I tried that and my program crashes whenever I run it. It works the way I put it or when I take out &* and keep it as letter=='o'.
    
    The function appears to be a player move function rather than one to check for the game end, and includes 18 occurences of the code:

    Code:
                            for (r=0;r<3;r++){
             
                                for (c=0;c<3;c++)
                                {
                                     printf("%c ",board[r][c]);
              
                                }                   
                                if (r==0 || r==1)
                                {
                                    printf("\n");   
    
                                }
                            }
    So your code could be greatly simplified by placing the board drawing code in its own function.

    That will give you a start to fixing some of the issues, but it would probably help you even more to forget the code for a moment and sit down with paper and pen and plan your program out by breaking it down into well defined functions.

    I am completely lost right now. Can you push me in the right direction?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Post your complete code.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by gemera View Post
    Post your complete code.
    Code:
    
    #include<stdio.h>
    void displayBoard();
    int selectLocation(int *);
    int setTurn(char *);
    void checkForWin();
    
    
    int main()
    {
    int *piece;
    char yes, *letter;
    
    
        do
        {
        displayBoard();    
        selectLocation(&piece);
        setTurn(&letter);
        checkForWin();
    
    
        printf("\n\n\nPress Y if you want to play again.\n");
        scanf("\n%c",&yes);
        } while (yes=='y' || yes=='Y');
    
    
    return 0;
    }
    
    
    void displayBoard()
    {
        int r,c;
        char board[3][3]={{'1','2','3'},
                          {'4','5','6'},
                          {'7','8','9'}};
        printf("\nHello, \n\nThis is your game board.\n\n");
        
        for (r=0;r<3;r++)
        {
            
            for (c=0;c<3;c++)
            {
            printf("%c ",board[r][c]);            
            }
            if (r==0 || r==1)
            {
            printf("\n");    
            }
        }    
    }
        
    int selectLocation(int *piece)
    {        
        do
        {
            printf("\n\nPlease type the number where you want to place your piece: ");        
            scanf("%d",piece);    
        
            if (*piece!=1&& *piece!=2&& *piece!=3 && *piece!=4 && *piece!=5 && *piece!=6 && *piece!=7 && *piece!=8 && *piece!=9)
            {        
            printf("\n\nEror: Not a correct number. Please try again\n\n");    
            }
            
        }while (*piece!=1&& *piece!=2&& *piece!=3 && *piece!=4 && *piece!=5 && *piece!=6 && *piece!=7 && *piece!=8 && *piece!=9);
        
        
    return *piece;    
    }
    
    
    int setTurn(char *letter)
    {
        do
        {
            printf("\n\nDo you want to place a X or an O? ");        
            scanf("\n%c",letter);    
        
            if (*letter!='x' && *letter!='X' && *letter!='o' && *letter!='O')
            {        
            printf("\n\nEror: Not a correct option. Please try again\n\n");    
            }
        }while (*letter!='x' && *letter!='X' && *letter!='o' && *letter!='O');
                
    return *letter;    
    }
    Here it is without the checkForWin. I changed it from an int to a void trying to get it work but I am still stuck.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Don't have the time to check it in detail ( maybe some of the other guys will chime in )

    But if you are passing your piece and letter variables from main() like that, they shouldn't be pointers.

    What you want is

    int piece;

    char letter;

    Also whereas its good that you now have a function to print the board, the actual declaration of the array for the board should be in main() so that you can pass it to the various functions that need it, including displayBoard().

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by gemera View Post
    Don't have the time to check it in detail ( maybe some of the other guys will chime in )

    But if you are passing your piece and letter variables from main() like that, they shouldn't be pointers.

    What you want is

    int piece;

    char letter;

    Also whereas its good that you now have a function to print the board, the actual declaration of the array for the board should be in main() so that you can pass it to the various functions that need it, including displayBoard().

    I tried changing *piece to piece used a printf to see what it was returning and it was no longer returning the value. It would always return 0.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at this snippet:
    Code:
     
    int main()
    {
    int *piece;
    char yes, *letter;
    Where are you allocating memory to those pointers?

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    Look at this snippet:
    Code:
     
    int main()
    {
    int *piece;
    char yes, *letter;
    Where are you allocating memory to those pointers?

    Jim
    What do you mean exactly? I declared them there and I declared them in their own functions.
    Code:
    intselectLocation(int*piece){        
        do
        {
            printf("\n\nPlease type the number where you want to place your piece: ");        
            scanf("%d",piece);    
    
            if (*piece!=1&& *piece!=2&& *piece!=3 && *piece!=4 && *piece!=5 && *piece!=6 && *piece!=7 && *piece!=8 && *piece!=9)
            {        
            printf("\n\nEror: Not a correct number. Please try again\n\n");    
            }
    
        }while (*piece!=1&& *piece!=2&& *piece!=3 && *piece!=4 && *piece!=5 && *piece!=6 && *piece!=7 && *piece!=8 && *piece!=9);
    
    
    return *piece;    
    }
    
    
    int setTurn(char *letter)
    {
        do
        {
            printf("\n\nDo you want to place a X or an O? ");        
            scanf("\n%c",letter);    
    
            if (*letter!='x' && *letter!='X' && *letter!='o' && *letter!='O')
            {        
            printf("\n\nEror: Not a correct option. Please try again\n\n");    
            }
        }while (*letter!='x' && *letter!='X' && *letter!='o' && *letter!='O');
    
    return *letter;    
    }

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I mean where have you allocated memory for those pointers? Just declaring a pointer variable doesn't provide memory for the pointers, you need to either allocate memory using malloc() or use assignment to point to an existing variable.




    Jim

  11. #11
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    I mean where have you allocated memory for those pointers? Just declaring a pointer variable doesn't provide memory for the pointers, you need to either allocate memory using malloc() or use assignment to point to an existing variable.




    Jim

    Oh yeah. I forgot to do that. I'll look into it when I get home.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But do note, you probably shouldn't be using the pointers in main(), just use "standard" non-pointer variables. Also several of your functions should probably be returning nothing (void) because you're trying to pass the parameters by pointer, which means that the changes made in the functions will be reflected in the calling functions.

    Jim

  13. #13
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    @Kobeissi2

    Reviewing chapters on pointers and functions in your C book would be a good idea.

    That should give you the necessary confidence and understanding in passing variables and arrays to functions.

  14. #14
    Registered User
    Join Date
    Mar 2015
    Posts
    18
    Quote Originally Posted by jimblumberg View Post
    But do note, you probably shouldn't be using the pointers in main(), just use "standard" non-pointer variables. Also several of your functions should probably be returning nothing (void) because you're trying to pass the parameters by pointer, which means that the changes made in the functions will be reflected in the calling functions.

    Jim
    I fixed it this way and it works now. I just don't know how to actually determine the winner.

    Code:
    #include<stdio.h>
    void displayBoard();
    int selectLocation(int);
    char setTurn(char);
    void checkForWin(int, char);
    
    
    int main()
    {
    int piece;
    char yes, letter;
    
    
        do
        {
        displayBoard();    
        piece=selectLocation(piece);
        letter=setTurn(letter);
        checkForWin(piece, letter);
        
        printf("\n\n\nPress Y if you want to play again.\n");
        scanf("\n%c",&yes);
        } while (yes=='y' || yes=='Y');
    
    
    return 0;
    }
    
    
    void displayBoard()
    {
        int r,c;
        char board[3][3]={{'1','2','3'},
                          {'4','5','6'},
                          {'7','8','9'}};
        printf("\nHello, \n\nThis is your game board.\n\n");
        
        for (r=0;r<3;r++)
        {
            
            for (c=0;c<3;c++)
            {
            printf("%c ",board[r][c]);            
            }
            if (r==0 || r==1)
            {
            printf("\n");    
            }
        }    
    }
        
    int selectLocation(int piece)
    {    
        int *piecePtr;
        piecePtr=&piece;    
        do
        {
            printf("\n\nPlease type the number where you want to place your piece: ");        
            scanf("%d",&piece);    
        
            if (*piecePtr!=1&& *piecePtr!=2&& *piecePtr!=3 && *piecePtr!=4 && *piecePtr!=5 && *piecePtr!=6 && *piecePtr!=7 && *piecePtr!=8 && *piecePtr!=9)
            {        
            printf("\n\nEror: Not a correct number. Please try again\n\n");    
            }
            
        }while (*piecePtr!=1&& *piecePtr!=2&& *piecePtr!=3 && *piecePtr!=4 && *piecePtr!=5 && *piecePtr!=6 && *piecePtr!=7 && *piecePtr!=8 && *piecePtr!=9);
        
        return piece;
    }
    
    
    char setTurn(char letter)
    {
        char *letterPtr;
        letterPtr=&letter;
        do
        {
            printf("\n\nDo you want to place a X or an O? ");        
            scanf("\n%c",&letter);    
        
            if (*letterPtr!='x' && *letterPtr!='X' && *letterPtr!='o' && *letterPtr!='O')
            {        
            printf("\n\nEror: Not a correct option. Please try again\n\n");    
            }
        }while (*letterPtr!='x' && *letterPtr!='X' && *letterPtr!='o' && *letterPtr!='O');
        
        return letter;
    }
    
    
    void checkForWin(int piece, char letter)
    {
         int r,c;
         char board[3][3]={{'1','2','3'},
                           {'4','5','6'},  
                           {'7','8','9'}};        
    }
    Quote Originally Posted by gemera View Post
    @Kobeissi2

    Reviewing chapters on pointers and functions in your C book would be a good idea.

    That should give you the necessary confidence and understanding in passing variables and arrays to functions.
    I do not have a C book. I got that part working though. I just do not truly understand how to determine the winner. I also do not know how to keep giving the player turns until someone wins.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    If you don't have a good C book, I would highly recommend you get one. Nothing beats a good structured approach to learning programming imo.

    There's also a good online course by Steve Summit here. That's an excellent course, but I would still say it isn't a substitute for a good book.

    Looking at your code, you still need to declare the board array in main() and pass it to the functions that need it.

    As far as checking for the end of a tic tac toe game - this kind of problem solving is where the real programming begins, and often involves planning and refining code before writing it.

    So I'm only going to point you in the right direction:

    Imagine that you have a program with a 1 - Dimensional array of 3 elements, and the program takes each element in turn and randomly decides if the the element should be an X or a Y or empty.

    So you could have 4 different results such as

    Code:
    1) XXX
    
    2) OXO
    
    3) _X_
    
    4) _OX
    (Where _ is an empty element)

    How would you check for an end result of three X or three Y?

    A basic way would be to have an X counter and a Y counter.

    Code:
    int x_counter;
    int y_counter;
    Each element is considered in turn and if an X or a Y is found the appropriate counter incremented.

    If an element is found at any point to be empty, the code could break out immediately.

    At the end of the process if either counter is 3, then you know there is either 3 X or 3 Y.

    With the 2D array in tac tac toe there is clearly an element of redundancy in how many elements need to be checked, but the general process would be similar.
    Last edited by gemera; 04-23-2015 at 12:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program problem
    By Khalil Collins in forum C Programming
    Replies: 13
    Last Post: 03-07-2014, 04:31 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. What's the problem about this program?
    By Mathsniper in forum C Programming
    Replies: 18
    Last Post: 12-18-2006, 07:44 AM
  4. i have a problem on my program, please help!
    By ok_good in forum C++ Programming
    Replies: 13
    Last Post: 11-19-2005, 04:18 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM