Thread: Rock, Paper, Scissors

  1. #1
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43

    Rock, Paper, Scissors

    Any idea why this isn't working quite right? I'm stumped.

    #include <stdio.h>

    /*
    ROCK 1
    PAPER 2
    SCISSOR 3
    */

    int user_choice = 0;
    int computer_choice = 0;

    void do_get_input()
    {
    printf("Please choose 1, 2, or 3.\nRock being 1, Paper being 2 and Scissors being 3.\n");
    scanf("%d", &user_choice);
    if (user_choice != '1' || user_choice != '2' || user_choice != '3')
    {
    printf("Your choice was invalid. Choose again.\n");
    scanf("%d", &user_choice);
    }
    }

    void do_who_wins()
    {
    rand(computer_choice)%4;
    if (computer_choice == '1' && user_choice == '1')
    {
    printf("You tied.\n");
    }
    else if (computer_choice == '1' && user_choice == '2')
    {
    printf("You win!\n");
    }
    else if (computer_choice == '1' && user_choice == '3')
    {
    printf("You loose!\n");
    }
    else if (computer_choice == '2' && user_choice == '1')
    {
    printf("You loose!\n");
    }
    else if (computer_choice == '2' && user_choice == '2')
    {
    printf("You tie.\n");
    }
    else if (computer_choice == '2' && user_choice == '3')
    {
    printf("You win!\n");
    }
    else if (computer_choice == '3' && user_choice == '1')
    {
    printf("You win!\n");
    }
    else if (computer_choice == '3' && user_choice == '2')
    {
    printf("You loose!\n");
    }
    else if (computer_choice == '3' && user_choice == '3')
    {
    printf("You tie.\n");
    }else{
    printf("Bug: Do_who_wins");
    }
    }

    int main()
    {
    do_get_input();
    do_who_wins();
    return 0;
    }
    To error is human, to really foul things up requires a computer

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When you're reading input into an integer any conditional statement should compare this value with an integer. You have -

    computer_choice == '1'

    '1' is a character. You want -

    computer_choice == 1

    Also to test for validity of input you have -

    if (user_choice != '1' || user_choice != '2' || user_choice != '3')

    The compound statement under this will be executed if the choice is not '1' or the choice is not '2' or the choice is not '3'. Therefore if you entered '1', the the choice wouldn't be '2' or '3' so the above statement would be true. You want -

    if (user_choice != 1 && user_choice !=2 && user_choice != 3)

    I'm not sure want compiler you're using, but most implementations of the random functions require a seed, otherwise you'll get the same results everytime you'll execute the program so you may have to change this.
    zen

  3. #3
    Unregistered
    Guest
    whats a seed?

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    An initial value that the randomise function uses to produce random numbers. The standard function rand() uses an initial default seed of 1. Therefore if you don't provide your own you'll get the same series of random numbers generated everytime you run the program. The most popular method of seeding the random number generator is to use the time, as unless your program is run at exactly the same time, you'll get a different series of random numbers everytime it's run. To do this you'd do -

    srand((unsigned)time(0));
    zen

  5. #5
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    So looking through my program. How would I use this srand function and assign it to the computer_choice variable? I'm thinking

    srand(computer_choice)time(0);

    ?

    --Twigg
    To error is human, to really foul things up requires a computer

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The FAQ has examples of the syntax required to use srand() and rand().
    zen

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:

    void do_who_wins()
    {
    computer_choice = rand()%3 + 1;
    .
    .
    .
    int main()
    {
    srand((unsigned) time(NULL));
    do_get_input();
    do_who_wins();
    return 0;
    }
    Last edited by swoopy; 11-05-2001 at 04:51 PM.

  8. #8
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Ok, I've finished everything and it seems to work. If you see any problems in the code could you tell me?

    [QUOTE]
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    /*
    ROCK 1
    PAPER 2
    SCISSOR 3
    */

    int rand(void);

    //Global Int's
    int cnt = 0;
    int score = 0;
    int user_choice = 0;
    int computer_choice = 0;

    void do_get_input()
    {
    printf("Enter 1 for Rock, 2 for Paper or 3 for Scissors.\n");
    scanf("%d", &user_choice);
    if (user_choice != 1 && user_choice != 2 && user_choice != 3)
    {
    printf("Your choice was invalid. Choose again.\n");
    scanf("%d", &user_choice);
    }
    }

    void do_who_wins()
    {
    srand(time(NULL));
    computer_choice = rand()%3;
    if (computer_choice == 0 && user_choice == 1)
    {
    printf("You: Rock | Computer: Rock\n");
    printf("You tied.\n");
    }
    else if (computer_choice == 0 && user_choice == 2)
    {
    printf("You: Paper | Computer: Rock\n");
    printf("You win!\n");
    score++;
    }
    else if (computer_choice == 0 && user_choice == 3)
    {
    printf("You: Scissors | Computer: Rock\n");
    printf("You loose!\n");
    cnt++;
    }
    else if (computer_choice == 1 && user_choice == 1)
    {
    printf("You: Rock | Computer: Paper\n");
    printf("You loose!\n");
    cnt++;
    }
    else if (computer_choice == 1 && user_choice == 2)
    {
    printf("You: Paper | Computer: Paper\n");
    printf("You tie.\n");
    cnt++;
    }
    else if (computer_choice == 1 && user_choice == 3)
    {
    printf("You: Scissors | Computer: Paper\n");
    printf("You win!\n");
    score++;
    cnt++;
    }
    else if (computer_choice == 2 && user_choice == 1)
    {
    printf("You: Rock | Computer: Scissors\n");
    printf("You win!\n");
    score++;
    cnt++;
    }
    else if (computer_choice == 2 && user_choice == 2)
    {
    printf("You: Paper | Computer: Scissors\n");
    printf("You loose!\n");
    cnt++;
    }
    else if (computer_choice == 3 && user_choice == 3)
    {
    printf("You: Scissors | Computer: Scissors\n");
    printf("You tie.\n");
    cnt++;
    }else{
    printf("Bug: Win Table");
    }
    }

    int main()
    {
    char answer;

    while (cnt < 25)
    {
    do_get_input();
    do_who_wins();
    while ( getchar() != '\n' );
    printf("Play again?(Y/N)\n");
    scanf("%c", &answer);
    answer = toupper(answer);
    if (answer == 'N')
    {
    printf("You scored %d out of %d.\n", score, cnt);
    return 0;
    }else{
    continue;

    }

    }
    printf("You scoured %d out of %d.\n", score, cnt);
    return 0;
    }

    [\QUOTE]

    -Twigg
    Last edited by Twiggy; 11-05-2001 at 06:18 PM.
    To error is human, to really foul things up requires a computer

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Best to only seed the srand() once. I had problems because my PC was so fast the time was the same for all the srand calls.

    Create a static var to tell if you have seeded the srand() and don't do it a second time
    Code:
    static int iCount=0;
    
    if(iCount==0)
    {
        iCount++;
        srand(time(NULL));
    }
    computer_choice = rand()%3; 
    if (computer_choice == 0 && user_choice == 1)
    There are better ways to do this but this will give you an easy solution.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    a better way to do this :

    void do_who_wins(void)
    {
    int Array[]={-2,1,2,-1,0};
    int computer_choice,i;
    char *Message[]={"You win!\n","You loose!\n","You tie.\n","Bug: win table\n"};
    char *ChoiceName[]={"Rock","Paper","Scissors"};

    if(user_choice>3||user_choice<1){
    printf("bad choice\n");
    return;
    }
    computer_choice=rand()%3+1;
    for(i=0;i<sizeof(Array)/sizeof(int);i++)
    if((user_choice-computer_choice)==Array[i]){
    printf("You: %s | Computer: %s\n",ChoiceName[user_choice-1],ChoiceName[computer_choice-1]);
    if(i<2)printf(Message[0]);
    else if(i<4)printf(Message[1]);
    else if(i==4)printf(Message[2]);
    else printf(Message[3]);
    break;
    }
    }
    Last edited by winbill; 11-06-2001 at 07:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  2. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  3. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM
  4. Another rock paper scissor question
    By mattflick in forum C Programming
    Replies: 11
    Last Post: 09-29-2005, 09:41 PM
  5. PRS Help
    By SpudNuts in forum C Programming
    Replies: 10
    Last Post: 08-07-2005, 01:14 PM