Strcmp in textboxes of Rock Paper Scissors
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (2개)
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
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!