(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Note:I implemented this program using C++
CODE:
OUTPUT:
Raw_code:
//including iostream for input and output streams
#include<iostream>
//including ctime because we use the time function for random
generation
#include<ctime>
//including cstdlib for exit function
#include<cstdlib>
//using standard namespace
using namespace std;
//beginning of the main funcion
int main()
{
//srand function sets the starting point for producing
of random integers
srand((unsigned)time(0));
//game array of 3 values 0,1 and 2 which represents
scissor, rock and paper respectively
int game[4]={0,1,2};//[scissor, rock, and paper]
//integer variable to take value from the user either
0 or 1 or 2
int n;
//int variable selecting option purpose
int op;
//if the op is 0 this loop is terminated
while(op!=0)
{
//menu based program
cout<<"\n0.exit\n1.Play"<<endl;
//taking the option from the
user
cout<<"\nEnter the
option:";
cin>>op;
switch(op)
{
//if the user
select 0 option then we exit from the game
case
0:exit(0);
break;
case
1:cout<<"Enter 0,1 or 2[scissor, rock, and paper]:";
cin>>n;
int i;
//random generation of 0,1
and 2
//which represents scissor,
rock and paper respectively
i=game[rand()%3];
//our value
cout<<"ours:"<<n<<endl;
//randomly generated
value
cout<<"opponent:"<<i<<endl;
//if the value that we given
is equal to randomly generated
//value it shows draws
if(n==i)
cout<<"draws"<<endl;
//if we select scissor and
opponent select paper we wins
else
if(n==0&&i==2)
cout<<"Wins"<<endl;
//if we select rock and
opponent select scissor we wins
else
if(n==1&&i==0)
cout<<"Wins"<<endl;
//if we select paper and
opponent select rock we wins
else
if(n==2&&i==1)
cout<<"Wins"<<endl;
//in all other cases we
loses
else
cout<<"Loses"<<endl;
break;
default:cout<<"Invalid option";
break;
}
}
return 0;
}
Note:THIS IS ONE OF THE BASIC WAY TO IMPLEMENT THIS PROGRAM USING C++
in the above there is no specification of language
************For any queries comment me in the comment box*******************
Get Answers For Free
Most questions answered within 1 hours.