i need this for my tictactoe game. pls help.

This is a discussion on i need this for my tictactoe game. pls help. within the C Programming forums, part of the General Programming Boards category; i have this kind of code: --------------------------------------------------------------------- Code: #include <stdio.h> #include <stdlib.h> void show_box(void); char who_first(void); unsigned int get_random_exponent(void); unsigned ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    i need this for my tictactoe game. pls help.

    i have this kind of code:

    ---------------------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void show_box(void);
    char who_first(void);
    unsigned int get_random_exponent(void);
    unsigned int get_integer(int exp);
    int get_sum(int a, int b);
    int take_box(int input);
    int player_move(void);
    
    
    char box[9] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
    int exponent[9], n_integer[9], m_integer[9], score[2];
    char answer, input, value;
    int i, a, b;
    
    int main(void)
    {
        int playerMove, c;
        int score[2] = {0, 0};  
            
        printf("let's play tic-tac-toe\n\n");
        printf("your score:%d\tcomputer's score:%d\n\n\n", score[0], score[1]);
      
        show_box();
       
        get_random_exponent();
        for (i=0 ; i<9 ; i++)
            n_integer[i] = get_integer(exponent[i]);
    
        for(i=0 ; i<9 ; i++)
                exponent[i] = 0;
                
        get_random_exponent();
        for (i=0 ; i<9 ; i++)
            m_integer[i] = get_integer(exponent[i]);
                     
        for(i=0; i<9 ; i++)
        {       printf("%c = %d + %d\n\n", box[i], n_integer[i], m_integer[i]);
        }
        
    
        for (i=0 ; i<9 ; i++)
            box[i] = get_sum(n_integer[i], m_integer[i]);
            
        printf("sums:\n");
        for(i=0 ; i<9 ; i++)
                printf("%d\n", box[i]);
        
        who_first();
        if (answer == 'y'||answer == 'Y')
           {    player_move();
           }
        else if (answer == 'n'||answer == 'N')
           {    
                box[4] = 'X';
                /* computer will take the middle box */           
           }
        else 
           {   printf("i guess that's a no.");
               /* computer takes middle box */
           }
        
    
        scanf("%d", &answer);
        getchar();
        return 0;
    }
    
    void show_box (void)
    {
        printf("\t###################\n");
        printf("\t#     #     #     #\n");
        printf("\t#  %c  #  %c  #  %c  #\n", box[0], box[1], box[2]);
        printf("\t#     #     #     #\n");
        printf("\t###################\n");
        printf("\t#     #     #     #\tyou = X\n");
        printf("\t#  %c  #  %c  #  %c  #\n", box[3], box[4], box[5]);
        printf("\t#     #     #     #\tcomputer = O\n");
        printf("\t###################\n");
        printf("\t#     #     #     #\n");
        printf("\t#  %c  #  %c  #  %c  #\n", box[6], box[7], box[8]);
        printf("\t#     #     #     #\n");
        printf("\t###################\n\n");
        
    
    }
    
    char who_first(void)
    {   
         printf("play first (y/n)? ");
         scanf("%c", &answer);
         return answer;
    }
    
    unsigned int get_random_exponent(void)
    {
        int a, b, i;   
    
        exponent[0] = rand() % 9;
        for (i=1 ; i<9 ; i++)
        {   a = rand() % 9;
             for (b=0 ; b<i ; b++)
            {   if (a == exponent[b])
                {  do 
                    {    a = rand() % 9;
                         for (b=0 ; b<i ; b++)
                         {   if (a == exponent[b])
                             {  do 
                                {  a = rand() % 9;
                                   for (b=0 ; b<i ; b++)
                                    {   if (a == exponent[b])
                                       {  do 
                                            {  a = rand() % 9;
                                               for (b=0 ; b<i ; b++)
                                               {   if (a == exponent[b])
                                                   {  do 
                                                      {  a = rand() % 9;
                                                         for (b=0 ; b<i ; b++)
                                                         {   if (a == exponent[b])
                                                             {  do 
                                                                {  a = rand() % 9;
                                                                   for (b=0 ; b<i ; b++)
                                                                   {   if (a == exponent[b])
                                                                       {  do 
                                                                          {  a = rand() % 9;
                                                                             for (b=0 ; b<i ; b++)
                                                                             {   if (a == exponent[b])
                                                                                 {  do 
                                                                                    {  a = rand() % 9;
                                                                                    } while (a == exponent[b]);
                                                                                 }
                                                                             }
                                                                          } while (a == exponent[b]);
                                                                       }
                                                                   }
                                                                } while (a == exponent[b]);
                                                             }
                                                         }
                                                      } while (a == exponent[b]);
                                                   }
                                               }
                                            } while (a == exponent[b]);
                                       }
                                    }
                                } while (a == exponent[b]);
                             }
                         }
                    } while (a == exponent[b]);
                }
            }
            exponent[i] = a;
        }
        
    }
    
    unsigned int get_integer(int exp)
    {
        int value = 1;
        
        while (exp > 0)
               {     value *= 2;
                     exp--;
               }
        return value;
        
    }
    
    int get_sum(int a, int b)
    {
        value = a + b;
        return value;
    }
    
    int player_move(void)
    {
        printf("input the sum of the integers assigned in the square you want to take: ");
        scanf("%d", &input);
        
        for (i=0 ; i<9 ; i++)
        {   if (input == box[i])
            {  if (box[i] == 'X'||box[i] == 'O')
               {  printf("that box is already taken.\n");
                  player_move();
                box[i] = 'X';
               }
            } 
            else 
            {    printf("wrong sum. try again.\n");    
                 player_move();
            }
        }
        printf("you entered a wrong sum.");
        player_move();
    }
    -----------------------------------------------------

    these are my problems:

    1. why are the list of "sums" not sums? some are even difference! how can i fix this problem?

    2. this code is not yet complete. when the player inputs the sum of a box the letter in the box should be replaced with an 'X'. How can i do that?



    i badly need your help guys. thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you get warnings about return a char as an int? I don't think the numbers should be large enough to overflow a char, but I can't really tell. Plus then it's getting stuffed back into a char. I don't see anything else wrong with your sum function.

    As for the second part, your indentation (that is, you yourself) is lying to you. Just because you've moved the statement box[i] = 'X' backwards doesn't mean it's outside the if-clause.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You don't really need get_sum(), just use a+b instead of get_sum(a, b). Also, since get_integer(exp) returns 1 << exp (at least when exp >= 0), you can just use 1 << exp, assuming you only call it with exp >= 0.

    Edit: Just noticed that get_sum() wouldn't compile since value isn't defined, but unless you're planning to put in extra code later that does more than sum, just use +.
    Last edited by robatino; 01-19-2008 at 11:19 PM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    get_random_exponent lacks a return. Also you can greatly simplify your algorithm. Never Repeat code 6+ times like that. At the very least use a macro. Code duplication leads to errors.

    But in this case you can just do this:
    Code:
    void get_random_exponent()
    {
        int i, swap_with, random;
        //initially just put all the values in order
        for(i = 0; i<sizeof(exponent)/sizeof(int); i++)
            exponent[i]=i;
        //shuffle exponent
        for(i = sizeof(exponent)/sizeof(int)-1; i; --i){
            random=rand()&#37;(i +1);
            if( exponent[i] != exponent[random] ){ //this if is optional
                //swap exponent[i] and exponent[random]
                swap_with = exponent[i];
                exponent[i] = exponent[random];
                exponent[random] = swap_with;
            }
        }
    }
    Last edited by King Mir; 01-20-2008 at 01:20 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int player_move(void)
    {
        printf("input the sum of the integers assigned in the square you want to take: ");
        scanf("&#37;d", &input);
        
        for (i=0 ; i<9 ; i++)
        {   if (input == box[i])
            {  if (box[i] == 'X'||box[i] == 'O')
               {  printf("that box is already taken.\n");
                  player_move();
                box[i] = 'X';
               }
            } 
            else 
            {    printf("wrong sum. try again.\n");    
                 player_move();
            }
        }
        printf("you entered a wrong sum.");
        player_move();
    }
    This looks like infinite recursion. I can't see any execution path that wouldn't call player_move again.

    There are also other problems, such as trying to get numeric input (sum) into a character (why does input have to be a global variable, so that I need to scroll like crazy to find out the type).

    But the biggest problem is that I have no idea what you are expecting to do with these random numbers and sums. This is tic-tac-toe? So why this most convoluted way of getting the user to select a move and why can't I simply type 'a'-'i', as the boxes are initially displayed?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Riel,I didn't read the entire code,but it looks like an art work for sure!

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How can you tell the difference between beginners and trolls?
    I've never seen such a thing as get_random_exponent!

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by oogabooga View Post
    I've never seen such a thing as get_random_exponent!
    He's trying to do this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    my apologies about the code

    actually, this is the 3rd month since i was taught with c prog. if i'm soo dumb over this, i'm sorry. that's why i'm badly asking the help of those experts in this field. as i have said, the code is not yet finished. my apologies again. this is actually tic-tac-toe. it's just a little bit complicated.

    i followed your advice about the sum and the get_random_exponent. thanks. now my only problem is how to display 'X' or 'O' in my board.

    i got this simple tic-tac-toe code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    char matrix[3] [3];
    
    char check(void);
    void init_matrix(void);
    void get_player_move(void);
    void get_computer_move(void);
    void disp_matrix(void);
    int y;
    
    int main(void)
    {
    	char done;
    	int y;
    	
    	printf("This is the game of Tic Tac Toe.\n");
    	printf("You will be playing against me.\n");
    	
    	done = ' ';
    	
    	init_matrix();
    	
    	do	{
    	disp_matrix();
    	get_player_move();
    	done = check();
    	if(done!= ' ') break;
    	get_computer_move();
    	done = check();
    	} while(done == ' ');
    	
    	if (done == 'X') printf("You won!\n");
    	else printf("I won!!!!\n");
    	disp_matrix();
    	
    	scanf("%d", &y);
        getchar();
    	return 0;
    }
    
    void init_matrix(void)
    {
    	int i, j;
    	
    	for(i=0; i<3; i++)
    		for(j=0; j<3; j++) matrix[i][j] = ' ';
    }
    
    void get_player_move (void)
    {
    	int x,y;
    	
    	printf("Enter X,Y coordinates for your move: ");
    	scanf("%d%*c%d", &x, &y);
    	x--; y--;
    	
    	if(matrix[x] [y] != ' '){
    		printf("Invalid move, try again.\n");
    		get_player_move();
    		}
    		else matrix[x] [y] = 'X';
    }
    		
    void get_computer_move(void)
    	{
    			int i, j;
    			for(i=0; i<3; i++){
    					for(j=0; j<3; j++)
    						if(matrix[i] [j]==' ') break;
          					if(matrix[i] [j]==' ') break;
                                   }
    		
        		if(i*j==9)	{
    			printf("draw\n");
    			scanf("%d", &y);
    			exit(0);
    	        }
    		
    		    else
    			    matrix[i] [j] = 'O';
        }
    		
    void disp_matrix(void)
    {
    	int t;
    			
    	for(t=0; t<3; t++){
    		printf(" %c | %c | %c ",matrix[t] [0], matrix[t] [1], matrix [t] [2]);
    						
            if(t!=2) printf("\n---|---|---\n");
    	             		}
    	printf("\n");
    }
    	
    char check(void)
    {
    	int i;
    		
    	for(i=0; i<3; i++)
    		if(matrix[i][0]==matrix[i][1]&&matrix[i][0]==matrix[i][2]) 
                 return matrix[i][0];
    				
    	for(i=0; i<3; i++)
    		if(matrix[0][i]==matrix[1][i]&&
    			matrix[0][i]==matrix[2][i]) return matrix[0][i];
    			
    	if(matrix[0] [0]==matrix[1] [1] && matrix[1][1]== matrix[2][2])
    		return matrix[0][0];
    						
    	if(matrix[0][2]==matrix[1] [1] && matrix[1][1]==matrix[2][0])
    		return matrix[0][2];
    			
    			
    	return ' ';
    }
    if i'm not mistaken, those in red are the ways that could make a 'X' or an 'O' appear in the board. i did that, but when i run the code it doesn't happen.

    we are not yet allowed to use 2d arrays. does it mean that only works for 2d and not in 1d (if that's what it's called)?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your code has this:
    Code:
    matrix[i][j] = ' ';
    Where you initialize the board array. Matrix[][] is clearly a 2D array (has both rows and columns).

    If you're not allowed to use a 2D array, you need to re-do your program with a 1D array.

    I'd recommend each square of the board be either one letter wide by one letter high, or three letters wide by three letters high. That way it's easy to keep your X's and O's, centered.

    Of the two choices above, the single character wide and high, is the easier of the two to make work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  2. my upcoming UNO card game :)
    By Hussain Hani in forum Game Programming
    Replies: 5
    Last Post: 01-24-2008, 12:19 AM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

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