How to count how many games are won?
2 views (last 30 days)
Show older comments
For this rock, paper, scissors code I need to count how many games are won by the computer, player, and end in a tie. Would I use the count command?
disp('A game of Rock-Paper-Scissors-Lizard-Spock')
count=0
count1=0
count2=0
%Input player's choice
name=input('Make your move:','s');
%Create a numeric variable for player e.g. if
%player_name=='paper' then player_number=1
if strcmpi(name,'rock')
player_number=2;
elseif strcmpi(name,'paper')
player_number=1;
elseif strcmpi(name,'scissors')
player_number=0;
elseif strcmpi(name, 'Lizard')
player_number=3
elseif strcmpi(name, 'Spock')
player_number=4
else
error('myApp:argChk', 'Not a valid name, please choose only one of the three given superpowers.')
end
%Create a random number for the computer
computer_number=randi([0,4],1);
%Depending on the number, create a string value for computer's choise
if computer_number==0
computer_name='Scissors';
elseif computer_number==1
computer_name='Paper';
elseif computer_number==3
computer_name='Lizard'
elseif computer_number==4
computer_name='Spock'
else
computer_name='Rock';
end
%Compute the difference, make the comparison and find the winner
diff=mod(player_number-computer_number,5);
fprintf('Player chose %s \n Computer chose %s \n',name,computer_name)
if diff==2
disp('Player Wins')
count=count+1
elseif diff==1
disp('Computer Wins')
ccount1=count1+1
elseif diff==3
disp('Player Wins')
count=count+1
elseif diff==4
disp('Player Wins')
count=count+1
else
disp('Draw')
count2=count2+1
end
0 Comments
Answers (1)
Image Analyst
on 14 Nov 2015
Yes, you would keep 3 separate counts. But not 4. Why are you initializing count1 to zero, but not incrementing it, and instead assigning ccount1??? I strongly suspect you mean count1 instead of ccount1.
5 Comments
Image Analyst
on 14 Nov 2015
Edited: Image Analyst
on 14 Nov 2015
It should continue with the while. Here it is again:
gamesPlayed = 0
while gamesPlayed < 100 % Or whatever max you reasonably expect
% Other Code: menu(), randi(), etc.
% Increment the number of games played
gamesPlayed = gamesPlayed + 1;
promptMessage = sprintf('Do you want to Continue playing,\nor Quit?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(button, 'Quit')
break;
end
end
As you can see, if you click continue it does not break out of the loop and so it continues with the loop. I can't say why nothing happens unless you show your code.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!