Like Tree1Likes
  • 1 Post By laserlight

Issue with Rock, Paper, Scissors

This is a discussion on Issue with Rock, Paper, Scissors within the C++ Programming forums, part of the General Programming Boards category; I've written a basic program to play Rock Paper Scissors with a user. If the user beats the cpu, "You ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Unhappy Issue with Rock, Paper, Scissors

    I've written a basic program to play Rock Paper Scissors with a user. If the user beats the cpu, "You beat the computer!" is supposed to be displayed. If the cpu beats the user, "Rats! The computer won." is supposed to be displayed.

    This works brilliantly, except for when either the user or the cpu chooses scissors. Then, their respective choices are displayed like they are supposed to be, but neither of the above messages is displayed.

    The problem is only involving scissors. Everything I wrote involving scissors is exactly the same as for paper and rock, as far as I can tell... am I missing a glaring issue?

    Please help!

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    
    int main()
    {
        //Define
        srand(time(0));
        int cpuMove = rand() % 3;
    
    
        //Prompt user
        cout << "Enter 0(rock), 1(paper) or 2(scissors) > ";
        int userMove;
        cin >> userMove;
    
    
        //State user's move
        if (userMove == 1)
            cout << "You played paper." << endl;
        else if (userMove == 2)
            cout << "You played scissors." << endl;
        else if ((userMove > 2) || (userMove <= 0))
            userMove = 0;
        
        if (userMove == 0)
            cout << "You played rock." << endl;
    
    
    
    
        //State computer's move
        if (cpuMove == 1)
            cout << "The computer played paper." << endl;
        else if (cpuMove == 2)
            cout << "The computer played scissors." << endl;
        else
            cout << "The computer played rock." << endl;
    
    
        //Declare winner
        if ( (userMove == cpuMove) || ((userMove == 1) && (cpuMove == 0)) || ((userMove = 0) && (cpuMove == 2)) || ((userMove == 2) && (cpuMove = 1)) )
            cout << "You beat the computer!" << endl;
    
    
        if ( ((userMove == 1) && (cpuMove == 2)) || ((userMove == 0) && (cpuMove == 1)) || ((userMove == 2) && (cpuMove == 0)) )
            cout << "Rats! The computer won." << endl;
        
    
    
        system("pause");
    
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    I note that you wrote cpuMove = 1 where you probably intended to write cpuMove == 1

    (Do you have to say "CPU" rather than just "computer"? I mean, if you say that the CPU won, wouldn't the memory get jealous?)
    Elkvis likes this.
    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

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Gosh, you're right, thanks for catching that. Still didn't fix it though... sigh.

    I think the memory can probably live with being ignored this one time

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You also say (userMove = 0) where you mean (userMove == 0)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Thanks a bunch! Is that all you found??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Well, you should test and find out, and now that you know you are prone to making this kind of mistake, keep a look out for them.

    Also, compiling at a high warning level can help get the compiler hint of such potential problems.
    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

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Fixed it! Thank you!! Now how to repeat the "winner" message with a switch statement?

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, 06:14 PM
  2. paper rock scissors
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:59 AM
  3. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 09:16 PM
  4. Rock, Paper, Scissors
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 11-06-2001, 04:06 AM

Tags for this Thread


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