Strcmp in textboxes of Rock Paper Scissors

3 views (last 30 days)
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

Answers (2)

Guillaume
Guillaume on 4 Mar 2017
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 Comments
Jitti Somsiri
Jitti Somsiri on 4 Mar 2017
How about this, can you please give me an example of comparing values in textboxes using strcmp? Thank you in advantage.
Guillaume
Guillaume on 4 Mar 2017
Edited: Guillaume on 4 Mar 2017
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.

Sign in to comment.


Image Analyst
Image Analyst on 4 Mar 2017
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 Comments
Jitti Somsiri
Jitti Somsiri on 5 Mar 2017
What is player1(1)? Does it mean the first letter of the string of player1?

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!