Thread: Help with Rock Paper Scissors

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    4

    Post Help with Rock Paper Scissors

    Hey.. Im new into programming and after learning some of the basics about C, i tried making a game of Rock Paper Scissors.. but im having trouble with the output..
    PLEASE HELP!!

  2. #2
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    Code:
    #include <stdio.h>
    
    
    
    
    int main(){
    
    
    int A, B;
    srand(time(NULL));
        printf("Enter====> \n1 for Rock \n2 for Paper \n3 for Scissors \n");
        scanf("%d", &A);
    
    
    
    
        B=1+rand()%3;
        printf("Computer Choose %d\n", 1+rand()%3);
    
    
        if (A == 1 && B == 1 || A == 2 && B == 2 || A == 3 && B == 3)
            printf("DRAW!!");
    
    
        else if (A == 1 && B == 2 || A == 2 && B == 3 || A == 3 && B == 1)
            printf("YOU LOSE");
    
    
        else if (A == 3 && B == 2 || A == 2 && B == 1 || A == 1 && B == 3)
            printf("YOU WIN");
    
    
    
    
    
    
    
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    Code:
    #include <stdio.h>
    
    
    int main(){
    
    
    int A, B;
    srand(time(NULL));
        printf("Enter====> \n1 for Rock \n2 for Paper \n3 for Scissors \n");
        scanf("%d", &A);
    
    
    
    
        B=1+rand()%3;
        printf("Computer Choose %d\n", 1+rand()%3);
    
    
    if(B==1)
        {
        switch (A){
    case 1:
        printf("DRAW1!!");
        break;
    
    
    case 2:
        printf("YOU LOSE1!!");
        break;
    
    
    case 3:
        printf("YOU WIN1!!");
        break;
    
    
    default:
        printf("YOU SUCK!!");
        break;
    
    
        }
        }
    else if(B==2)
        {
        switch (A){
    case 1:
        printf("YOU LOSE2!!");
        break;
    
    
    case 2:
        printf("DRAW2!!");
        break;
    
    
    case 3:
        printf("YOU WIN2!!");
        break;
    
    
    default:
        printf("YOU SUCK!!");
        break;
    
    
        }
        }
    else if(B==3)
        {
        switch (A){
    case 1:
        printf("YOU WIN3!!");
        break;
    
    
    
    
    case 2:
        printf("YOU LOSE3!!");
        break;
    
    
    case 3:
        printf("DRAW3!!");
        break;
    
    
    default:
        printf("YOU SUCK!!");
        break;
    
    
        }
        }
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    4
    i tried two different structures

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        B=1+rand()%3;
        printf("Computer Choose %d\n", 1+rand()%3);
    You likely want
    Code:
        B=1+rand()%3;
        printf("Computer Choose %d\n", B);
    I strongly suggest using variables that are NOT single letters for NON loop control variables.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    6

    Arrow

    i'm modified your code in this simple rock scissors paper game program i'm using enumerations constants..
    Code:
    /**   Rock scissors paper game
    *   coded by hariharan S
    *   coded on nov wed 2014 4.38 PM
    */
    
    
    #include<stdio.h>
    #include<time.h>
    //enumerations constants for the game elements
    enum{rock,scissors,paper};
    
    
    void choice(int player)
    {
        switch(player)
        {
            case 0:printf("Rock");
                break;
            case 1:printf("scissors");
                break;
            case 2:printf("paper");
                break;
            default:break;
        }
    }
    
    
    int main()
    {
            int user,bot;   //two players user and bot
            printf("Rock=0,scissors=1,paper=2 \n");
          
    //get input from the user
        printf("user choice : ");
        scanf("%d",&user);
    
    //input for bot randomly
        srand(time(NULL));
        bot=rand()%3;
    
    //print the two players choice
        printf("User is ");
        choice(user);
       printf("\nBot is ");
       choice(bot);
    
    //print the game result
        if(bot==user)
        {
            printf("\nDraw game");
        }
        else if(user==rock && bot!=paper || user==paper &&bot!=scissors || user==scissors && bot!=rock)
        {
            printf("\nUser won");
        }
        else
            {
                printf("\nbot wons");
            }
    
    
        return 0;
    }
    Last edited by hariharaan; 11-05-2014 at 05:21 AM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, you need to slow down and take a deep breath. Don't keep cranking out programs quickly in the hopes that one might work. Instead, sit down and think about the problem. Use a pencil and paper to help find the logic that needs to be implemented.

    Secondly, make sure your code is neatly formatted. I'm going to use your second program, since that was the last you posted. Notice how much easier it is to read and follow. This will help prevent many mistakes.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int A, B;
        srand(time(NULL));
    
        printf("Enter====> \n1 for Rock \n2 for Paper \n3 for Scissors \n");
        scanf("%d", &A);
    
        B=1+rand()%3;
        printf("Computer Choose %d\n", 1+rand()%3);
    
        if(B==1)
        {
            switch (A)
            {
                case 1:
                    printf("DRAW1!!");
                    break;
                case 2:
                    printf("YOU LOSE1!!");
                    break;
                case 3:
                    printf("YOU WIN1!!");
                    break;
                default:
                    printf("YOU SUCK!!");
                    break;
            }
        }
        else if(B==2)
        {
            switch (A)
            {
                case 1:
                    printf("YOU LOSE2!!");
                    break;
                case 2:
                    printf("DRAW2!!");
                    break;
                case 3:
                    printf("YOU WIN2!!");
                    break;
                default:
                    printf("YOU SUCK!!");
                    break;
            }
        }
        else if(B==3)
        {
            switch (A)
            {
                case 1:
                    printf("YOU WIN3!!");
                    break;
                case 2:
                    printf("YOU LOSE3!!");
                    break;
                case 3:
                    printf("DRAW3!!");
                    break;
                default:
                    printf("YOU SUCK!!");
                    break;
            }
        }
    
        return 0;
    }
    Third, check your compiler warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|6|warning: implicit declaration of function 'srand'|
    main.c|6|warning: implicit declaration of function 'time'|
    main.c|11|warning: implicit declaration of function 'rand'|
    */
    This means you're missing some header files. "srand" and "rand" require stdlib.h, and "time" requires time.h.

    Finally, tell us what problems you are having. All you said is that you are having trouble "with the output". Are you getting the wrong output? Strange output? No output at all? Please be clear on the problems you're having.

    It actually looks like you have a working implementation, when I added the appropriate headers, made the correction indicated by stahta01, and ran it myself. It looks like you have some incorrect print messages; when the computer picks rock and I pick paper, it says I lose (though that should be a win for me).

    So if you're having issues, please be more clear on what they are.

    I'd also suggest reading this (don't take the title personally; this is actually a very important read if you're going to use tech forums): How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rock, Paper, Scissors
    By Slynet in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 07:14 PM
  2. paper rock scissors
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 10:59 AM
  3. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  4. Paper Scissors Rock v0.1
    By Kirdra in forum Game Programming
    Replies: 2
    Last Post: 09-14-2002, 11:32 AM

Tags for this Thread