Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Background: The computers role is defined in the class. All input/output must take place inside the main function. Also, I apologize for tagging c++98 and 14. I don't know which version this is.

<pre lang="c++">#include <string>
#include <iostream>
using namespace std;

int ROUNDS;
int USER_CHOICE;
int SCORE;

class ComputersChoice { //Class that contains the random number function for the AI player
    private:
        int randNum = rand() % 5 + 1;
        
    public:
        
        void SetNum(int value) { //Program runs slow for some reason without this parameter and setting it equal to randNum
            randNum = value;
        }
        
        int GetNum() {
            return randNum;
        }  
        
        void printResult() {
        if (randNum == 1) {
            cout <<"The computer chose rock.";
        }
        
        else if (randNum == 2) {
            cout<<"The computer chose paper.";
        }
        
        else if (randNum == 3) {
            cout<<"The computer chose scissors.";
        }
        
        else if (randNum == 4) {
            cout<<"The computer chose lizard.";
        }    
        
        else {
            cout<<"The computer chose spock.";
        }    
            
            
        }       
};

    
int main() {
     
   
   
    //How many rounds does the user want to play?
    cout<<"Do you want to play 3, 5 or 7 rounds?" << endl;
    cin>>ROUNDS;
    
    
    
    switch(ROUNDS) {
        case 3: 
            cout<<"You picked best of 3 rounds."<<endl;
            break;
        case 5: 
            cout<<"You picked best of 5 rounds."<<endl;
            break;
        case 7:
            cout<<"You picked best of 7 rounds."<<endl;
            break;
        default:
            cout<<"Invalid input. Enter 3, 5 or 7."<<endl;
            break;

    
        
}
    //User Input
    cout<<"Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock."<<endl;
    cin>>USER_CHOICE;
    
    switch(USER_CHOICE) {
        case 1:
            cout<<"You chose rock." << endl;;
            break;
        case 2:
            cout<<"You chose paper." << endl;
            break;
        case 3:
            cout<<"You chose scissors." << endl;
            break;
        case 4:
            cout<<"You chose lizard." << endl;
            break;
        case 5:
            cout<<"You chose spock." << endl;
            break;
        default:
            cout<<"Invalid input. Enter 1 for rock, 2 for paper, 3 for scissors, 4 for lizard or 5 for spock." << endl;
            break;
        
        
    }
    
    srand(time(NULL));
    
    //Object creation
    ComputersChoice randomNumber;
    
    //Fetches the random number and prints results accordingly
    randomNumber.GetNum();
    randomNumber.printResult();
    
    

    
    
    
    
    
}


What I have tried:

With the loop I have tried wrapping the entire switch statement in a loop, putting a loop on the outside of the switch statement above or below and inside the cases. I am completely lost on how to compare the random number of the computer to the user choice. The fact the random number is generated in a class, I am unsure how to access the random number.
Posted
Updated 5-Apr-17 22:33pm

1 solution

First off, you need to start by looping on the first user choice: if they enter an "Invalid input" then it complains, but lets them play anyway - that's bad. Add a "valid entry" variable, and loop until you get one.

Second, you need a second loop for the user input of the actual game. This time, you loop for the number of rounds the user selected: 3, 5, or 7.

The way I'd do it, is to abstract the code into functions: start with a GetRounds function:
C++
int GetRounds()
   {
   bool isValid = false;
   int numberRounds = 0;
   while (!isValid)
      {
      cout << "Do you want to play 3, 5 or 7 rounds?" << endl;
      cin >> numberRounds;
      switch(numberRounds) 
         {
         case 3: 
         case 5: 
         case 7:
            isValid = true;
            break;
         default:
            cout << "Invalid input. Enter 3, 5 or 7." << endl;
            break;
         }
      }
   cout<<"You picked best of " << numberRounds << " rounds."<<endl;
   return numberRounds;
   }

Then call that from your Main function.
Add a second "PlayRound" function - you can write that one yourself - and use a for loop to call it the number of times that the user selected.
Make the PlayRounds function return 1 for player win, -1 for computer win, and 0 for a draw, then add up each result. At the end, a positive total is a player win for the session, negative is a computer session victory.

Start by trying that, and see how far you get.
 
Share this answer
 
Comments
Member 13109743 6-Apr-17 14:34pm    
Regarding the PlayRound function, how is it possible to use the information gathered from GetRounds because they're local scopes? Same with comparing the users choice to the computers choice because the computers choice is located in a class - how would I access that?
OriginalGriff 6-Apr-17 14:48pm    
PlayRound doesn't need to access the info from GetRounds: it plays a single round and returns the result.
The outside world - in this case the Main function - calls it as many times as it needs.

As far as the computer class goes - create an instance in Main and pass it to the PlayRound function.

Make sense?
Member 13109743 6-Apr-17 14:55pm    
Yes I think it makes sense. I'm going to mess around with it more, I'll let you know.
Member 13109743 6-Apr-17 17:32pm    
I'm lost trying to figure out how to design the loop.
OriginalGriff 6-Apr-17 17:47pm    
How difficult is it to "design" a for loop? :laugh:
All it needs is a number that runs from 1 (or zero) and lets it loop the number of times they entered...
Think about it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900