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.

Recommended Answers

All 4 Replies

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.

#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;
}

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'

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.