Reputation: 9
I would like to compare strings in textboxs of rock, paper and scissors game. After comparing, display the result in another textbox. But it does not compare. Can someone please help me?
if strcmp('Rock'==get(handles.computer,'string'),'Rock'==get(handles.player,'string'))
set(handles.result,'string','Draw');
elseif strcmp('Rock'==get(handles.computer,'string'),'Paper'==get(handles.player,'string'))
set(handles.result,'string','Player wins');
elseif strcmp('Rock'==get(handles.computer,'string'),'Scissors'==get(handles.player,'string'))
set(handles.result,'string','Computer wins');
elseif strcmp('Paper'==get(handles.computer,'string'),'Rock'==get(handles.player,'string'))
set(handles.result,'string','Computer wins');
elseif strcmp('Paper'==get(handles.computer,'string'),'Paper'==get(handles.player,'string'))
set(handles.result,'string','Draw');
elseif strcmp('Paper'==get(handles.computer,'string'),'Scissors'==get(handles.player,'string'))
set(handles.result,'string','Player wins');
elseif strcmp('Scissors'==get(handles.computer,'string'),'Rock'==get(handles.player,'string'))
set(handles.result,'string','Player wins');
elseif strcmp('Scissors'==get(handles.computer,'string'),'Paper'==get(handles.player,'string'))
set(handles.result,'string','Computer wins');
elseif strcmp('Scissors'==get(handles.computer,'string'),'Scissors'==get(handles.player,'string'))
set(handles.result,'string','Draw');
end
Upvotes: 0
Views: 163
Reputation: 4191
As an alternative to @il_raffa, I would suggest mapping rock
, scissor
, paper
to [1,2,3]
, respectively. Then subtract the value of computer
from the player
.
Then you can do if, else if, else
easily.
if ismember(player - computer, [-1 2]) %This mean player got a
'player wins'
elseif ismember(player - computer, [1 -2])
'computer wins'
else
'draw'
end
When there are only 9 conditions, switch, case
may not be a big deal but if the number of conditions was higher, such a technique would make the work easier.
Upvotes: 0
Reputation: 5190
The problem is in the way you compare the string of the two textboxes:
if strcmp('Rock'==get(handles.computer,'string'),'Rock'==get(handles.player,'string'))
In this statement:
'Rock'==get(handles.computer,'string')
you compare element by element the characters of the two strings.
This comparison returns an array of type logical
(1
if the i-th character of the first string is equal to the i-th character of the second string, 0
otherwise)
Notice that if the two strings are of different length, you will get an error such as Matrix dimensions must agree.
The same applies to the second part of the statement:
'Rock'==get(handles.player,'string')
In case both the computer and the player insert the same string (e. g. Rock), you are then using strcmp
to compare two logical
arrays and, it fails.
cases
You have, therefore, to directly compare the strings.
A possible solution could be to use a switch
statement:
lower case
(Rock and rock are ... the same)cases
of the switch
holds the differnt combinationsA possible implementation could be:
% Get the Computer string (in lower case)
computer=lower(get(handles.computer,'string'))
% Get the Player string (in lower case)
player=lower(get(handles.player,'string'))
% Concatenate the two string
res=[computer player]
% Check for the different combination
switch(res)
case 'rockrock'
set(handles.result,'string','Draw');
case 'rockpaper'
set(handles.result,'string','Player wins');
case 'rockscissor'
set(handles.result,'string','Computer wins');
case 'paperrock'
set(handles.result,'string','Computer wins');
case 'paperpaper'
set(handles.result,'string','Draw');
case 'paperscissor'
set(handles.result,'string','Player wins');
case 'scissorrock'
set(handles.result,'string','Player wins');
case 'scissorpaper'
set(handles.result,'string','Computer wins');
case 'scissorscissor'
set(handles.result,'string','Draw');
otherwise
set(handles.result,'string','Invalid text');
end
Hope this helps,
Qapla'
Upvotes: 2