Help needed, going blind.

This is a discussion on Help needed, going blind. within the C Programming forums, part of the General Programming Boards category; Whats wrong here, getting wrong results from the game. Looks like things stay in stdin and produces wrong results. And ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    7

    Question Help needed, going blind.

    Whats wrong here, getting wrong results from the game. Looks like things stay in stdin and produces wrong results. And also, you have too answer the question twice. As i see the code is flawless (apperently not). But anyway, im desperate for helt here so.. This is my second week with C so..

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    #include<string.h>
    
    #define MAX_TURNS 10
    
    #define MAX_BUFFER 6            /* Tricky 5 won't do */
    
    #define DRAW 0
    #define COMPUTER_WIN 1
    #define HUMAN_WIN 2
    
    #define STONE 0
    #define SCISSOR 1
    #define PAPER 2
    
    #define STONE_STR "s"   /* Used in logic not IO */
    #define SCISSOR_STR "k"
    #define PAPER_STR "p"
    
    
    
    
    
    /* ------------------- Utilities ---------------*/
    void clear_stdin();
    int human_choice(int human);
    int computer_choice(int computer);
    void print_computer_choice();
    int winner(int human,int computer);
    void print_winner(int result);
    
    /***************************************************
     *
     *    MAIN
     *
     ***************************************************/
    
    int main()
    {
      int human, computer, result, statistic;
               
      srand( time(0) );
      statistic = 0;
    
      printf("Welcome to paper, scissor, stone game ...\n"); 
      
      human=human_choice(human);
      computer=computer_choice(computer);
      print_computer_choice();
      result=winner(human,computer);
      print_winner(result);
      
       
      return 0;
    }
    
    /******************************************************
     *
     *  DEFINITIONS
     * 
     ******************************************************/
    
    void clear_stdin()
    {
      while( getchar() != '\n' ){;}
    }
    
    int human_choice(int human) {  
      char humanchar;
      printf("Stone(s), Scissor(k) or bag(p)\n");
      printf("  : ");
    
      scanf("%c", &humanchar);
     
      switch(humanchar){
      case 's': human = STONE; break;
      case 'k': human = SCISSOR; break;
      case 'p':  human = PAPER; break;
      }
      clear_stdin();
      return human; 
    }
    
    int computer_choice(computer){
      computer = rand() % 3;
      return computer;
    }
    
    void print_computer_choice(int computer) {
      char c_took;
      switch(computer){
      case 0: c_took = 's'; break;
      case 1: c_took = 'k'; break;
      case 2: c_took = 'p'; break;
      }
      printf("Computer choosed '%c'\n", c_took);
    }
    
    int winner(int human,int computer)
    
    {
      human=human_choice(human);
      computer=computer_choice(computer);
      int result=0;  
    
      if (computer == human) {
        result = 4;
      }
      else if (human == 0 && computer == 1) {
        result = 5;
      }
      else if (human == 1 && computer == 2) { 
        result = 5;
      }
      else if (human == 2 && computer == 0) { 
        result = 5;
      }
      else {
        result = 6;
      }
      return result;
    }
    
    void print_winner(int result)
    {
      if(result==4)
        printf("None win!\n");
      if(result==6)
        printf("Computer win!\n");
      if(result==5)
        printf("You win!\n");
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you copy this from somewhere on this forum?

    It looks very familiar.

    You are calling human_choice twice, which is probably why you are "having to answer the question twice".

    If you give further details of what the problems are that you are trying to solve, perhaps we can help better.

    --
    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
    Dec 2007
    Posts
    7
    Just removed

    human=human_choice(human);
    computer=computer_choice(computer);

    from function "winner". Oh, i didnt know. I see that its nearly the same code. We have to code this game as an exercise.
    I have one further question, is it ugly to use case-clauses in C-code?. I havent seen that very often. But again, im really new to this.

    Thanks anyway, as i say, sometimes you get blind.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    switch/case statements are fine. It's an excellent solution for multiple choice situations, as long as there is a constant set of values to use in the case list.

    --
    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

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Releasing a program - what is needed? A few Q's
    By ulillillia in forum Tech Board
    Replies: 9
    Last Post: 04-28-2007, 12:18 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. error with function, help needed quick
    By chris285 in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 08:31 AM

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