in c++ 1. Write a function named kids_game 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 function accept two argument, a randomly generated number 0, 1, or 2 representing scissor, rock, or paper and an integer which will be entered by user, that can only be 0, 1, or 2. The function will displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs:
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
void kids_game(int gen, int choice){
if(gen == choice) cout<<"Draw\n";
else if(gen == 0 && choice == 1) cout<<"You win\n";
else if(gen == 0 && choice == 2) cout<<"Computer wins\n";
else if(gen == 1 && choice == 2) cout<<"You win\n";
else if(gen == 2 && choice == 1) cout<<"Computer wins\n";
else if(gen == 1 && choice == 0) cout<<"Computer wins\n";
else if(gen == 2 && choice == 0) cout<<"You win\n";
}
int main(){
int choice, gen;
string picks[3] = {"Scissors", "Rock", "Paper"};
char c;
srand(time(NULL));
do{
cout<<"Make your decision\n 0. Scissors\n 1. Rock\n 2. Paper\n";
do{
cin>>choice;
if(choice < 0 || choice > 2) cout<<"\nInvalid choice, try again\n";
}while(choice < 0 || choice > 2);
gen = rand() % 3;
cout<<"\nYour pick: "<<picks[choice]<<endl;
cout<<"Computer pick: "<<picks[gen]<<endl;
kids_game(gen, choice);
cout<<"\nPlay again? (y, n)\n";
cin>>c;
}while(c == 'y' || c == 'Y');
return 0;
}
Comments
Leave a comment