How to count how many games are won?

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

Risposte (1)

Image Analyst
Image Analyst il 14 Nov 2015

0 voti

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 Commenti

And why not use descriptive names like numWinsPlayer, numWinsComputer, and numTies?
Yes, I did mean count1 sorry. I guess I should use more descriptive names, thanks. One thing I cant figure out is how to keep the game going, instead of ending right after 1 round.
Image Analyst
Image Analyst il 14 Nov 2015
Modificato: Image Analyst il 14 Nov 2015
I think I told you that in your duplicate question. Use a while loop and have a questdlg() at the bottom of it. If your original question was answered, you can click "Accept this Answer" to mark it as answered.
Yea, I accepted your answer because you helped me out, and i appreciate that. I get the contine or quit menu to pop up, but when I hit continue nothing happens.
Image Analyst
Image Analyst il 14 Nov 2015
Modificato: Image Analyst il 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.

Accedi per commentare.

Categorie

Scopri di più su Just for fun in Centro assistenza e File Exchange

Richiesto:

il 14 Nov 2015

Modificato:

il 14 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by