Thread: i need this for my tictactoe game. pls help.

  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. when i execute the program, 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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my upcoming UNO card game :)
    By Hussain Hani in forum Game Programming
    Replies: 5
    Last Post: 01-24-2008, 01:19 AM
  2. i need this for my tictactoe game. pls help.
    By riel in forum C Programming
    Replies: 9
    Last Post: 01-21-2008, 05:10 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