Thread: stone,scissors,paper!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Question stone,scissors,paper!

    Hi,
    I'm trying to write a code for the game Stone-Scissors-Paper.
    I faced some problems when it comes to announcing the winner. I get a syntax error in line 62.
    I'll appreciate it if someone help me out =)
    Code:
    /*
     *  File    : pss.c 
     *  Program : Paper, stone, scissors  game
     *  Author  : Sepideh
     */
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    #include<string.h>
    
    #define MAX_TURNS 10
    
    #define MAX_BUFFER 6	       
    
    #define DRAW 0
    #define COMPUTER_WIN 1
    #define HUMAN_WIN 2
    
    #define STONE 0
    #define SCISSOR 1
    #define PAPER 2
    
    #define STONE_STR "stone"      
    #define SCISSOR_STR "scissors"
    #define PAPER_STR "paper"
    
    
    
    
    
    /* ------------------- Utilities ---------------*/
    void clear_stdin();
    int human_choice();
    int computer_choice();
    void print_computer_choice();
    int winner(int human,int computer);
    
    /***************************************************
     *
     *    MAIN
     *
     ***************************************************/
    
    int main()
    {
      int human,
        computer,
        result;
        /*statistic;
        int i;*/
               
      srand( time(0) );
      /* statistic = 0;*/
    
      printf("Welcome to Paper-Scissors-Stone game by Sepideh!\n"); 
    
      human=human_choice();
      computer=computer_choice();
      print_computer_choice();
      result=winner(int human,int computer);
        
      /*  Get result depending on choices */
      /*  Print result */
    
      /*  Update statistics */
    
      
     
      /* Print total depending on statistics */
     
      return 0;
    }
    
    /******************************************************
     *
     *  DEFINITIONS
     * 
     ******************************************************/
    
    void clear_stdin()
    {
      while( getchar() != '\n' ){;}
    }
    
    int human_choice()
    {
      char humanch[10];
      int resultat;
      printf("Your turn!Type your choice.\n");
      scanf("%s",humanch);
      while(strcmp(humanch,STONE_STR)!=0&&strcmp(humanch,SCISSOR_STR)!=0&&strcmp(humanch,PAPER_STR)!=0){
        printf("Wrong entry! Try again.\n");
        scanf("%s",humanch);
      }
      if(humanch=="stone")
        resultat=0;
      if(humanch=="scissors")
        resultat=1;
      if(humanch=="paper")
        resultat=2;
      return resultat;
      void clear_stdin();
    }
    
    int computer_choice()
    {
      int slumptal;
      slumptal = rand()%3;
      return slumptal;
      printf("%d",slumptal);
    }
    
    void print_computer_choice(){
      int slumptal;
      if(slumptal==0)
        printf("stone\n");
      if(slumptal==1)
        printf("scissors\n");
      if(slumptal==2)
        printf("paper\n");
    }
    
    int winner(int human,int computer)
    {
      int winresult;
      if(human_choice()==computer_choice())
        winresult=0;
      if((human_choice()==0&&computer_choice()==1)||(human_choice()==1&&computer_choice()==2)||(human_choice()==2&&computer_choice()==2))
        winresult=2;
      return winresult;
      /* printf("%d",winresult);*/
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    let me guess, this is line 62:
    Code:
      result=winner(int human,int computer);
    Remove the int's.

    But you are also making mistakes of using local variables that should probably be parameters to the function, e.g your slumptal in "print_computer_choice". It is uninitialized. Also
    Code:
    int winner(int human,int computer)
    {
      int winresult;
      if(human_choice()==computer_choice())
        winresult=0;
      if((human_choice()==0&&computer_choice()==1)||(human_choice()==1&&computer_choice()==2)||(human_choice()==2&&computer_choice()==2))
        winresult=2;
      return winresult;
      /* printf("%d",winresult);*/
    }
    The winresult MAY be undefined here - if none of the two if-statements are "true", then winresult isn't being set.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    let me guess, this is line 62:
    Code:
      result=winner(int human,int computer);
    Remove the int's.
    I removed them and get no errors no more =) but when I execute the program it's no good!
    and it's certainly because of my other mistakes u said.
    But you are also making mistakes of using local variables that should probably be parameters to the function, e.g your slumptal in "print_computer_choice". It is uninitialized.
    ok, I'm trying to use the result from the computer_choice function there. how should I do it then? shall I call the "computer_choice" function in "print_computer_choice" function?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sifeet View Post
    I removed them and get no errors no more =) but when I execute the program it's no good!
    and it's certainly because of my other mistakes u said.
    ok, I'm trying to use the result from the computer_choice function there. how should I do it then? shall I call the "computer_choice" function in "print_computer_choice" function?
    No, you need to pass in the result from computer_choice instead of thelocal variable called "slumptal" [which happens to be the same name as in computer_choice, but they are NOT the same variable, since they are both local variables in different functions, so two different variables].

    Code:
      if(humanch=="stone")
        resultat=0;
      if(humanch=="scissors")
        resultat=1;
      if(humanch=="paper")
        resultat=2;
      return resultat;
      void clear_stdin();
    This is also broken - you need to use strcmp() [and why not use the #defines that you use a couple of lines above?]. You then tell return the result, and then tell the compiler you have a function that returns nothing called clear_stdin(). I'm pretty sure the latter should be a call to clear_stdin(), which should then be done BEFORE the return.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Unhappy

    Hi again,
    I'm still struggling with this code! I used the tips that Mats gave me. Now when I run the program, I write in my choice, computer randoms its choice and then instead of printing the result it goes back to printf("Your turn!Type your choice.\n"); !!
    And It keeps going like that 3 or 4 times and then it prints the result. (which actually is correct!!)
    It's very strange coz printf("Your turn!Type your choice.\n"); is not in a while loop or so. I don't understand why the program goes back there!
    This version of program which I'm writing now should runs only one time and print the result. (It's in future versions where I have to edit it so that it runs 10 times)
    Here is the code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    #include<string.h>
    
    #define MAX_TURNS 10
    
    #define MAX_BUFFER 6	       
    
    #define DRAW 0
    #define COMPUTER_WIN 1
    #define HUMAN_WIN 2
    
    #define STONE 0
    #define SCISSOR 1
    #define PAPER 2
    
    #define STONE_STR "stone"      
    #define SCISSOR_STR "scissors"
    #define PAPER_STR "paper"
    
    
    
    
    
    /* ------------------- Utilities ---------------*/
    void clear_stdin();
    int human_choice();
    int computer_choice();
    void print_computer_choice();
    int winner(int human,int computer);
    void print_winner(int result);
    
    
    /***************************************************
     *
     *    MAIN
     *
     ***************************************************/
    
    int main()
    {
      int human,
        computer,
    
        result;
      /*statistic;
        int i;*/
    
               
      srand( time(0) );
      /* statistic = 0;*/
    
      printf("Welcome to Paper-Scissors-Stone game by Sepideh!\n"); 
    
      human=human_choice();
      computer=computer_choice();
      print_computer_choice();
      result=winner(human,computer);
      print_winner(result);
      
        
      /*  Get result depending on choices */
      /*  Print result */
    
      /*  Update statistics */
    
      
     
      /* Print total depending on statistics */
     
      return 0;
    }
    
    /******************************************************
     *
     *  DEFINITIONS
     * 
     ******************************************************/
    
    void clear_stdin()
    {
      while( getchar() != '\n' ){;}
    }
    
    int human_choice()
    {
      char humanch[10];
      int resultat;
      printf("Your turn!Type your choice.\n");
      scanf("%s",humanch);
      while(strcmp(humanch,STONE_STR)!=0&&strcmp(humanch,SCISSOR_STR)!=0&&strcmp(humanch,PAPER_STR)!=0){
        printf("Invalid entry! Try again.\n");
    
        scanf("%s",humanch);
      }
    
      if(strcmp(humanch,STONE_STR)==0)
        resultat=STONE;
       
      else if(strcmp(humanch,SCISSOR_STR)==0)
        resultat=SCISSOR;
        
      else if(strcmp(humanch,PAPER_STR)==0)
        resultat=PAPER;
    
      clear_stdin();
    
      return resultat;
     
    }
    
    int computer_choice()
    {
      int slumptal;
      slumptal = rand()%3;
      return slumptal;
    
    }
    
    void print_computer_choice(){
      int slumptal;
      if(slumptal==0)
        printf("stone\n");
      if(slumptal==1)
        printf("scissors\n");
      if(slumptal==2)
        printf("paper\n");
    }
    
    int winner(int human,int computer)
    {
      human=human_choice();
      computer=computer_choice();
      int result;  
      if((human_choice()==0||computer_choice()==1)&&(human_choice()==1||computer_choice()==2)&&(human_choice()==2||computer_choice()==0)){
        result=2;
        printf("a");
      }
      else if((computer_choice()==0||human_choice()==1)&&(computer_choice()==1||human_choice()==2)&&(computer_choice()==2||human_choice()==0)){
        result=1;
        printf("b");
      }
      else {
        result=0;
        printf("c");
      }
      return result;
      
    }
    
    void print_winner(int result)
    {
      if(result==0)
        printf("No one wins!\n");
      if(result==1)
        printf("Computer wins!\n");
      if(result==2)
        printf("You win!\n");
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not going to give you the answer directly. Instead, I give you a hint: Which is the function that prints the message, and where is it caled?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Hmmmm, I really have no clue! The human_choice function prints the message and this function is the first function in the Main function. still not in a loop ...
    although in the function winner I use human as one of the arguments. Does this have to do with the problem?! I changed the arguments and get lot of errors now :-S

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what does the line just after the brace in "winner" do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Smile

    Quote Originally Posted by matsp View Post
    So, what does the line just after the brace in "winner" do?

    --
    Mats
    yepp! I see that now! :"> tnx for the help!

    Now I should find out how to use the result of a function in another one. You said this about the variabel slumptal which I used in 2 different functions(hoping that it would have the same value in both functions!) I didn't(couldn't) correct that. but it still works!

    tnx again for taking your time.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One function should return the slumptal, and the other should take it as a parameter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed