In: Computer Science
Develop a C++ program that plays out a round of Rock, Paper, Scissors using Functional Programming
1) Develop a function that prompts the user to enter their choice (1=Rock 2=Paper 3=Scissors)
2) Develop a function that generates the computer player's choice
3) Develop a function that displays which player won the game round
4) Develop your main function to play one round of RPS versus a randomized computer opponent
input code:
output:
code:
#include <iostream>
using namespace std;
int user_choice()
{
/*declare the variable*/
int choice;
/*print menu*/
cout<<"1.Rock\n2.Paper\n3.Scissors\nEnter your
choice:";
/*take user input*/
cin>>choice;
/*return input*/
return choice;
}
/*computer_choice*/
int computer_choice()
{
/*seed the value*/
srand(time(NULL));
/*return choice*/
return rand()%3+1;
}
void win(int u,int c)
{
/*if user choice Rock*/
if(u==1)
{
/*if Computer choice Rock*/
if(c==1)
{
/*print tie*/
cout<<"----Tie----";
}
/*if Computer choice Paper*/
else if(c==2)
{
/*Computer win*/
cout<<"Paper beat Rock....Computer win";
}
/*if Computer choice Scissors*/
else
{
/*player win*/
cout<<"Rock beat Scissors.....User win";
}
}
/*if user choice Paper*/
else if(u==2)
{
/*if Computer choice rock*/
if(c==1)
{
/*player win*/
cout<<"Paper beat Rock....User win";
}
/*if Computer choice Paper*/
else if(c==2)
{
/*print tie*/
cout<<"----Tie----";
}
/*if Computer choice Scissors*/
else
{
/*Computer win*/
cout<<"Scissors beat Paper.....Computer win";
}
}
/*if user choice Scissors*/
else if(u==3)
{
/*if Computer choice rock*/
if(c==1)
{
/*Computer win*/
cout<<"Rock beat Scissors.....Computer win";
}
/*if Computer choice Paper*/
else if(c==2)
{
/*player win*/
cout<<"Scissors beat Paper....User win";
}
else
/*if Computer choice Scissors*/
{
/*print tie*/
cout<<"----Tie----";
}
}
else
{
/*print Invalid if none of them*/
cout<<"Invalid user input";
}
}
int main()
{
/*call function and store input*/
int u=user_choice();
int c=computer_choice();
/*call win function*/
win(u,c);
return 0;
}