Write a MATLAB function M-file named RPSLK to play a round of Rock-Paper-Scissor
ID: 3686280 • Letter: W
Question
Write a MATLAB function M-file named RPSLK to play a round of Rock-Paper-Scissors-Lizard-Spock. The input arguments to the function will be two characters, P1 & P2, one for each player in the game. The character values can be R (for rock), P (for paper), S (for scissors), L (for lizard) and K (for Spock). The output argument, outcome, is for the result The function should return 1 if player 1 wins, 2 if player 2 wins, and zero if it is a tie. This game is traditionally played with hand gestures, which we have replaced with the five characters. The winner is determined as shown in the graphic. For example, lizard (mouth-shaped) beats Spock (live long & prosper) and paper (flat hand), as indicated by the green arrows. The function should return a red error message for the following conditions. Any input is nonscalar Any input is not one of the 5 specified characters:Explanation / Answer
function RockPaperScissorsLizardSpock(player1, player2, rounds)
%// creating the table with all combinations
header = {'Rock';'Paper';'Scissors';'Lizard';'Spock'};
Rock = [0;-1;1;1;-1];
Paper = [1;0;-1;-1;1];
Scissors = [-1;1;0;1;-1];
Lizard = [-1;1;-1;0;1];
Spock = [1;-1;1;-1;0];
T = table(Rock,Paper,Scissors,Lizard,Spock,'RowNames',header);
%// play and display winners of every round
points = 0;
for ii = 1:rounds
pointsRound = T{player1(ii), player2(ii)}; %// no need for if-conditions
%// one line is enough
points = points + pointsRound;
if pointsRound > 0; disp(['Player 1 wins round ' num2str(ii) '!'])
elseif pointsRound < 0; disp(['Player 2 wins round ' num2str(ii) '!'])
else disp(['Draw in round ' num2str(ii) '!'])
end
end
%// display overall winner
if points > rounds/2; disp(1)
elseif points == 0; disp(0)
else disp(2)
end
%//now playing the game
player1 = {'Rock','Scissors','Scissors'}
player2 = {'Paper','Rock','Scissors'}
RockPaperScissorsLizardSpock(player1, player2, 3)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.