Write a program that plays the popular scissor-rock- paper game. (A scissor can
ID: 3796357 • Letter: W
Question
Write a program that plays the popular scissor-rock- paper 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. Here are sample runs: scissor (0), rock (1), paper (2): 1 Enter The computer is scissor. You are rock. You won. scissor (0), rock (1), paper (2): 2 Enter The computer is paper. You are paper too. It is a draw.Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main()
{
int v1, v2; // Values to represent one of the events Rock, Paper, Scissor
int n, cwon, uwon, tie;
n = 1;
cwon = 0;
uwon = 0;
tie = 0;
do
{
v1 = rand() % 3; // Generates random values == 0,1,2
/* 0 = Rock
1 = Paper
2 = Scissor
*/
cout << "Enter!!! 0 = Rock, 1 = Paper, 2 = Scissor ";
cin >> v2;
if ((v1 == 0 && v2 == 0) || (v1 == 1 && v2 == 1) || (v1 == 2 && v2 == 2))
{
if (v1 == 0)
cout << " Computer is Rock. You are Rock. Its a draw";
if (v1 == 1)
cout << " Computer is Paper. You are Paper. Its a draw";
if (v1 == 2)
cout << " Computer is Scissor. You are Scissor. Its a draw";
}
if (v1 == 0 && v2 == 1)
cout << " Computer is Rock. You are Paper. You won";
if (v1 == 0 && v2 == 2)
cout << " Computer is Rock. You are Scissor. You lose";
if (v1 == 1 && v2 == 0)
cout << " Computer is Paper. You are Rock. You lose";
if (v1 == 1 && v2 == 2)
cout << " Computer is Paper. You are Scissor. You won";
if (v1 == 2 && v2 == 0)
cout << " Computer is Scissor. You are Rock. You won";
if (v1 == 2 && v2 == 1)
cout << " Computer is Scissor. You are Paper. You lose";
//cout << " V1 = " << v1 << " V2 = " << v2 <<" "; // if you want to see values of v1 and v2, remove this comment
cout << " Do you want continue (Press 1 for yes / Press 0 for No) ";
cin >> n;
} while (n != 0);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.