Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that that plays the rock paper scissors game A rock should b

ID: 673547 • Letter: W

Question

Write a C++ program that that plays the rock paper scissors game A rock should beat scissors, scissors beats paper and paper beats rock. If both players choose the same choice, it results in a tie. You first need to ask the user how many games they wish to play. Unlike the normal game, do not replay ties until a winner is found. Make the computer randomly generate a choice between rock, paper or scissors (should be approximately 1/3 probability for each move). Each game, you should accept an input from the user for their choice between rock, paper or scissors by entering 'R', 'P' or 'S' respectively. If the user inputs a non-valid choice, the program should end. After the specified number of games have been played, display whether the computer or player won more games or if it was a draw if both won the same amount of games. Do not copy an existing rock-paper-scissors game code from online!

Explanation / Answer

#include<iostream>

#include<time.h>

using namespace std;

string getRPS(int i){

switch (i) {

case 0:

return "ROCK";

break;

  

case 1:

return "PAPER";

break;

  

case 2:

return "SCISSORs";

break;

}

return "";

}

int rps(int i, int j) {

if(i==j) {

cout<<"Your both tie by playing "<<getRPS(j)<<" ";

return 0;

}

if (i==0 && j==2) {

cout<<"Your "<<getRPS(i)<<" beat the computer's "<<getRPS(j)<<"! ";

return 1;

}

if(i==1 && j==0) {

cout<<"Your "<<getRPS(i)<<" beat the computer's "<<getRPS(j)<<"! ";

return 1;

}

if(i==2 && j==1) {

cout<<"Your "<<getRPS(i)<<" beat the computer's "<<getRPS(j)<<"! ";

return 1;

}

cout<<"Your "<<getRPS(i)<<" lost to the computer's "<<getRPS(j)<<"! ";

return 0;

}

int main() {

srand(time(NULL));

  

  

  

int count = 0 ;

cout<<"How many games of Rock-Paper-Scissor do you wish to play? ";

cin>>count;

char in;

int win=0;

  

while(count != 0) {

  

cout<<"Rock....Paper....Scissors....! ";

cin>>in;

int random = (rand()%3);

  

switch (in) {

case 'R':

win += rps(0, random);

break;

  

case 'P':

win += rps(1, random);

break;

  

case 'S':

win += rps(2, random);

break;

  

default:

cout<<"Invalid input, stopping..";

return 0;

break;

}

  

count--;

}

  

if(win > count-win) {

cout<<"Congratulations, you beat the computer!";

}

else if(win < count-win) {

cout<<"You lost to the computer, better luck next time.";

}

else {

cout<<"The game ends in a draw.";

}

cout<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote