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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Dez. 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

Weitere Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by