Strcmp in textboxes of Rock Paper Scissors

3 visualizaciones (últimos 30 días)
Jitti Somsiri
Jitti Somsiri el 4 de Mzo. de 2017
Comentada: Image Analyst el 5 de Mzo. de 2017
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

Respuestas (2)

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

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 4 de Mzo. de 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 comentarios
Jitti Somsiri
Jitti Somsiri el 5 de Mzo. de 2017
What is player1(1)? Does it mean the first letter of the string of player1?
Image Analyst
Image Analyst el 5 de Mzo. de 2017
Exactly. Yes it does.

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by