error in creating rock, paper, scissors game
1 view (last 30 days)
Show older comments
%game conditions
user_play = input('chose R for rock, P for paper, S for scissors:');
%randomness
for comp_play = randi(imax,n)
imax = 3;
n = 1;
randi(imax,n)
1 == R;
2 == P;
3 == S;
end
% results
if user_play == R && comp_play == P || user_play == P && comp_play == S || user_play == S && comp_play == R
result = 'Computer win';
elseif comp_play == R && user_play == P || comp_play == P && user_play == S || comp_play == S && user_play == R
result = 'User win';
else comp_play == R & user_play == R | comp_play == P & user_play == P | comp_play == S & user_play == S;
result = 'Tie';
end
I wrote this code a couple days ago and I was able to successful play the game. I tried to code it so that it would rerun until x number of games was won but I couldn't get it to work. I removed that code and now when I try to input a selection for rock paper or scissors this error code pops up.
Error using input
Unrecognized function or variable 'R'.
Error in RPS_simulator (line 3)
user_play == input('chose R for rock, P for paper, S for scissors:');
I then get stuck in a loop of matlab asking me to pick R,P, or S and that error occuring.
0 Comments
Answers (1)
Image Analyst
on 5 Oct 2021
Hints:
choices = {'R', 'P', 'S'}
computersChoice = choices{randi([1 3])}
user_play = input('Choose R for rock, P for paper, or S for scissors : ', 's');
if contains(user_play, 'R', 'IgnoreCase', true)
% User chose Rock.
elseif contains(user_play, 'P', 'IgnoreCase', true)
% User chose Paper.
elseif contains(user_play, 'S', 'IgnoreCase', true)
% User chose Scissors.
end
Make the obvious modifications.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!