In: Computer Science
Rock-Paper-Scissors
Implement Rock-Paper-Scissors such that the user can play the computer in a best-of series! The user inputs the number of rounds which should be positive and odd. This is a free form assignment but structure your code similar to the test cases provided. USING MATLAB ONLY!!!
Program Inputs
• How many rounds would you like to play (odd rounds only)?:
XXX
– XXX should be positive and odd. Restart the game if the input is not positive or odd.
Program Outputs
• XXX wins this round
– Replace XXX with either User or Computer depending on who wins the round • Game Over!
– After the Game is over display the times User and Computer won after the total number of rounds. Finally display who wins the entire game.
Sample Outputs:
Test Case 1:
Welcome to Rock-Paper-Scissors! Play if you dare! How many rounds would you like to play (odd rounds only)? 3 You choose to do best out of 3 rounds ********************************************************* Round 1 ********************************************************* Rock (0)... Paper (1)... Scissors (2)... Shoot: 1 The computer chooses Rock and you picked Paper User wins this round! *********************************************************
********************************************************* Round 2 ********************************************************* Rock (0)...
Paper (1)... Scissors (2)... Shoot: 2 The computer chooses Rock and you picked Scissors Computer wins this round! *********************************************************
********************************************************* Round 3 ********************************************************* Rock (0)...
Paper (1)... Scissors (2)... Shoot: 1 The computer chooses Scissors and you picked Paper Computer wins this round! *********************************************************
********************************************************* Game over! User won 1 out of 3 rounds Computer won 2 out of 3 rounds
Computer wins! Did you expect anything else? *********************************************************
Test Case 2:
Welcome to Rock-Paper-Scissors! Play if you dare! How many rounds would you like to play (odd rounds only)? 3 You choose to do best out of 3 rounds ********************************************************* Round 1 ********************************************************* Rock (0)... Paper (1)... Scissors (2)... Shoot: 5 How dare you cheat at rock paper scissors! You lose this round! You muggle! *********************************************************
********************************************************* Round 2 ********************************************************* Rock (0)...
Paper (1)... Scissors (2)... Shoot: 7 How dare you cheat at rock paper scissors! You lose this round! You muggle! *********************************************************
********************************************************* Game over! User won 0 out of 3 rounds Computer won 2 out of 3 rounds
Computer wins! Did you expect anything else? *********************************************************
Test Case 3:
Welcome to Rock-Paper-Scissors! Play if you dare! How many rounds would you like to play (odd rounds only)? 1 You choose to do best out of 1 rounds ********************************************************* Round 1 ********************************************************* Rock (0)... Paper (1)... Scissors (2)... Shoot: 5 How dare you cheat at rock paper scissors!
You lose this round! You muggle! *********************************************************
********************************************************* Game over! User won 0 out of 1 rounds Computer won 1 out of 1 rounds
Computer wins! Did you expect anything else? *********************************************************
Test Case 4:
Welcome to Rock-Paper-Scissors! Play if you dare! How many rounds would you like to play (odd rounds only)? 2 2 is not a valid number of rounds... Try again! *********************************************************
How many rounds would you like to play (odd rounds only)? 1 You choose to do best out of 1 rounds ********************************************************* Round 1 ********************************************************* Rock (0)...
Paper (1)... Scissors (2)... Shoot: 1 The computer chooses Paper and you picked Paper This round is a tie! Replaying this round... *********************************************************
********************************************************* Round 1 ********************************************************* Rock (0)...
Paper (1)... Scissors (2)... Shoot: 1 The computer chooses Scissors and you picked Paper Computer wins this round! *********************************************************
********************************************************* Game over! User won 0 out of 1 rounds Computer won 1 out of 1 rounds
Computer wins! Did you expect anything else? *********************************************************
Following code snippet can be used
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));