help needed with game code

This is a discussion on help needed with game code within the C Programming forums, part of the General Programming Boards category; hello, i am writing a code to simulate the game rock paper scissors, i have it working, but i have ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    help needed with game code

    hello, i am writing a code to simulate the game rock paper scissors, i have it working, but i have a small problem. the game asks for R P S or Q(quit), then it tells you the outcome of the game, then it prompts the used for another
    R P S or Q. but instead of asking for it only once(for the second/third/fourth/etc prompt) , it prints the line twice. i was using fflush(stdin) but i don't want to use that. this is only a minor issue but i was wondering if someone could give me some suggestions to what i am doing wrong. thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
        int random = rand() % 3;
        int userWinCount = 0 ;
        int computerWinCount = 0;
        int tieCount = 0;
        char choice;
        
        printf("**Rock Paper Scissors Game**\n");
        
        srand(time(NULL));
        
        do
        {
            printf("\nPlease enter R, S, P or Q(Quit)\n");
            scanf("%c" , &choice); 
                  
            switch (random)
        {
                case 0:
                      switch (choice) 
        {
                               case 'R': case 'r':
                                    printf("Same throw Rock. Game is tie\n");
                                    tieCount++;
                                    break;
                               case 'S': case 's':
                                    printf("Rock breaks scissors! Computer wins\n!");                                       
                                    computerWinCount++;                                       
                                    break;                                    
                               case 'P': case 'p':
                                    printf("Paper covers rock! User wins\n!");
                                    userWinCount++;
                                    break;
        }    
                      break;
                case 1:
                      switch ( choice )
        {
                               case 'R': case 'r':
                                    userWinCount++;
                                    printf("Rock breaks scissors! User wins!\n");
                                    break;
                               case 'S': case 's':
                                    printf("Same throw Scissors. Game is tie\n");
                                    tieCount++;
                                    break;
                               case 'P': case 'p':
                                    computerWinCount++;
                                    printf("Scissors cut paper! Computer wins!\n");                      
                                    break;
        }
                      break;
                case 2:
                      switch ( choice )
        {
                               case 'R': case 'r':
                                    printf("Paper covers rock! Computer wins!\n");
                                    computerWinCount++;
                                    break;
                               case 'S': case 's':
                                    printf("Scissors cut paper! User wins!\n");
                                    userWinCount++;
                                    break;
                               case 'P': case 'p':
                                    printf("Same throw Paper. Game is tie\n");          
                                    tieCount++;
                                    break;
        }
                        
                      break;
        }
            
        } 
        
        while ( choice != 'Q' && choice != 'q' );
        
        printf("Total User Wins = %d\n" , userWinCount);
        printf("Total Computer Wins = %d\n" , computerWinCount );
        printf("Total Tie Count = %d\n" , tieCount ) ;
        
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    i had that originally but that actually causes another problem. thank you for trying though.
    here is what comes up in my program

    please enter r p s or q: (i enter r for example)

    (then for ex the program prints) " paper beats rock, computer wins!!

    (then the program prints)
    please enter r p s or q: (yes this is good)

    please enter r p s or q: (i don't want this 2nd prompt to come up)

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Are you sure your randomness for the computer choices are really random?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    It looks like you call rand() before seeding with srand().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    It looks like you call rand() before seeding with srand().
    You've got to have an edge, otherwise you'll never beat the computer

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Oh after another look, you may want to add getchar() just before your last squirly brace from your computer's choice switch statement.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    thank you! i guess it isn't random, when i put the fflush(stdin) it becomes random and fixes my other problem. but i have read that it is not a good idea to use this. is there anything else that i oculd use that would be equivalent to fflush?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    By the way, you should indent your code better to improve readability, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int random = rand() % 3;
        int userWinCount = 0 ;
        int computerWinCount = 0;
        int tieCount = 0;
        char choice;
    
        printf("**Rock Paper Scissors Game**\n");
    
        srand(time(NULL));
    
        do
        {
            printf("\nPlease enter R, S, P or Q(Quit)\n");
            scanf("%c" , &choice);
    
            switch (random)
            {
            case 0:
                switch (choice)
                {
                case 'R': case 'r':
                    printf("Same throw Rock. Game is tie\n");
                    tieCount++;
                    break;
                case 'S': case 's':
                    printf("Rock breaks scissors! Computer wins\n!");
                    computerWinCount++;
                    break;
                case 'P': case 'p':
                    printf("Paper covers rock! User wins\n!");
                    userWinCount++;
                    break;
                }
                break;
            case 1:
                switch ( choice )
                {
                case 'R': case 'r':
                    userWinCount++;
                    printf("Rock breaks scissors! User wins!\n");
                    break;
                case 'S': case 's':
                    printf("Same throw Scissors. Game is tie\n");
                    tieCount++;
                    break;
                case 'P': case 'p':
                    computerWinCount++;
                    printf("Scissors cut paper! Computer wins!\n");
                    break;
                }
                break;
            case 2:
                switch ( choice )
                {
                case 'R': case 'r':
                    printf("Paper covers rock! Computer wins!\n");
                    computerWinCount++;
                    break;
                case 'S': case 's':
                    printf("Scissors cut paper! User wins!\n");
                    userWinCount++;
                    break;
                case 'P': case 'p':
                    printf("Same throw Paper. Game is tie\n");
                    tieCount++;
                    break;
                }
                break;
            }
        }
        while ( choice != 'Q' && choice != 'q' );
    
        printf("Total User Wins = %d\n" , userWinCount);
        printf("Total Computer Wins = %d\n" , computerWinCount );
        printf("Total Tie Count = %d\n" , tieCount ) ;
    
        system("pause");
        return 0;
    }
    You might also want to break up your main function into smaller functions that do one thing and do it well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Tools I use, since I am lazy sometimes for indention and quick standards check is

    indent
    splint

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    ok so i fixed the problem with it not being random, now i still have the other problem which is that it prompts the user twice for r p s or quit after inputing r p s the first time(see above post at 11:38 for ex.) i tried using choice = getchar() but it still does the same thing. can anyone help me see what i am doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int random; 
        int userWinCount = 0 ;
        int computerWinCount = 0;
        int tieCount = 0;
        char choice;
    
        printf("**Rock Paper Scissors Game**\n");
         
        srand(time(NULL));
    
        do
        {   
          
            printf("\nPlease enter R, S, P or Q(Quit)\n");
            scanf("%c", &choice);
            
            random = rand() % 3;
    
            switch (random)
            {
            case 0:
                switch (choice)
                {
                case 'R': case 'r':
                    printf("Same throw Rock. Game is tie!\n");
                    tieCount++;
                    break;
                case 'S': case 's':
                    printf("Rock breaks scissors! Computer wins!\n");
                    computerWinCount++;
                    break;
                case 'P': case 'p':
                    printf("Paper covers rock! User wins!\n");
                    userWinCount++;
                    break;
                }
                break;
            case 1:
                switch (choice)
                {
                case 'R': case 'r':
                    userWinCount++;
                    printf("Rock breaks scissors! User wins!\n");
                    break;
                case 'S': case 's':
                    printf("Same throw Scissors. Game is tie!\n");
                    tieCount++;
                    break;
                case 'P': case 'p':
                    computerWinCount++;
                    printf("Scissors cut paper! Computer wins!\n");
                    break;
                }
                break;
            case 2:
                switch (choice)
                {
                case 'R': case 'r':
                    printf("Paper covers rock! Computer wins!\n");
                    computerWinCount++;
                    break;
                case 'S': case 's':
                    printf("Scissors cut paper! User wins!\n");
                    userWinCount++;
                    break;
                case 'P': case 'p':
                    printf("Same throw Paper. Game is tie!\n");
                    tieCount++;
                    break;
                }
                break;
            }
        }
        while ( choice != 'Q' && choice != 'q' );
    
        printf("Total User Wins = %d\n" , userWinCount);
        printf("Total Computer Wins = %d\n" , computerWinCount );
        printf("Total Tie Count = %d\n" , tieCount ) ;
    
        system("pause");
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    They did tell you right - it prompts you twice because there is a newline left on the keyboard buffer after the first answer of the user.

    Say you answer right away with 'Q'. So the keyboard buffer looks like: Q\n, before scanf() pulls the first char off the buffer - leaving just the '\n' (newline) char.

    Since you entered a q right away, the game exits without repeating the prompt.

    But if you play one round or more, then you get the repeat prompts, because you still have choice != 'Q' or 'q', and the scanf() won't stop, because it plucks off the newline char and says "Bye! Thanks a lot for the input", and loops on.

    Only for the *second* loop, does scanf() wait, because now the newline is gone, and the keyboard buffer (input stream in this case), is truly empty.

    You can solve this problem be adding a getchar() line immediately after the scanf() line of code, or you can add a space inside and just after the double quotes, in scanf().

    So now it's like this:
    scanf("%c", &choice);

    and you need it like this:
    scanf(" %c", &choice);

    and that solves your problem.

    Edit:
    You can't use choice = getchar(), because choice is being tested for at the bottom of the Do While loop, which would cause the test to fail, and the loop to again, loop too many times around.
    Last edited by Adak; 03-13-2009 at 04:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Business code' vs 'Game code'
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-09-2007, 02:33 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Game of Craps Source Code
    By Miles in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 12:46 PM

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