Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ ONLY - We have a fully functioning rock-paper-scissors game. Now we want

ID: 3750828 • Letter: I

Question

IN C++ ONLY - We have a fully functioning rock-paper-scissors game. Now we want to modify our game so the user can continue to play another game if they choose. We will need to wrap our code in a while loop until the user says they don't want to play anymore. When the user says they don't want to play again, print out a nice message thanking them for playing.

As the user continues to play, keep track of how many games the user won, lost, and tied. When the user stops playing, print out how many games were won, lost, and tied.

Editable Code

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int whoWon = 0; // Global variable to find who won and keep track


int getUserChoice(){
cout << "Welcome to a round of rock, paper, scissors!!!" <<endl;
cout << "Choose your weapon" << endl;
cout << "1. Rock" << endl;
cout << "2. Paper" << endl;
cout << "3. Scissors" << endl;
int option;
while(true){
cout << "Choose an option: ";
cin >> option;
if(option < 1 || option > 3){
cout << "Error. Invalid choice. Try again!!" << endl;
}
else{
break;
}
}
return option;
}
void printComputerChoice(int choice){
if(choice == 1){
cout << "Computer chose Rock" << endl;
}
else if(choice == 2){
cout << "Computer chose Paper" << endl;
}
else{
cout << "Computer chose Scissors" << endl;
}
}
bool displayResult(int player, int computer){
if((player == 1 && computer == 3) || (player == 2 && computer == 1) || (player == 3 && computer == 2)){
cout << "You win!" << endl;
whoWon = 1;
return true;
}
else if(player == computer){
whoWon = 0;
cout << "Tie!" << endl;
cout << "Try again." << endl << endl;
return false;
}
else{
whoWon = 2;
cout << "Computer wins!" << endl;
return true;
}
}


int main()
{
srand(time(NULL));
int computer;
int player;
bool playing = true;
int userWon = 0;
int computerWon = 0;
int tie = 0;

do {
bool gameOver = false;
while(!gameOver)
{
computer = 1 + rand() % 3;
player = getUserChoice();
printComputerChoice(computer);
gameOver = displayResult(player, computer);

if(whoWon == 1) {
userWon++;
}

if(whoWon == 2) {
computerWon++;
}

if(whoWon == 0) {
tie++;
}

}

cout << "Want to play another (type 1): ";

int x;
cin >> x;

if(x != 1) {
playing = false;
cout << "Thankyou for playing!" << endl;

cout << "You won " << userWon << " games" << endl;
cout << "Computer won " << computer << " games" << endl;
cout << "Total tied " << tie << " games" << endl;
}

} while(playing != false);

return 0;
}

There is the need for one global variable to keep track of whoWon, which is then used in main function

to keep the count.