For program 2 you will simulate the game of rock paper scissors (RPS). Prompt th
ID: 3653742 • Letter: F
Question
For program 2 you will simulate the game of rock paper scissors (RPS). Prompt the player to input an r, p, or s for throwing rock, paper or scissors, respectively. You must allow the player to enter either r or R, p or P, and s or S. If an incorrect letter is entered, then output an error and exit the game. For the computer's turn, randomly select rock, paper or scissors by randomly generating numbers to represent rock, paper, and scissors. Determine whether the player or computer wins or if there is a tie and then output the result. Be sure to output what the player's choice and the computer's choice: Rock wins to scissors but loses to paper Paper wins to rock but loses to scissors Scissors win to paper but lose to rock Note: if both choose rock, paper, or scissors then it is a tieExplanation / Answer
Please rate...
Program rps.m
===================================================
while(true)
ui=input('Enter r or R, p or P, s or S: ','s');
if(~(ui=='r' || ui=='R' || ui=='p' || ui=='P' || ui=='s' || ui=='S'))
disp('Wrong input');
break;
end
ci = randi([1 3],1,1);
%ci=1 means rock
%ci=2 means paper
%ci=3 means roscissors
if((ui=='r' || ui=='R') && (ci==3))
disp('Player chose rock, computer chose scissors');
disp('Player wins');
elseif((ui=='p' || ui=='P') && (ci==1))
disp('Player chose paper, computer chose rock');
disp('Player wins');
elseif((ui=='s' || ui=='S') && (ci==2))
disp('Player chose scissors, computer chose paper');
disp('Player wins');
elseif((ui=='r' || ui=='R') && (ci==2))
disp('Player chose rock, computer chose paper');
disp('Computer wins');
elseif((ui=='p' || ui=='P') && (ci==3))
disp('Player chose paper, computer chose scissors');
disp('Computer wins');
elseif((ui=='s' || ui=='S') && (ci==1))
disp('Player chose scissors, computer chose rock');
disp('Computer wins');
elseif((ui=='r' || ui=='R') && (ci==1))
disp('Player chose rock, computer chose rock');
disp('Its a tie');
elseif((ui=='p' || ui=='P') && (ci==2))
disp('Player chose paper, computer chose paper');
disp('Its a tie');
elseif((ui=='s' || ui=='S') && (ci==3))
disp('Player chose scissors, computer chose scissors');
disp('Its a tie');
end
end
===================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.