Write a program to play \"Rock, Paper, Scissors, Lizard, Spock.\" A user will en
ID: 3852317 • Letter: W
Question
Write a program to play "Rock, Paper, Scissors, Lizard, Spock." A user will enter a character to represent one of the five items. The program will randomly select one of the five items. Once these two objects have been determined, the program will decide who wins: Rock beats Scissors Paper beats Rock Scissors beats Paper See http: //www.samkass.com/theories/RPSSL.html for additional rules. Display your standard output information using a function named ShowProgramHeader() as on the previous assignments. Prompt user for input (rock, paper, scissors, lizard, or Spock). Get input from user (first player). Generate second player's object (get input from user for second player for initial testing). Decide and display outcome of the game. Program-fully documented. Your final program should contain all test code used during development. Output-test your program using the following input data: R P, p R, S p, p P, L K, p l, K p, s k Output-at least five games using the computer as player 2. Sample calculation sheet-In addition to your program and output, attach a page showing your sample calculations. Check your results using your sample calculations. If you have any questions regarding this assignment, do not hesitate to contact me. Start working on this assignment as soon as possible, so that you have plenty of time to get help if you need it.Explanation / Answer
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void cast(int);
int flag=0;
int main() {
int ch,i=0;
char choice;
do
{
// Seed the RNG once, at the start of the program
srand( time( NULL ) );
cout<<" Menu: ";
cout<<" 1.Rock";
cout<<" 2.Paper";
cout<<" 3.Scissors";
cout<<" Enter your choice : ";
cin>>ch;
if(ch<1||ch>3)
continue;
flag=0;
// Take your chance(start game)
cast(ch);
if(flag==1) //In case both get the same thing
{
cout<<" Its a tie!! Please play again";
continue;
}
cout<<" Do you want to play again ? (y/n)" ; //to continue playing or stop
cin>>choice;
if(choice=='n'||choice=='N')
i=1;
}while(i==0);
return 0;
}
void cast(int ch)
{
// Get a pseudo-random number among (1,2,3)
int num = (rand() % 3) + 1;
cout <<" The system has chosen : ";
switch(num)
{
case 1: cout<<"Rock!";
if(ch==1)
{flag=1;
cout<<" Your choice : Rock";
break;
}
else if(ch==2)
{
cout<<" Your choice : Paper";
cout<<" You Win!! ";
break;
}
else
{
cout<<" Your choice : Scissors";
cout<<" You Lose!! ";
break;
}
case 2: cout<<"Paper!";
if(ch==1)
{ cout<<" Your choice : Rock";
cout<<" You Lose!! ";
break;
}
else if(ch==2)
{ flag=1;
cout<<" Your choice : Paper";
break;
}
else
{
cout<<" Your choice : Scissors";
cout<<" You Win!! ";
break;
}
case 3: cout<<"Scissors!";
if(ch==1)
{ cout<<" Your choice : Rock";
cout<<" You Win!! ";
break;
}
else if(ch==2)
{
cout<<" Your choice : Paper";
cout<<" You Lose!! ";
break;
}
else
{flag=1;
cout<<" Your choice : Scissors";
break;
}
default : break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.