error in creating rock, paper, scissors game

2 次查看(过去 30 天)
%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.

回答(1 个)

Image Analyst
Image Analyst 2021-10-5
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.

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by