Task 1: Rock, Paper, Scissors (Topics: Variables, user-input, flow-control, function) Task 1 implements the "rock, paper, scissors" game. This is a two-player game, where each player simultaneously selects from one of three options: rock, paper, scissors.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 27PE
icon
Related questions
Question
Task 1: Rock, Paper, Scissors (Topics: Variables, user-input, flow-control, function)
Task 1 implements the "rock, paper, scissors" game. This is a two-player game, where
each player simultaneously selects from one of three options: rock, paper, scissors.
Scissors
beats paper
The game is typically played with hand gestures and has the following rules:
Rock wins against scissors, loses to paper, ties with rock.
Paper wins against rock, loses to scissors, ties with paper.
Scissors win against paper, lose to rock, tie with scissors.
In a real game, both players would make their selection simultaneously.
In our version, the user will select first, and then the computer opponent named "HAL" will make a random selection.
Image credit: Enzoklap (CC)
Require function
In addition to the task1() function, you need to implement the following function:
printOutcome (params: userSelection, computerSelection) -> no return
The parameters are the user's and computer's selection. The function prints the outcomes depending on the selections:
(1) "You win!"; (2) "HAL wins!"; (3) "A tie!"
You decide what data type the parameters should be (e.g., integers representing the selection or strings representing
"rock", "paper", "scissors"). Just ensure your function prints the correct outcome.
Task 1 should behave as follows:
(1) Print "Rock, Paper, Scissors!"
(2) Print "Make your selection..."
(3) Get input from user – 1 (rock), 2 (paper), 3 (scissors).
(4) If the input is not 1, 2, or 3, print "Invalid selection. Try again." and ask the user for input again.
(5) Randomly select HAL's choice – rock, paper, scissors.
(6) Print out the user's selection and HAL's selection.
(7) Pass the user's and HAL's selections to the function printOutcome() to print the outcome of the game.
(8) Ask the user if they want to play again (Y). If the input is 'Y' or 'y', go back to (2).
Example of task 1 running (user input in red) – see video link for more examples.
Task1: Rock, Paper, Scissors
-------
Rock, Paper, Scissors!
Make your selection. ..
(1) rock, (2) paper, (3) scissors? 1
You: rock
HAL: rock
A tie!
Play again (Y/N)? y
Make your selection.
(1) rock, (2) paper, (3) scissors? 2
You: paper
HAL: scissors
HAL wins!
Play again (Y/N)? y
Make your selection.
(1) rock, (2) paper, (3) scissors? 44
Invalid selection. Try again.
Make your selection.
(1) rock, (2) paper, (3) scissors? 3
You: scissors
You can assume the input will be an integer.
Check if it is 1, 2 or 3. If not, inform user of
invalid selection as shown.
..
HAL: раper
You win!
Play again (Y/N)? n
Paper
Rock
bets socsone
Transcribed Image Text:Task 1: Rock, Paper, Scissors (Topics: Variables, user-input, flow-control, function) Task 1 implements the "rock, paper, scissors" game. This is a two-player game, where each player simultaneously selects from one of three options: rock, paper, scissors. Scissors beats paper The game is typically played with hand gestures and has the following rules: Rock wins against scissors, loses to paper, ties with rock. Paper wins against rock, loses to scissors, ties with paper. Scissors win against paper, lose to rock, tie with scissors. In a real game, both players would make their selection simultaneously. In our version, the user will select first, and then the computer opponent named "HAL" will make a random selection. Image credit: Enzoklap (CC) Require function In addition to the task1() function, you need to implement the following function: printOutcome (params: userSelection, computerSelection) -> no return The parameters are the user's and computer's selection. The function prints the outcomes depending on the selections: (1) "You win!"; (2) "HAL wins!"; (3) "A tie!" You decide what data type the parameters should be (e.g., integers representing the selection or strings representing "rock", "paper", "scissors"). Just ensure your function prints the correct outcome. Task 1 should behave as follows: (1) Print "Rock, Paper, Scissors!" (2) Print "Make your selection..." (3) Get input from user – 1 (rock), 2 (paper), 3 (scissors). (4) If the input is not 1, 2, or 3, print "Invalid selection. Try again." and ask the user for input again. (5) Randomly select HAL's choice – rock, paper, scissors. (6) Print out the user's selection and HAL's selection. (7) Pass the user's and HAL's selections to the function printOutcome() to print the outcome of the game. (8) Ask the user if they want to play again (Y). If the input is 'Y' or 'y', go back to (2). Example of task 1 running (user input in red) – see video link for more examples. Task1: Rock, Paper, Scissors ------- Rock, Paper, Scissors! Make your selection. .. (1) rock, (2) paper, (3) scissors? 1 You: rock HAL: rock A tie! Play again (Y/N)? y Make your selection. (1) rock, (2) paper, (3) scissors? 2 You: paper HAL: scissors HAL wins! Play again (Y/N)? y Make your selection. (1) rock, (2) paper, (3) scissors? 44 Invalid selection. Try again. Make your selection. (1) rock, (2) paper, (3) scissors? 3 You: scissors You can assume the input will be an integer. Check if it is 1, 2 or 3. If not, inform user of invalid selection as shown. .. HAL: раper You win! Play again (Y/N)? n Paper Rock bets socsone
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage