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

(use c++)There is a game called “rock, paper, scissors”. Each of the two players

ID: 3905913 • Letter: #

Question

(use c++)There is a game called “rock, paper, scissors”. Each of the two players will pick one of these three objects without the other knowing player knowing the opponent’s selection. The rules are that if both people choose the same object, it is a tie. Otherwise, rock beats scissors, scissors beats paper, and paper beats rock. No other input is valid.

            The program will ask for the first input and read it. Then the program will ask for the second input and read it. The program will evaluate the inputs. It will output one of the following results, depending on the input

tie

rock wins

paper wins

scissors wins

invalid input

After the output is displayed, the program will ask “Do you want to play again? (y or n)” and read the reply. As long as the reply is “y” the program will loop back and play the game again. When the user enters “n”, the game is over.

      If you wish to, you may use nested loops to check the input to be sure that it is valid before going on to process the input. For example, if the user entered “u” instead of “y” or “n”, the nested loop would display “invalid input” and let the user enter the answer to “Do you want to play again? (y or n)” again.

Explanation / Answer

#include<iostream>
#include<string>
#include<ctype.h>

using namespace std;

int main(){
   string ch1,ch2;
   int n1;
   int winner = 0;
   while(1){
      winner = 0;
      //while (winner == 0){
         cout << "Enter first input(rock/paper/scissor):";
         cin >> ch1;
         while(ch1 != "rock" && ch1 != "paper" && ch1 != "scissor"){
             cout << "Invalid choice ";
             cout << "Enter (rock/paper/scissor):";
             cin >> ch1;
         }
         cout << "Enter second input(rock/paper/scissor):";
         cin >> ch2;
         while(ch2 != "rock" && ch2 != "paper" && ch2 != "scissor"){
             cout << "Invalid choice ";
             cout << "Enter (rock/paper/scissor):";
             cin >> ch2;
         }
         if (ch1 == ch2)
             cout << "Its a tie ";
         if (ch1 == "paper"){
             if (ch2 == "rock"){
                cout << "paper wins ";
             }
             if (ch2 == "scissor"){
                cout << "scissor wins ";
             }
             winner = 1;
         }
         if (ch1 == "rock"){
             if (ch2 == "paper"){
                cout << "paper wins ";
             }
             if (ch2 == "scissor"){
                cout << "rock wins ";
             }
             winner = 1;
         }
         if (ch1 == "scissor"){
             if (ch2 == "paper"){
                cout << "scissor wins ";
             }
             if (ch2 == "rock"){
                cout << "rock wins ";
             }
             winner = 1;
         }

         cout << "Do you want to play again? (1=yes, 0-No):";
         cin >> n1;
         if (n1 == 0){
            break;
         }

       //}

   }
   return 0;
}