Strcmp in textboxes of Rock Paper Scissors

조회 수: 2 (최근 30일)
Jitti Somsiri
Jitti Somsiri 2017년 3월 4일
댓글: Image Analyst 2017년 3월 5일
Hi, I would like to compare strings in textboxes of rock, paper and scissors game. After comparing, display the result in another textbox. Here is the code. I don't know what to do next. Can someone please help me? Thank you.
if strcmp(get(handles.edit1,'string'), get(handles.edit4,'string'))
end
  댓글 수: 1
Guillaume
Guillaume 2017년 3월 4일
You haven't shown any code (of any value).

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2017년 3월 4일
The whole purpose of the homework is to make you think about what the algorithm should be. Delegating that task to others is not going to help you learn.
You clearly haven't thought enough about what's involved in a game of rock/paper/scissors. If you had you'd realised that testing if the strings are the same is not enough. With that little snippet you've given the only thing that can be added is:
if strcmp(get(handles.edit1,'string'), get(handles.edit4,'string'))
disp('stalemate');
else
disp('somebody won. Don't know who');
end
Side note: I would encourage you to rename your controls. Player1 and Player2 would be much better than edit1, edit2.
  댓글 수: 2
Jitti Somsiri
Jitti Somsiri 2017년 3월 4일
How about this, can you please give me an example of comparing values in textboxes using strcmp? Thank you in advantage.
Guillaume
Guillaume 2017년 3월 4일
편집: Guillaume 2017년 3월 4일
I gave you an example of comparing "values in textboxes using strcmp".
The code I wrote prints 'stalemate' if the values are the same and state that it can't solve it otherwise since comparing the two values for equality is not the way to go.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2017년 3월 4일
Hint:
player1 = lower(strtrim(handles.edit1.String)); % Strip white space and convert to lower case.
player2 = lower(strtrim(handles.edit4.String));
Compare first letters:
if player1(1) == 'r' && player2(1) == 'r'
% Draw
elseif player1(1) == 'r' && player2(1) == 'p'
% Player2 wins.
elseif player1(1) == 'r' && player2(1) == 's'
% Player1 wins.
and so on. There will be 9 possibilities. This is one way anyway. You can use strcmpi() if you want to compare the whole word instead of the first letters, but then you should put in some code to handle the case where the person puts in a wrong word, like 'rabbit', 'Shoe', or 'game'.
  댓글 수: 6
Jitti Somsiri
Jitti Somsiri 2017년 3월 5일
What is player1(1)? Does it mean the first letter of the string of player1?
Image Analyst
Image Analyst 2017년 3월 5일
Exactly. Yes it does.

댓글을 달려면 로그인하십시오.

카테고리

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