The winner is selected depending on the following rules:
Instructions:
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!
#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;
}
}
Comments
Leave a comment