Rock, Paper, Scissors in MATLAB?

조회 수: 23 (최근 30일)
Brian Tiffman
Brian Tiffman 2015년 11월 12일
댓글: Brian Tiffman 2015년 11월 13일
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!

채택된 답변

Image Analyst
Image Analyst 2015년 11월 12일
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
Image Analyst
Image Analyst 2015년 11월 12일
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 2015년 11월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by