Check for the Winner
Learn to check for the winner of the game.
We'll cover the following
What we have
We have completed all the steps to be able to play the game. 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;
}
// Function to generate computer's choice
string generateComputerChoice() {
srand(time(NULL));
int rand_num = rand() % 3;
if (rand_num == 0) {
return "rock";
} else if (rand_num == 1) {
return "paper";
} else {
return "scissors";
}
}
Considering these steps, we won’t be able to end the game because there is still no check when the game will end.
What now?
We can create a function that compares players’ choices and prints who wins the game. It should also update the scores.
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.