954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rock Paper Scissors c++ code (help me pls)

Hello. I need help in completing my c++ assignment. Below are the question details:

Develop a simple Paper-Scissors-Rock program.
Make it a single player game where the player plays with the computer. The party that
accumulatively wins 3 times first will win the game.
Option: Your program may include a loop that lets the user repeat the game until the user says she
or he is done.

#include<iostream>
#include <cstdlib>  //to use rand function
#include <ctime>  // to use time as the seed for rand
using namespace std;
 
 
int main(){
    int number,usernumber;
    char repeat,userpick;
    int userscore,compscore,value;
    value=0;
    userscore=0;
    compscore=0;
     
     
 
    do{
         
         
        srand(time(0));
        number = 1 + (rand() % 3) ;
         
        userscore=userscore+value;
        compscore=compscore+value;
        cout<<"\nYour Score: "<<userscore<<endl;
        cout<<"Computers Score: "<<compscore<<endl;
         
        cout<<"Pick either (R)ock,(P)aper, or (S)cissors. \n";
        cin>>userpick;
 
        if(userpick=='r'|| userpick=='R'){
            usernumber=1;
            cout<<"so you picked rock";}
        if(userpick=='p'|| userpick=='P'){
            usernumber=2;
            cout<<"so you picked paper";}
        if(userpick=='s'|| userpick=='S'){
            usernumber=3;
            cout<<"so you picked scissors";}
     
 
        if(usernumber==number){
            cout<<"\nuser picked "<<userpick<<" and the computer picked the same thing, so it is a draw";
            userscore++;
            compscore++;}
        if(usernumber==1 && number==2){
            cout<<"\npaper covers rock, you lose";
            compscore++;}
        if(usernumber==1 && number==3){
            cout<<"\nrock beats scissors, you win";
            userscore++;}
        if(usernumber==2 && number==1){
            cout<<"\npaper covers rock, you win";
            userscore++;}
        if(usernumber==2 && number==3){
            cout<<"\nscissors cuts paper, you lose";
            compscore++;}
        if(usernumber==3 && number==1){
            cout<<"\nrock beats scissors, you lose";
            compscore++;}
        if(usernumber==3 && number==2){
            cout<<"\nscissors cut paper, you win";
            userscore++;}
 
        cout<<"\nYour Score: "<<userscore<<endl;
        cout<<"Computers Score: "<<compscore<<endl;
 
     
 
 
 
 
        cout<<"\ndo you want to repeat<y or n>?\n";
        cin>>repeat;
 
 
        system("cls");
    }while(repeat=='y'|| repeat=='Y');
 
    if(repeat=='n'|| repeat=='N'){
        if(userscore>compscore)
            cout<<"you win"<<endl;
        if(userscore<compscore)
            cout<<"you lose"<<endl;
    }
     
 
system("pause");
    return 0;
}

I'm stuck in figuring out how to accumulate the score, and how to end the game after 3 winnings. I would really appreciate if anyone could help me. Thank you.

mftbob
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Code tags and formatting. Your post is impossible to read without them.


[code]
// code here
[/code]

srand(time(0)) should be called only once at the very top of main. It should not be inside of a loop.

Get the system("cls") calls out of there. It's impossible to debug with them in there. If you want to put them back in after it all works, OK, but not before then.

Pick a better name for the variable called value . It's way too vague. I don't know what it represents. As of now, it does nothing. It is initialized to 0, then never changes. You have some code that increments variables by value . Since it's 0, those lines of code do nothing.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
#include<iostream>
#include <cstdlib>  //to use rand function
#include <ctime>  // to use time as the seed for rand
using namespace std;
 
 
int main(){
    int number,usernumber;
    char repeat,userpick;
    int userscore,compscore,value;
    value=0;
    userscore=0;
    compscore=0;
     
     
 
    do{
         
         
        srand(time(0));
        number = 1 + (rand() % 3) ;
         
        userscore=userscore+value;
        compscore=compscore+value;
        cout<<"\nYour Score: "<<userscore<<endl;
        cout<<"Computers Score: "<<compscore<<endl;
         
        cout<<"Pick either (R)ock,(P)aper, or (S)cissors. \n";
        cin>>userpick;
 
        if(userpick=='r'|| userpick=='R'){
            usernumber=1;
            cout<<"so you picked rock";}
        if(userpick=='p'|| userpick=='P'){
            usernumber=2;
            cout<<"so you picked paper";}
        if(userpick=='s'|| userpick=='S'){
            usernumber=3;
            cout<<"so you picked scissors";}
     
 
        if(usernumber==number){
            cout<<"\nuser picked "<<userpick<<" and the computer picked the same thing, so it is a draw";
            userscore++;
            compscore++;}
        if(usernumber==1 && number==2){
            cout<<"\npaper covers rock, you lose";
            compscore++;}
        if(usernumber==1 && number==3){
            cout<<"\nrock beats scissors, you win";
            userscore++;}
        if(usernumber==2 && number==1){
            cout<<"\npaper covers rock, you win";
            userscore++;}
        if(usernumber==2 && number==3){
            cout<<"\nscissors cuts paper, you lose";
            compscore++;}
        if(usernumber==3 && number==1){
            cout<<"\nrock beats scissors, you lose";
            compscore++;}
        if(usernumber==3 && number==2){
            cout<<"\nscissors cut paper, you win";
            userscore++;}
 
        cout<<"\nYour Score: "<<userscore<<endl;
        cout<<"Computers Score: "<<compscore<<endl;
 
     if (userscore==3 )
 {
	cout<<"USER WINS"<<endl ;
	exit(0);
 }
 if(compscore==3)
 {
	cout<<"COMPUTER WINS"<<endl ;
	exit(0);
 }
if(userscore==compscore==3)
{
cout<<"GAME DRAWS"<<endl ;
exit(0);
}
 
        cout<<"\ndo you want to repeat<y or n>?\n";
        cin>>repeat;
 
 
        system("cls");
    }while(repeat=='y'|| repeat=='Y');
 
    if(repeat=='n'|| repeat=='N'){
        if(userscore>compscore)
            cout<<"you win"<<endl;
        if(userscore<compscore)
            cout<<"you lose"<<endl;
    }
     
 
system("pause");
    return 0;
}
imsinu
Newbie Poster
12 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
 

Try splitting main() into several subfunctions, 100 lines makes it frustrating to maintain. Then call one round while the users wants to continue. If round() in declared like this int round() you can return different values deping on who won. Also if you include ctype you can use the function tolower() so you dont have to userpick=='p'|| userpick=='P'

Epicurus
Newbie Poster
10 posts since Sep 2011
Reputation Points: 27
Solved Threads: 0
 

This may be a bit advanced of a suggestion, depending on just how the professor is teaching the course, but: one way to improve the readability of the code would be to use an enumeration for the three different states rather than using numbers for them. Combining this with the previous suggestions would give the following example:

enum PLAY {ROCK, PAPER, SCISSORS};

// ...

PLAY getUserPlay()
{
    char userpick;
    PLAY userplay; 

    cout<<"Pick either (R)ock,(P)aper, or (S)cissors. \n";
    cin>>userpick;
    userpick = toupper(userpick);

    if(userpick=='R'){
        userplay = ROCK;
        cout<<"so you picked rock";
    }
    else if(userpick=='P'){
        userplay = PAPER;
        cout<<"so you picked paper";
    }
    else if(userpick=='S'){
        userplay = SCISSORS;
        cout<<"so you picked scissors";
    }

    return userplay;
}


This makes a nicely readable and concise function which you can use to get the player's choice.

Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: