Answer to Question #336354 in C++ for Secret Agent

Question #336354

Jack `N Poy


The winner is selected depending on the following rules:

  • Rock beats Scissors
  • Scissors beats Paper
  • Paper beats Rock
  • If both players chose the same option, then it's a tie

Instructions:

  1. In the code editor, you are provided with an enum called option which contains three possible named values:
  2. ROCK - 'r'
  3. PAPER - 'p'
  4. SCISSORS - 's'
  5. Your task is to ask two users for there chosen options. And then based on their options, determine the winner.

Input


1. Option selected by Player 1

2. Option selected by Player 2

Output

If Player 1 wins, print the message "Player 1 wins!"

If Player 2 wins, print the message "Player 2 wins!"

If it's a tie, print the message "It's a tie!"

Player·1:·r
Player·2:·p
Player·2·wins!




1
Expert's answer
2022-05-02T13:36:56-0400
#include <iostream>
#include <string>

using namespace std;

enum weapon{
      ROCK = 0,
      PAPER = 1, 
      SCISSORS = 2
};
string displayWinner(weapon p1, weapon p2){
        if (p1 == 0 && p2 == 2) return "Player 1 wins!";
		if (p1 == 2 && p2 == 1) return "Player 1 wins!"; 
		if (p1 == 1 && p2 == 0) return "Player 1 wins!";
			
        if (p1 == 0 && p2 == 1 ) return "Player 2 wins!";
		if (p1 == 2 && p2 == 0) return "Player 2 wins!";
		if (p1 == 1 && p2 == 2) return "Player 2 wins!";
        if(p1 == p2) return " ... It's a tie! ...";
  }
int main()
{
	int games_played =0;
	char human_choice;
	weapon Player1_weapon;
	weapon Player2_weapon;
	bool again = true;
	while(again){
       
		cout <<  "Option selected by Player 1? ROCK (r), PAPER (p) or SCISSORS (s)  " ;
        cin >> human_choice;
        if(human_choice == 'r') Player1_weapon = ROCK;
        else if(human_choice == 'p') Player1_weapon = PAPER;
        else if(human_choice == 's') Player1_weapon = SCISSORS;
        else cout << "WRONG!!!";

        cout <<  "Option selected by Player 2? ROCK (r), PAPER (p) or SCISSORS (s)  " ;
        cin >> human_choice;
        if(human_choice == 'r') Player2_weapon = ROCK;
        else if(human_choice == 'p') Player2_weapon = PAPER;
        else if(human_choice == 's') Player2_weapon = SCISSORS;
        else cout << "WRONG!!!";
        
        cout << displayWinner(Player1_weapon, Player2_weapon);
		cout << "\n Again? (1-yes, 0-no)" << endl;
		cin >> again;
    }
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS