Starting from:

$15

Lab 7 – Rock, Paper, Scissors Solution

Goals
● Write a program that utilizes functions to simulate a game of rock-paper-scissors
● Continue practicing with loops
● Continue practicing with variable declaration and assignment
Setup
● Create a new .py file in your desired directory, and rename the file
● When you name the code, use the following naming convention
ITP115_l#_lastname_firstname (replace # with this lab number)
● Your new file must begin with comments in the following format (replace the name and email with your actual information):
# Name
# ITP 115, Spring 2017
# Lab practical L^ (replace ^ with this lab number)
# USC email
Description/Rules of Program
● Write a program that allows the user to play Rock, Paper, Scissors against the computer.
● When the program begins, you randomly choose a number from 0 to 2, which will represent the computer’s choice with 0 for rock, 1 for paper, or 2 for scissors.
● The user then enters his/her choice of 0 for rock, 1 for paper, or 2 for scissors.
● A winner is selected based on the following rules:
o Rock smashes scissors (If one player chooses rock and the other chooses
scissors, then the player who chooses rock wins).
o Scissors cut paper (If one player chooses scissors and the other chooses paper, then the player who chooses scissors wins).
o Paper covers rock (If one player chooses paper and the other chooses rock, then the player who chooses paper wins).
o If both players make the same choice, then it is a tie. ITP 115 – Programming in Python p. 2 of 4
● The game continues as long as the player wants to continue.
● When the player decides to exit the program, display the score results (how many times the player won and how many times the computer won).

Requirements
● main()
○ Input: none
○ Output: none
○ Create a while loop that runs as long as the user wants to continue the game
○ In the loop, you should display the menu, get the computer’s choice, get the player’s choice, and play a round (see who won)
○ Since playRound will return the result of who won the game, you will also
need to keep track of the score
■ This means keeping a counter for how many times the computer won, a counter for how many times the player won, and a counter for how many times they tied
■ These counters should be local variables to main and should be changed based on the return value (output) of playRound
○ Call continueGame to ask the user if they want to continue, and use their response to control the while loop
○ When the user exits, display all the final results (i.e. number of ties, number of
player wins, and number of computer wins)
● In addition to main, your program should have the following functions
○ displayMenu()
■ Input: none
■ Output: none
■ displays the game rules to the user
○ getComputerChoice()
■ Input: none
■ Output: integer that is randomly chosen, a number between 0 to 2
○ getPlayerChoice()
■ Input: none
■ Output: integer represents the choice
■ Asks the user for their choice: 0 for rock, 1 for paper, or 2 for scissors.
○ playRound(computerChoice, playerChoice)
ITP 115 – Programming in Python p. 3 of 4
■ Input: two integers—one representing the computer’s choice (0, 1, or
2) and the other representing the player’s choice (0, 1, or 2)
■ Output: integer
● return -1 if the computer won the round
● return 1 if the player won the round
● return 0 if there was a tie
■ This method contains the game logic so it simulates the game and determines a winner. Use the logic described above to see who should win a round
○ continueGame()
■ Input: none
■ Output: boolean
■ Ask the user if they want to continue (Y/N), and then return True or False accordingly
■ Note: This function must return True/False as a boolean, not as a
string
Sample Output
Welcome! Let's play rock, paper, scissors.
The rules of the game are:
Rock smashes scissors
Scissors cut paper
Paper covers rock
If both the choices are the same, it's a tie
Please choose (0) for rock, (1) for paper or (2) for scissors
You chose Rock.
The computer chose Paper.
Paper covers rock. Computer wins!
Do you want to continue playing? Enter (y) for yes or (n) for no.
y
Welcome! Let's play rock, paper, scissors.
The rules of the game are:
Rock smashes scissors
Scissors cut paper
Paper covers rock
ITP 115 – Programming in Python p. 4 of 4
If both the choices are the same, it's a tie
Please choose (0) for rock, (1) for paper or (2) for scissors
1
You chose Paper.
The computer chose Scissors.
Scissors cut paper. Computer wins!
Do you want to continue playing? Enter (y) for yes or (n) for no.
n
You won 0 game(s).
The computer won 2 game(s).
You tied with the computer 0 time(s).
Thanks for playing!
Deliverables and Submission Instructions
● Create a folder on your computer called
ITP115_a#_lastname_firstname
(replace # with this lab number)
● Inside the folder, include your python source code
● Compress the folder (make a zip file) called
ITP115_a#_lastname_firstname.zip
(replace # with this assignment number)
● Upload zip file to Blackboard site for our course

More products