Function Name: rPS Inputs: (char) Player 1’s name and move (format: \'Name-Move\
ID: 670204 • Letter: F
Question
Function Name: rPS
Inputs:
(char) Player 1’s name and move (format: 'Name-Move')
(char) Player 2’s name and move (format: 'Name-Move')
Outputs:
1. (char) Sentence containing outcome explanation and winning player(s)
Function Description:
Want to play Rock, Paper, Scissors? No thanks! The old R-P-S game is so boring and traditional. Instead of plain-old rock, how about some crystals with personalities, like timeless quartz, or chalky limestone? You decide to make a new R-P-S game that allows users to add panache to their moves. Write a MATLAB function that determines which player wins this new spin on Rock, Paper, Scissors.
The rules are as follows:
All Rock Types Are Equal:
Possible Rock Types: 'Sapphire', 'Ruby', 'Quartz', 'Limestone', 'Diamond'. All Scissor Types Are Equal:
Possible Scissor Types: 'Safety', 'Edge', 'Hair', 'Craft'. All Paper Types Are Equal:
Possible Paper Types: 'Cardstock', 'Origami', 'Wrapping', 'Graphing'.
Possible Scenarios
When Someone Wins
When a Tie Occurs
Paper Type > Rock Type
Rock Type == Rock Type (Tie)
Rock Type > Scissor Type
Scissor Type == Scissor Type (Tie)
Scissor Type > Paper Type
Paper Type == Paper Type (Tie)
Function Output
'<P# move> beats <P~# move>. <P# name> wins.'
(where # is the player with better move)
Notes:
All names and moves will be capitalized and separated by a hyphen.
Hints:
Switch statements may be very useful.
Possible Scenarios
When Someone Wins
When a Tie Occurs
Paper Type > Rock Type
Rock Type == Rock Type (Tie)
Rock Type > Scissor Type
Scissor Type == Scissor Type (Tie)
Scissor Type > Paper Type
Paper Type == Paper Type (Tie)
Function Output
'<P# move> beats <P~# move>. <P# name> wins.'
(where # is the player with better move)
'Tie; try again.'
Explanation / Answer
disp('A game of Rock-Paper-Scissors')
%input Players choice
name=input('Make your move:','s');
%Create a numeric variable for player e.g. if
%player_name='Paper' then payer_number=1
if strcmp(name,'Rock')
player_number=2;
elseif strcmp(name,'Paper')
player_number=1;
elseif strcmp(name,'Scissors')
player_number=0;
else
error('myApp:argChk','Not a valid name,please choose only one of the three given super powers')
end
%Create a random number for the computer
computer_number=rand([0,2],1);
%Depending on the number value,create a string value for computer's choice
if computer_number==0
computer_name='Scissors';
elseif computer_number==1
computer_name='Paper';
else
computer_name='Rock';
end
%Compute the difference,make the comparision and find the winner
diff=mod(player_number-computer_number,3);
fprintf('player choose %s/n computer choose %s/n', name,computer_name)
if diff==2
disp('player wins')
elseif diff==1
disp('computer wins')
else
disp('draw')
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.