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

1 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2017-12-2
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

更多回答(1 个)

Image Analyst
Image Analyst 2017-12-2
Use a for loop
numberOfTrials = 15; % Whatever you want...
for k = 1 numberOfTrials
% your existing code.....
end
  1 个评论
Jaime Vera
Jaime Vera 2017-12-2
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.

请先登录,再进行评论。

类别

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