Computer’s Move

Learn to generate the computer's move.

What we have

We have set up the user’s input. This is our inventory of already created functions:

// Function to display game introduction and rules
void displayIntroductionAndRules() {
    cout << "Welcome to Rock-Paper-Scissors game!\n" << endl;
    cout << "The rules of the game are simple:" << endl;
    cout << "1. Rock beats scissors" << endl;
    cout << "2. Scissors beats paper" << endl;
    cout << "3. Paper beats rock" << endl;
}

// Function to get number of rounds
int getRound() {
    int rounds;
    cout << "How many rounds would you like to play? ";
    cin >> rounds;
    return rounds;
}

// Function to get player's choice
string getPlayerChoice() {
    string choice;
    cout << "Choose rock, paper, or scissors: ";
    cin >> choice;
    return choice;
}

In this task, we will create a function to get a move from the computer.

What now?

We’ll use a random number generator between 1 and 3 to select one of the three options and return that option as the computer’s choice.

Try it yourself

Write your code in the code widget below and click the “Run” button to execute your code.

If you’re unsure how to do this, click the “Show Hint” button.

Get hands-on with 1400+ tech skills courses.