Rock, Paper, Scissors in MATLAB?

23 vues (au cours des 30 derniers jours)
Brian Tiffman
Brian Tiffman le 12 Nov 2015
Commenté : Brian Tiffman le 13 Nov 2015
In the attached document, looking at problem 4, how would I write the code using a matrix and while loop. I know how to write the code using many if statements, but I want some insight on how to use the matrix I created in a while loop. Im just looking for help, and not the answer given to me for my problem. Any help would be greatly appreciated. Thanks!

Réponse acceptée

Image Analyst
Image Analyst le 12 Nov 2015
Here's a hint/snippet
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
  3 commentaires
Image Analyst
Image Analyst le 12 Nov 2015
Your code has to get the computer and your choice. Then use those as rows and columns into the matrix to decide if you won, the computer won, or it was a tie. Then increment the wins and losses and ties for you and the computer.
Brian Tiffman
Brian Tiffman le 13 Nov 2015
I went ahead and did it without using the matrix way, and I still need to include what you gave me earlier,
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
but could you show me a little more of how to incorporate my matrix into this. My matrix is coded like this
a=[3 1 2 2 1;2 3 1 1 2;1 2 3 2 1;1 2 1 3 2;2 1 2 1 3]
Im not looking for someone to give me the answer, Im just stuck. I know you tried explaining it, but im still confused on incorporating the matrix, but everything else you said has made sense. I feel like this code could be much shorter.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by