Thread: Please help me in finding the problem.. I can't find it.. I tried alot... :(

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Please help me in finding the problem.. I can't find it.. I tried alot... :(

    Plz plz plz plz plz help me!!!!! i tried alot to figure out the pblm but i failed.. therefore i m putting my querie in front of xperts.. i m a beginner.. please help.!!!


    Program before winchck function call

    every thing working fine...... while function in mai runs 9 times taking all inputs.. Please run it and chck


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    
    void charsel(char *p1choic, char *p2choic);
    int posin(char arr[9]);
    void tableout(char arr[9]);
    int winchck(char arr[9], char p1choic, char p2choic);
    
    int main(){
     
      char arr[9]={32,32,32,32,32,32,32,32,32};
      int i,j=0;
      int p1=1,p2=1;
      int a=0;
    
      char p1choic=0, p2choic=0;
     
      charsel(&p1choic, &p2choic);      //charachter selection
      
      srand( time(NULL));
      j = rand() % 2;
      system("pause");
       
       
       
       for(i=1; i<10; i++){
        if(j == 0){
         system("cls"); 
         tableout(arr);   
         printf("\nPlayer 1 turn\n");
         p1=posin(arr);
         
         while(arr[p1-1] != 32){
             system("cls");
             if(p1<1 || p1 >9){
             printf("Selection is not between 1 - 9\n\a"); 
             tableout(arr);
             printf("\nPlayer 1 turn\n");
             p1=posin(arr);  
             }
             else{
             printf("Place has already been selected before\n\a");
             tableout(arr);
             printf("\nPlayer 1 turn\n");
             p1=posin(arr);
          }
          }
          arr[p1-1]=p1choic;      
          j++;
       }
       
      
                  
        else if(j == 1){
         system("cls");
         tableout(arr);
         printf("\nPlayer 2 turn\n");
         p2=posin(arr);
         
         while(arr[p2-1] != 32){
             system("cls");
             if(p2<1 || p2 >9){
             printf("Selection is not between 1 - 9\n\a"); 
             tableout(arr);
             printf("\nPlayer 2 turn\n");
             p2=posin(arr);  
             }
             else{
             printf("Place has already been selected before\n\a");
             tableout(arr);
             printf("\nPlayer 2 turn\n");
             p2=posin(arr);
          }
          }
          arr[p2-1]=p2choic;
          
          j--; 
          
        
       }
      
       }  
       system("cls");
       tableout(arr);       
      
      
       
      getch ();
      return 0;
    }
    
    
    
    // Functions
    
    // --Position Input
    int posin(char arr[9]){
       int n=0;
          printf("you can choose position:\n\n");
        int i=0;  
          for(i=0;i<9;i++){
           if(i == 3 || i == 6 || i == 9){
             printf("%c\n - - -\n",179);
             }
           
           if(arr[i] == 32){    
            printf("%c%d",179,i+1);
           } 
           else if(arr[i]!=32)
           printf("%c%c",179,178);
          }
          printf("%c\n",179);
          
          printf("\nEnter choice: ");
          scanf("%d",&n);
          return n;
    }
    
    // !!--Position Input
    
    // --Table Output f
    
    void tableout(char arr[9]){
       int i=0;
       printf("\n*RESULT*\n\n");  
          for(i=0;i<9;i++){
           if(i == 3 || i == 6 || i == 9){
             printf("%c\n - - -\n",179);
             }
           
           if(arr[i] == 32){    
            printf("%c%c",179,arr[i+1]);
           } 
           else if(arr[i]!=32)
           printf("%c%c",179,arr[i]);
          }
          printf("%c\n",179);
    }
    
    // !!--Table Output
    
    // -- Winchck
    
    int winchck(char arr[9], char p1choic, char p2choic){
       
       if(arr[0]==p1choic && arr[1]==p1choic && arr[2]==p1choic)
       return 1;
       
       else if(arr[3]==p1choic && arr[4]==p1choic && arr[5]==p1choic)
       return 1;
       
       else if(arr[6]==p1choic && arr[7]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[0]==p1choic && arr[3]==p1choic && arr[6]==p1choic)
       return 1;
       
       else if(arr[1]==p1choic && arr[4]==p1choic && arr[7]==p1choic)
       return 1;
       
       else if(arr[2]==p1choic && arr[5]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[0]==p1choic && arr[4]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[2]==p1choic && arr[4]==p1choic && arr[6]==p1choic)
       return 1;
       
       
       
       else if(arr[0]==p2choic && arr[1]==p2choic && arr[2]==p2choic)
       return 2;
       
       else if(arr[3]==p2choic && arr[4]==p2choic && arr[5]==p2choic)
       return 2;
       
       else if(arr[6]==p2choic && arr[7]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[0]==p2choic && arr[3]==p2choic && arr[6]==p2choic)
       return 2;
       
       else if(arr[1]==p2choic && arr[4]==p2choic && arr[7]==p2choic)
       return 2;
       
       else if(arr[2]==p2choic && arr[5]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[0]==p2choic && arr[4]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[2]==p2choic && arr[4]==p2choic && arr[6]==p2choic)
       return 2;
       
        
       
       return 0;
           
    }
       
       
    
    // !!-- Win Check
    
    
    // --Charachter Selection--
    void charsel(char *p1choic, char *p2choic){
      
     int i=0;
    
     while(1){
        
      printf("Enter your Choice\nx or o : ");
      fflush(stdin);
      scanf("%c",p1choic);
     
      switch(*p1choic){
        
         case 'x':
           *p2choic='o';
           break;
          
         case 'o':
           *p2choic='x';
           break;
          
         default:
           printf("\nPlease choose x or o\n");
      }
           
         if (*p1choic == 'x' || *p1choic == 'o')
         break;
     }  
     printf("\nPlayer 1 choice: %c\nPlayer 2 choice: %c\n",*p1choic,*p2choic);
    
      
    }
    // !!--Charachter Selection--



    Code after winchck function call.. while loop only running once and then breaking in main..



    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    
    void charsel(char *p1choic, char *p2choic);
    int posin(char arr[9]);
    void tableout(char arr[9]);
    int winchck(char arr[9], char p1choic, char p2choic);
    
    int main(){
     
      char arr[9]={32,32,32,32,32,32,32,32,32};
      int i,j=0;
      int p1=1,p2=1;
      int a=0;
    
      char p1choic=0, p2choic=0;
     
      charsel(&p1choic, &p2choic);      //charachter selection
      
      srand( time(NULL));
      j = rand() % 2;
      system("pause");
       
       
       
       for(i=1; i<10; i++){
        if(j == 0){
         system("cls"); 
         tableout(arr);   
         printf("\nPlayer 1 turn\n");
         p1=posin(arr);
         
         while(arr[p1-1] != 32){
             system("cls");
             if(p1<1 || p1 >9){
             printf("Selection is not between 1 - 9\n\a"); 
             tableout(arr);
             printf("\nPlayer 1 turn\n");
             p1=posin(arr);  
             }
             else{
             printf("Place has already been selected before\n\a");
             tableout(arr);
             printf("\nPlayer 1 turn\n");
             p1=posin(arr);
          }
          }
          arr[p1-1]=p1choic;      
          j++;
       }
       
       
       if(winchck(arr,p1choic,p2choic) ==! 1){
           printf("Player 1 Wins");
           break;  
          }
                  
        else if(j == 1){
         system("cls");
         tableout(arr);
         printf("\nPlayer 2 turn\n");
         p2=posin(arr);
         
         while(arr[p2-1] != 32){
             system("cls");
             if(p2<1 || p2 >9){
             printf("Selection is not between 1 - 9\n\a"); 
             tableout(arr);
             printf("\nPlayer 2 turn\n");
             p2=posin(arr);  
             }
             else{
             printf("Place has already been selected before\n\a");
             tableout(arr);
             printf("\nPlayer 2 turn\n");
             p2=posin(arr);
          }
          }
          arr[p2-1]=p2choic;
          
          j--; 
          
        
       }
       if(winchck(arr,p1choic,p2choic) ==! 2){
           printf("Player 2 Wins");
           break;  
          }
       
       }  
       system("cls");
       tableout(arr);       
      
      
       
      getch ();
      return 0;
    }
    
    
    
    // Functions
    
    // --Position Input
    int posin(char arr[9]){
       int n=0;
          printf("you can choose position:\n\n");
        int i=0;  
          for(i=0;i<9;i++){
           if(i == 3 || i == 6 || i == 9){
             printf("%c\n - - -\n",179);
             }
           
           if(arr[i] == 32){    
            printf("%c%d",179,i+1);
           } 
           else if(arr[i]!=32)
           printf("%c%c",179,178);
          }
          printf("%c\n",179);
          
          printf("\nEnter choice: ");
          scanf("%d",&n);
          return n;
    }
    
    // !!--Position Input
    
    // --Table Output f
    
    void tableout(char arr[9]){
       int i=0;
       printf("\n*RESULT*\n\n");  
          for(i=0;i<9;i++){
           if(i == 3 || i == 6 || i == 9){
             printf("%c\n - - -\n",179);
             }
           
           if(arr[i] == 32){    
            printf("%c%c",179,arr[i+1]);
           } 
           else if(arr[i]!=32)
           printf("%c%c",179,arr[i]);
          }
          printf("%c\n",179);
    }
    
    // !!--Table Output
    
    // -- Winchck
    
    int winchck(char arr[9], char p1choic, char p2choic){
       
       if(arr[0]==p1choic && arr[1]==p1choic && arr[2]==p1choic)
       return 1;
       
       else if(arr[3]==p1choic && arr[4]==p1choic && arr[5]==p1choic)
       return 1;
       
       else if(arr[6]==p1choic && arr[7]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[0]==p1choic && arr[3]==p1choic && arr[6]==p1choic)
       return 1;
       
       else if(arr[1]==p1choic && arr[4]==p1choic && arr[7]==p1choic)
       return 1;
       
       else if(arr[2]==p1choic && arr[5]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[0]==p1choic && arr[4]==p1choic && arr[8]==p1choic)
       return 1;
       
       else if(arr[2]==p1choic && arr[4]==p1choic && arr[6]==p1choic)
       return 1;
       
       
       
       else if(arr[0]==p2choic && arr[1]==p2choic && arr[2]==p2choic)
       return 2;
       
       else if(arr[3]==p2choic && arr[4]==p2choic && arr[5]==p2choic)
       return 2;
       
       else if(arr[6]==p2choic && arr[7]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[0]==p2choic && arr[3]==p2choic && arr[6]==p2choic)
       return 2;
       
       else if(arr[1]==p2choic && arr[4]==p2choic && arr[7]==p2choic)
       return 2;
       
       else if(arr[2]==p2choic && arr[5]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[0]==p2choic && arr[4]==p2choic && arr[8]==p2choic)
       return 2;
       
       else if(arr[2]==p2choic && arr[4]==p2choic && arr[6]==p2choic)
       return 2;
       
        
       
       return 0;
           
    }
       
       
    
    // !!-- Win Check
    
    
    // --Charachter Selection--
    void charsel(char *p1choic, char *p2choic){
      
     int i=0;
    
     while(1){
        
      printf("Enter your Choice\nx or o : ");
      fflush(stdin);
      scanf("%c",p1choic);
     
      switch(*p1choic){
        
         case 'x':
           *p2choic='o';
           break;
          
         case 'o':
           *p2choic='x';
           break;
          
         default:
           printf("\nPlease choose x or o\n");
      }
           
         if (*p1choic == 'x' || *p1choic == 'o')
         break;
     }  
     printf("\nPlayer 1 choice: %c\nPlayer 2 choice: %c\n",*p1choic,*p2choic);
    
      
    }
    // !!--Charachter Selection--

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Seriously? You want me to compile and run some poorly structured code and see what happens? I'd rather play Russian Roulette as it is a safer game.

    Try creating a small and clear but complete sample of code that exhibits your problem. In the process of doing that, you might have an "aha!' moment and realise what the problem is. If not, you will have a sample of code that is more easily understood, so other folks have a chance of giving you useful advice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of a big while loop, with just an if statement in it for the user's input check, I'd use a big while loop, and then use ANOTHER while loop, inside that, just for the user's input.

    Maybe a do while:

    Code:
    do {
       scanf(" %d", &myMove);
      
    }while(myMove <1 || myMove >9);
    THEN check and see if the square is empty or not, etc.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    if(winchck(arr,p1choic,p2choic) ==! 1){
    What do you think "==!" means?
    It DOES NOT mean not equal 1.

    Tim S.
    Last edited by stahta01; 12-22-2011 at 09:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-23-2011, 01:40 PM
  2. Why isn't map::find() finding anything?
    By cpjust in forum C++ Programming
    Replies: 14
    Last Post: 07-18-2008, 08:38 PM
  3. alot?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-08-2008, 07:36 PM
  4. storing alot of variables...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 05-30-2008, 12:31 PM
  5. Replies: 6
    Last Post: 08-18-2002, 08:01 AM