Need a GUI for this rock paper scissors code on Matlab. close all clc clear var
ID: 3604457 • Letter: N
Question
Need a GUI for this rock paper scissors code on Matlab.
close all
clc
clear var
player1=input('Player 1 please choose 1 for rock, 2 for paper, or 3 for scissors:');
player2=input('Player 2 please choose 1 for rock, 2 for paper, or 3 for scissors:');
if player1 == 1
if player2 == 1
result = 'Tie!'
elseif player2 == 2
result = 'Player 2 wins!'
else result = 'Player 1 loses!'
end
elseif player1 == 2
if player2 == 1
result = 'Player 1 wins!'
elseif player2 == 2
result = 'Tie!'
else result = 'Player 2 loses!'
end
elseif player1 == 3
if player2 == 1
result = 'Player 2 wins!'
elseif player2 == 2
result = 'Player 1 wins!'
else result = 'Tie!'
end
for k = 1:3
end
end
Explanation / Answer
The GUI code for this rock paper scissors code on Matlab is given below:
code:
disp('+++++++++++++++++++++++++++++++++++++++++++++')
disp('A game of Rock-Paper-Scissors')
%Input player's choise
name=input('Make your move:','s');
%Create a numeric variable for player e.g. if
%player_name=='paper' then player_number=1
if strcmpi(name,'rock')
player_number=2;
elseif strcmpi(name,'paper')
player_number=1;
elseif strcmpi(name,'scissors')
player_number=0;
else
error('myApp:argChk', 'Not a valid name, please choose only one of the three given superpowers.')
end
%Create a random number for the computer
computer_number=randi([0,2],1);
%Depending on the number, create a string value for computer's choise
if computer_number==0
computer_name='Scissors';
elseif computer_number==1
computer_name='Paper';
else
computer_name='Rock';
end
%Compute the difference, make the comparison and find the winner
diff=mod(player_number-computer_number,3);
fprintf('Player chose %s Computer chose %s ',name,computer_name)
if diff==2
disp('Player Wins')
elseif diff==1
disp('Computer Wins')
else
disp('Draw')
end
disp('+++++++++++++++++++++++++++++++++++++++++++++')
Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.