Question

Create MATLAB code for a rock paper scissors game. It is expected to use an if...

Create MATLAB code for a rock paper scissors game. It is expected to use an if statement, a loop, and a function in the code. Make sure to properly comment the code as well.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

n = input('Enter the number of times you wish to play ');
i = 0;
player_Score = 0; % intialise the scores
computer_Score = 0;
while i < n % iterate over the number of times user wishes to play
player_plays = input('Enter r/p/s to play ','s'); % prompt the user for his turn
computer_plays_out = computer_plays();
fprintf(['Computer played ' computer_plays_out '\n']); % output what the computer played
if player_plays == 'r' % if player plays rock update the scores accordingly
if computer_plays_out == 'p'
computer_Score = computer_Score + 1;
elseif computer_plays_out == 's'
player_Score = player_Score + 1;
end
elseif player_plays == 'p' % if player plays scissors update the scores accordingly
if computer_plays_out == 's'
computer_Score = computer_Score + 1;
elseif computer_plays_out == 'r'
player_Score = player_Score + 1;
end
elseif player_plays == 's' % if player plays paper update the scores accordingly
if computer_plays_out == 'r'
computer_Score = computer_Score + 1;
elseif computer_plays_out == 'p'
player_Score = player_Score + 1;
end
else
fprintf('Enter valid input'); % prompt the user for another time without incrementing i
i = i-1;
end
i = i+1; % Update the loop counter
fprintf("Scores\n Player : %.f \n Computer : %.f \n",player_Score,computer_Score); % Show the scores
end
if player_Score > computer_Score % Show the results
fprintf("You beat the computer by %.f \n",player_Score-computer_Score);
elseif player_Score < computer_Score
fprintf("You lost to computer by %.f \n",-player_Score+computer_Score);
else
fprintf("It was a tie \n");
end
function out = computer_plays() % returns 'r' / 'p' / 's' randomly
output_arr = ['r' 'p' 's'];
random_num = randi(3); % randi(a) returns a random number between 1 and a
out = output_arr(random_num); % return r,p,s randomly
end

Add a comment
Know the answer?
Add Answer to:
Create MATLAB code for a rock paper scissors game. It is expected to use an if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the...

    Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the computer. x=rand; if x< 1/3 then it is Rock, if l/3<= x < 2/3 it is Paper else it is Scissors end if This is how the game should look like on the screen: Enter "r" for Rock, "p" for Paper, or "s" for Scissors", or enter "q" to Quit the game r leftarrow say, this is what you entered Computer choice: Scissors RESULT:...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long...

    Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long as the user enters 'Y'. Make this case sensitive; if they enter a lower case y the game will not continue. If the user enters in anything besides upper case Y the game will end. Your text must exactly match the examples below: Example 1 with correct input Let's play Rock, Paper, Scissors Enter 1 for rock, 2 for paper, 3 for scissors 2...

  • Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive:...

    Write a computer game based on rock-paper- scissors in C++. The 2-player game will be interactive: human vs. computer! Making the game interactive presents a new issue for us -- how do we get the computer&#39;s choice? To solve this, we will use a &quot;random number generator&quot;, which is the basis for all computer gaming. This enables the properly programmed computer to act like it is actually &quot;thinking&quot; and making its own decisions. This is the largest, most involved program...

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There...

    JAVA Beginnings of a paper-rock-scissors game. Paper-rock-scissors is a game often used to make decisions. There are two players. Each player secretly chooses either, paper, rock or scissors. At the count of three, each player reveals what they have chosen. The winner of the game is determined this way: paper beats rock (because paper covers rock) rock beats scissors (because rock crushes scissors) scissors beat paper (because scissors cut paper) If the two players choose the same item, it is...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for...

    I need the create a Python code that makes a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game) Please help me see why my "continue" is asking the players to start a new game. Here is my code; user1= input("Whats your name? ") user2= input("And your name? ") a = input("%s, do yo want to choose...

  • Write a program in python that lets the user play the game Rock, Paper, Scissors against...

    Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT