Strcmp in textboxes of Rock Paper Scissors

3 ビュー (過去 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 Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by