How can I play many rounds in Rock, Paper and Scissors game?

1 view (last 30 days)
Hi,
I have a script to simulate this classic game. The question is: How can I play many rounds? I was thinking that was using for loop, but I could not find the correct way to accomplish that.
Here the script without repetition:
disp('Stone, Paper and Scissors')
disp('Stone (1)')
disp('Paper (2)')
disp('Scissors (3)')
n=input('Which choice?: ')
pc=[1,2,3];
switch(n)
case 1
MATChoice=datasample(pc,1)
if c1==1
return
end
if MATChoice==2
display('MATLAB win; Player LOSE')
return
end
if MATChoice==3
display('MATLAB lose; Player WIN')
return
end
case 2
MATChoice=datasample(pc,1)
if MATChoice==1
disp('MATLAB lose; Player WIN')
return
end
if MATChoice==2
return
end
if MATChoice==3
disp('MATLAB win; Player LOSE')
return
end
case 3
MATChoice=datasample(pc,1)
if MATChoice==1
disp('MATLAB win; Player LOSE')
return
end
if MATChoice==2
disp('MATLAB lose; Player WIN')
return
end
if MATChoice==3
return
end
otherwise
error('Choose 1,2 or 3')
return
end
return
end
Could you help me, please?
Thank you.

Accepted Answer

Image Analyst
Image Analyst on 2 Dec 2017
Try this:
numberOfTrials = 30; % Whatever you want...
for k = 1 : numberOfTrials
choices = {'Rock', 'Paper', 'Scissors', 'Quit'};
userChoice = menu('Which choice?: ', choices)
if userChoice == 4
break;
end
computerChoice =randi([1,3])
message = sprintf('You chose %s and computer chose %s', ...
choices{userChoice}, choices{computerChoice})
switch userChoice
case 1
switch computerChoice
case 1
message = sprintf('%s\nTie', message);
case 2
message = sprintf('%s\nComputer wins.', message);
case 3
message = sprintf('%s\nYou win', message);
end
case 2
switch computerChoice
case 1
message = sprintf('%s\nYou win!', message);
case 2
message = sprintf('%s\nTie', message);
case 3
message = sprintf('%s\nComputer wins', message);
end
case 3
switch computerChoice
case 1
message = sprintf('%s\nComputer wins.', message);
case 2
message = sprintf('%s\nYou win!', message);
case 3
message = sprintf('%s\nTie', message);
end
case 4
break;
end
uiwait(helpdlg(message));
end

More Answers (1)

Image Analyst
Image Analyst on 2 Dec 2017
Use a for loop
numberOfTrials = 15; % Whatever you want...
for k = 1 numberOfTrials
% your existing code.....
end
  1 Comment
Jaime Vera
Jaime Vera on 2 Dec 2017
I thought that too, but when I do that, the script does not ask me again, after one round, about the n option: 1, 2 or 3.
Do you know why is this happening?
Thank you.

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!