In: Computer Science
Code the game of Rock, Paper, Scissors between a human player and the computer. You can check out the game on Wikipedia if you are not familiar with it. Create a 4 option menu with the human player choices, plus the option of exiting the game. Randomly determine the computer’s choice (although a great deal of AI research has gone in to determining the best computer move).
• Loop the game until the human player exits.
• Count the number of times the computer and the human each won.
When you’re done, display the total number of hands played, the number of times the human won and the number of times the computer won. Declare the highest total of winning hands the game winner.
//C++ program
/*
(1) program gets input from user by displaying options ,with
getUserChoice() function.
and Dispaly user choice in text using function displayChoice().
(2) program gets input from computer choice by random number
generator function ,with getComputerChoice() function.
and Dispaly computer choice in text using function
displayChoice().
(3)now it compare User choice and computer choice according to rule given .uses function determineWinner();
(4)if return value of function determineWinner() is true ,then
User is winner and it displays Rules according to which user
wins
and Loop Breaks.
(5)if return value of function determineWinner() is false,then Match Tie. And program keeps reapting steps 1-3;
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
int getUserChoice(){
int choice;
cout<<"1:Rock \n2:Paper \n3:Scissors \n4:Lizard
\n5:Spock\n";
cout<<"Enter your choice : ";
cin>>choice;
return choice;
}
int getComputerChoice(){
return rand()%5+1;
}
bool determineWinner(int User_choice, int Computer_choice){
return (User_choice == 3 && Computer_choice ==
2) || (User_choice == 2 && Computer_choice == 1) ||
(User_choice ==
1 && Computer_choice == 4) || (User_choice == 4 &&
Computer_choice == 5) ||
(User_choice ==
5 && Computer_choice == 3) || (User_choice == 3 &&
Computer_choice == 4) ||
(User_choice ==
4 && Computer_choice == 2) || (User_choice == 2 &&
Computer_choice == 5) ||
(User_choice ==
5 && Computer_choice == 1) || (User_choice == 1 &&
Computer_choice == 3);
}
void displayChoice(int choice){
string option[]={
"","Rock","Paper","Scissors","Lizard","Spock"
};
cout<<option[choice]<<"\n";
}
int main(){
int User_choice , Computer_choice;
int
total_game=0,user_won=0,computer_won=0,tie=0;
char exit;
string Rule[]={
"Scissors cut
paper","Paper covers rock","Rock crushes lizard","Lizard poisons
Spock",
"Spock smashes
(or melts) scissors","Scissors decapitate lizard","Lizard eats
paper",
"Paper disproves
Spock","Spock vaporizes rock","Rock breaks scissors"
};
do{
total_game++;
User_choice =
getUserChoice();
cout<<"Your choice : ";
displayChoice(User_choice);
Computer_choice =
getComputerChoice();
cout<<"Computer choice : ";
displayChoice(Computer_choice);
if(determineWinner (User_choice ,
Computer_choice) ){
user_won++;
cout<<"//////You win/////////\nRule : ";
if(User_choice
== 3 && Computer_choice == 2)
cout<<Rule[0]<<"\n";
else
if(User_choice == 2 && Computer_choice == 1)
cout<<Rule[1]<<"\n";
else
if(User_choice == 1 && Computer_choice == 4)
cout<<Rule[2]<<"\n";
else
if(User_choice == 4 && Computer_choice == 5)
cout<<Rule[3]<<"\n";
else
if(User_choice == 5 && Computer_choice == 3)
cout<<Rule[4]<<"\n";
else
if(User_choice == 3 && Computer_choice == 4)
cout<<Rule[5]<<"\n";
else
if(User_choice == 4 && Computer_choice == 2)
cout<<Rule[6]<<"\n";
else
if(User_choice == 2 && Computer_choice == 5)
cout<<Rule[7]<<"\n";
else
if(User_choice == 5 && Computer_choice == 1)
cout<<Rule[8]<<"\n";
else
if(User_choice == 1 && Computer_choice == 3)
cout<<Rule[9]<<"\n";
cout <<
"\n";
}
else if(determineWinner
(Computer_choice , User_choice) )computer_won++;
else {
cout<<"///////Tie////////\n";
tie++;
}
cout<<"\nexit (y/n) :
";
cin>>exit;
}while(exit!='y');
cout<<"Total games : "<<total_game;
cout<<"\nUser won : "<<user_won;
cout<<"\nComputer won :
"<<computer_won;
cout<<"\ntie : "<<tie<<"\n";
return 0;
}
//sample output