Thread: Issue with Rock, Paper, Scissors

  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
    28,413
    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?)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 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. Rock, Paper, Scissors
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 11-06-2001, 05:06 AM

Tags for this Thread