Question
I need help with writing a MATLAB program that can automatically play Rock Paper Scissors Lizard Spock. I have finished most of the code I
I need help with writing a MATLAB program that can automatically play Rock Paper Scissors Lizard Spock. I have finished most of the code I just can't figure out how to loop the game using the while function so that the game will repeat until the player or computer get best 2 out of 3. It is step 47. in the picture. Here is my code so far and please don't make things look confusing.
% Purpose: Rock Paper Sissors Lizard Spock Game Hw#3
clc clear
rng('shuffle') human = 0; computer = 0; % Intro to game
fprintf('This is Rock-Paper-Sissors-Lizard-Spock, a variation of ') fprintf('Rock-Paper-Sissors which is played exactly like the ') fprintf('original game, have fun!! ') % Game code
fprintf('Rock-Paper-Sissors-Lizard-Spock! ') % Choices for the game fprintf('Press 1 for Rock ') fprintf('Press 2 for a piece of Paper ') fprintf('Press 3 for a pair of Scissors ') fprintf('Press 4 for Lizard ') fprintf('Press 5 for Spock ')
a = input('What object will you make with your hand?: '); if a == 1 disp('Player made a rock') elseif a == 2 disp('Player made a piece of paper') elseif a == 3 disp('Player made a pair of sissors') elseif a == 4 disp('Player made a Lizard') elseif a == 5 disp('Player made a Spock') else disp('Player made a rock') end
b = randi(5); if b == 1 disp('Computer made a rock') elseif b == 2 disp('Computer made a piece of paper') elseif b == 3 disp('Computer made a pair of sissors') elseif b == 4 disp('Player made a Lizard') elseif b == 5 disp('Computer made a Spock') else disp('Computer function has issue') end
if a==b disp('Player and computer both tied this round') elseif a==3 & b==2 disp('Scissors cuts paper, Player wins') human = human +1 elseif a==2 & b==3 disp('Scissors cuts paper, Computer wins') computer = computer +1 elseif a==2 & b==1 disp('Paper covers rock, Player wins') human = human +1 elseif a==1 & b==2 disp('Paper covers rock, Computer wins') computer = computer +1 elseif a==1 & b==4 disp('Rock crushes lizard, Player wins') human = human +1 elseif a==4 & b==1 disp('Rock crushes lizard, Computer wins') computer = computer +1 elseif a==4 & b==5 disp('Lizard poisons Spock, Player wins') human = human +1; elseif a==5 & b==4 disp('Lizard poisons Spock, Computer wins') computer = computer +1 elseif a==5 & b==3 disp('Spock smashes sissors, Player wins') human = human +1 elseif a==3 & b==5 disp('Spock smashes sissors, Computer wins') computer = computer +1 elseif a==3 & b==4 disp('Scissors decapitates Lizard, Player wins') human == human +1 elseif a==4 && b==3 disp('Scissors decapitates Lizard, Computer wins') computer = computer +1 elseif a==4 & b==2 disp('Lizard eats paper, Player wins') human = human +1 elseif a==2 & b==4 disp('Lizard eats paper, Computer wins') computer = computer +1 elseif a==2 & b==5 disp('Paper disproves Spock, Player wins') human = human +1 elseif a==5 & b==2 disp('Paper disproves Spock, Computer wins') computer = computer +1 elseif a==5 & b==1 disp('Spock vaporizes rock, Player wins') human = human +1 elseif a==1 & b==5 disp('Spock vaporizes rock, Computer wins') computer = computer +1 elseif a==1 & b==3 disp('Rock crushes scissors, Player wins') human = human +1 elseif a==3 & b==1 disp('Rock crushes scissors, Computer wins') computer = computer +1 elseif a==1 & b==3 disp('Rock crushes scissors, Player wins') human = human +1 elseif a==3 & b==1 disp('Rock crushes scissors, Computer wins') computer = computer +1 else disp('All possible combinations have been covered, error') end
a. Add an Iprintl of a disp Function to indicate m a w u TULLIU . b. Increment the number of wins for the human player variable by 1. 43. Elseif the object the human player makes is equivalent with 3 AND the object the computer player makes is equivalent with 1: a. Add an fprintf or a disp function to indicate the computer wins due to "rock crushes scissors". b. Increment the number of wins for the computer player variable by 1. 44. Else: a. Add an fprintf or a disp function to indicate this condition should never happen. (All the possible combinations have been covered by the elseif statements. If we see this printed in the command window, we need to debug the elseif statements.) 45. End. 46. Run the program a few times and check the object the human player makes, the object the computer player makes, and the decision about who wins from the command window. After ensure they are correct, add semicolon :) to the end of every line with the assignment operation () to stop the auto repeating values. 47. Now we want to add a while loop to keep looping steps 6 through 45 while no one wins 2 hands. a. Highlight the code block with codes generated from steps 6 through 45 and click the increase indent button b. Insert the keyword while right above the disp or fprintf function from step 6. Add an end right after the end statement for step 45. c. Add the while loop logical expression to loop while the number of wins for the human player variable value is less than 2 AND the number of wins for the computer player variable value is less than 2 48. Test the program a few times to ensure the while loop will end when one of the players wins 2 hands After checking they are correct, add semicolon ) to the end of every line with the assignment operation (-) to stop the auto repeating values in the command window 49. If the number of wins for the human player is greater than the number of wins for the computer player: a. Add an fprintf or a disp function to congratulate the human 50. Else: a. Add an fprintf or a disp function to congratulate the computer, Homework 3 Pseudo Code: (There are many ways to write a program. Here is one of the potential steps to write Homework 3.) 1. Add your name, purpose, and copyright the program. 2. Clear command window and clear all variables. 3. Randomize the pseudorandom number generator with the MATLAB built-in rng function and provide "shuffle' as the function input. 4. Create a variable that will track the number of wins for the human player. Set the value of this variable to 0. (O means o win.) 5. Create another variable that will track the number of wins for the computer player. Set the value of this variable to 0. 6. Add one or more disp or fprintf function(s) to print a welcome message and briefly explain the game rules and score rules in the command window. (For example, enter 1 to make a Rock, enter 2 to make a piece of Paper, enter 3 to make a pair of Scissors, enter 4 to make a Lizard, and enter 5 to get Spock.) 7. Add an input function to ask the human player to enter the object the human player makes from keyboard. Save the human player's keyboard entry in a new variable. 8. If the object the human player makes is equivalent with 1: a. Add an fprintf or a disp function to indicate the human player made a Rock. 9. Elseif the object the human player makes is equivalent with 2: a. Add an fprintf or a disp function to indicate the human player made a piece of paper. 10. Elseif the object the human player makes is equivalent with 3: a. Add an fprintf or a disp function to indicate the human player made a pair of scissors. 11. Elseif the object the human player makes is equivalent with 4: a. Add an fprintf or a disp function to indicate the human player made a lizard. 12. Elseif the object the human player makes is equivalent with 5: a. Add an fprintf or a disp function to indicate the human player got Spock. 13. Else: Add an forintf or a disn function to indicate the human nlaver made a rock /Treat all other
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started