(c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows:
When the program begins, the user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard using a menu in a function, userChoice, that returns a character.
Next, there should be a function, computerChoice, that generates the computer’s play. A random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. The computer’s choice is returned as a character.
After, a function, determineWinner, will determine the winner between the user’s choice vs. the computer’s choice. (NOTE: It will return nothing, but it will take in the user’s choice and the computer’s choice as the two arguments.) The result is selected according to the following rules:
If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.)
If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.)
If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)
If both players make the same choice, the game ends in a draw and there is no winner.
Finally, after a result is selected, there should be a function, playAgain, in which the player should have the option of playing again. This should return a boolean.
Be sure that the program contains at least the four functions mentioned in parts a – d.
Here is the code that I have implemented.
Source code:
#include<iostream>
#include<ctype.h>
#include <stdlib.h>
using namespace std;
char userChoice(){
char choice;
cout<<"Welcome to Rock/Paper/Scissors Game! Here are your choices.\n";
cout<<"(R/r) Rock\n(P/p) Paper\n(S/s) Scissors\n";
cin>>choice;
if(choice!='R' && choice!='r' && choice!='P' && choice!='p' && choice!='S' && choice!='s' && choice!='Q' && choice!='q'){
cout<<"Invalid Choice.Please try again!!\n";
userChoice();
}
return choice;
}
char computerChoice(){
/*
Here rand will generate number from 0 to RAND_MAX
but we modulo it to get number in range 1-3
*/
int randchoice=((rand()%3)+1);
char choice;
switch(randchoice){
case 1:
choice='R';
break;
case 2:
choice='P';
break;
case 3:
choice='S';
break;
}
return choice;
}
void determineWinner(char usrchoice,char compchoice){
//Rock v\s scissors Rock wins
if((usrchoice=='R' || usrchoice=='r') && (compchoice=='S' || compchoice=='s')){
cout<<"User wins\n";
return;
}
else if((usrchoice=='S' || usrchoice=='s') && (compchoice=='R' || compchoice=='r')){
cout<<"Computer wins\n";
return;
}
//scissors v\s paper scissors wins
else if((usrchoice=='S' || usrchoice=='s') && (compchoice=='P' || compchoice=='p')){
cout<<"User wins\n";
return;
}
else if((usrchoice=='P' || usrchoice=='p') && (compchoice=='S' || compchoice=='s')){
cout<<"Computer wins\n";
return;
}
//paper v\s rock paper wins
else if((usrchoice=='P' || usrchoice=='p') && (compchoice=='R' || compchoice=='r')){
cout<<"User wins\n";
return;
}
else if((usrchoice=='R' || usrchoice=='r') && (compchoice=='P' || compchoice=='p')){
cout<<"Computer wins\n";
return;
}
//Both have same choice game draws
else if(tolower(usrchoice)==tolower(compchoice)){
cout<<"There is no winner.Game draw\n";
return;
}
}
bool playagain(){
bool b;
char ch;
cout<<"Do you want to play again (Y/y) for yes (N/n) for no)?";
cin>>ch;
if(ch=='Y' || ch=='y')
b=true;
else if(ch=='N' || ch=='n')
b=false;
else{
cout<<"Invalid Option.Please try again!!!\n";
b=playagain();
}
return b;
}
void showChoices(char usrchoice,char cmpchoice){
string usr,cmp;
//user choice
if(usrchoice=='R' || usrchoice=='r'){
usr="Rock";
}
else if(usrchoice=='P' || usrchoice=='p'){
usr="Paper";
}
else{
usr="Scissors";
}
//computer choice
if(cmpchoice=='R' || cmpchoice=='r'){
cmp="Rock";
}
else if(cmpchoice=='P' || cmpchoice=='p'){
cmp="Paper";
}
else{
cmp="Scissors";
}
cout<<"Your choice: "<<usr<<endl;
cout<<"Computer Choice: "<<cmp<<endl;
}
int main(){
while(true){
char usrchoice=userChoice();
char compchoice=computerChoice();
showChoices(usrchoice,compchoice);
determineWinner(usrchoice,compchoice);
bool b=playagain();
// In case b is true we play again else we terminate
if(b)
continue;
else{
cout<<"Thank you for Playing Rock/Paper/Scissors\n";
break;
}
}
}
Output:

Feel free to reach out to me in comment in case you have any doubt or need any clarifications. I will be more than happy to help.
Welcome to Rock/Paper/Scissors Game! Here are your choices. (R/r) Rock (P/p) Paper (S/3) Scissors Your choice: Rock Computer Choice: Paper Computer wins Do you want to play again (Y/y) for yes (N/n) for no)?y Welcome to Rock/Paper/Scissors Game! Here are your choices. (R/r) Rock (P/p) Paper (S/s) Scissors Your choice: Paper Computer Choice: Paper There is no winner.Game draw Do you want to play again (Y/y) for yes (N/n) for no) ?n Thank you for Playing Rock/Paper/Scissors ...Program finished with exit code O Press ENTER to exit console. I
(c++ only)Write a program that lets the user play the game of Rock, Paper, Scissors against...
(Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...
IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....
Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...
C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...
In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...
In python language
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...
Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the...
Write this program using C++ , (Rock, Paper, Scissors Game – Page 373) This programming assignment is from the textbook with some slight modifications and clarifications. Here is what I am going to except from you. Your program should include at least the following functions: getComputerGuess(): this function should return a random value generated by the computer using rand function (read more about random numbers in Chapter 3 pages 126 – 128). getUsersChoice(): this function will ask the user to...
(C++) Hey guys we just went over loops and it got me
confused, it has me scrathcing my head for the past two days. I
would appreciate any help you guys have to offer. Few places I have
a hard time is get input and determine winner(cummulative
part).
Write a program that lets the user play the Rock, Paper,
Scissors game against the computer. The computer first chooses
randomly between rock, paper and scissors, but does not display its
choice....
C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...