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
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
#include#include #include using namespace std; int getUserChoice(){ cout << "Welcome to a round of rock, paper, scissors!!!" < 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; return true; } else if(player == computer){ cout << "Tie!" << endl; cout << "Try again." << endl << endl; return false; } else{ cout << "Computer wins!" << endl; return true; } } int main() { srand(time(NULL)); int computer; int player; bool gameOver = false; while(!gameOver) { computer = 1 + rand() % 3; player = getUserChoice(); printComputerChoice(computer); gameOver = displayResult(player, computer); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
