error in creating rock, paper, scissors game

조회 수: 2 (최근 30일)
Aidan Palermo
Aidan Palermo 2021년 10월 4일
답변: Image Analyst 2021년 10월 5일
%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