Question

In: Computer Science

Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...

Rock, Paper, Scissors Game

Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer.

The program should work as follows:

You can set these constant global variables at the top outside of your main function definition:

COMPUTER_WINS = 1

PLAYER_WINS = 2

TIE = 0

INVALID = 3

ROCK = 1

PAPER = 2

SCISSORS = 3

For this program 1 represents rock, 2 represents paper, and 3 represents scissors.

In your main function:

- Ask the user to choose rock, paper, or scissors like so: 'Enter 1 for rock, 2 for paper, 3 for scissors: ' set the value to a variable called 'player'.

- Generate a random number between 1 and 3 for the computer. Set that value to a variable called 'computer'. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.)

- Call your choiceString(choice) function to print the computer's hand like so:

'Computer chose ....'

- Call your choiceString(choice) function to print the player's hand like so:

'Player chose ....'

- Call your value returning function rockPaperScissors(computer, player) to get the result of the round between the computer and player.

- If the result is tie, print 'You made the same choice as the computer. Starting over' and repeat the process until there is no tie.

- Declare if the computer is the winner with 'The computer wins the game' or whether the player is the winner with 'You win the game' or if there was an invalid choice print 'You made an invalid choice. No winner'. If they made an invalid choice you do not need to loop through again.

In the main, please comment each of the above bullet points (5 points)

Outside of your main, define the following functions:

rockPaperScissors(computer, player):

[value returning function]

This takes in integers representing the computer and player's 'hand' so to speak of rock, paper, or scissors as arguments.

It returns one of the following values:

TIE

PLAYER_WINS

COMPUTER_WINS

INVALID

Using conditionals, based on the arguments passed into the function, determine which value to return based on these rules:

If one player chooses rock and the other player chooses scissors, then rock wins.

(Rock smashes scissors.)

If one player chooses scissors and the other player chooses paper, then scissors wins.

(Scissors cuts paper.)

If one player chooses paper and the other player chooses rock, then paper wins. (Paper

wraps rock.)

If both players make the same choice, the game must be played again to determine the

Winner.

choiceString(choice)

Value returning function that takes in the argument choice (the number representing rock, paper, or scissors) and return one of the following strings:

'rock'

'paper'

'scissors'

'Something went wrong'

In other words, the function returns the string value of the integer being passed in as an argument.

Solutions

Expert Solution

import random
COMPUTER_WINS = 1
PLAYER_WINS = 2
TIE = 0
INVALID = 3
ROCK = 1
PAPER = 2
SCISSORS = 3
def choiceString(choice):
if choice==1:#set the string based on choice
computer="rock"
elif choice==2:
computer="paper"
elif choice==3:
computer="scissors"
else:
return "Something went wrong"
return computer#return rock,paper, or scissor
def rockPaperScissors(computer, player):
if player==computer:
return "TIE"
elif computer=="rock" and player=="paper":#return the result based on conditions provided
return "PLAYER_WINS"
elif computer=="rock" and player=="scissors":
return "COMPUTER_WINS"
elif computer=="paper" and player=="rock":
return "COMPUTER_WINS"
elif computer=="paper" and player=="scissors":
return "PLAYER_WINS"
elif computer=="scissors" and player=="rock":
return "PLAYER_WINS"
elif computer=="scissors" and player=="paper":
return "COMPUTER_WINS"
else:
return "INVALID"
while True:
player=int(input("Enter 1 for rock, 2 for paper, 3 for scissors: "))
computer=random.randint(1,3)#generate radom number 1,2,3
  
print("Computer choose ",choiceString(computer))#call function and print the choice of computer
computer=choiceString(computer)
print("Player choose ",choiceString(player))#call function and print the choice of player
player=choiceString(player)
res=rockPaperScissors(computer, player)#call function to get the result
if res=="TIE":#print the result based on returned value and continue game if it is a tie
print("You made the same choice as the computer. Starting over")
continue
elif res=="COMPUTER_WINS":
print("The computer wins the game")
break
elif res=="PLAYER_WINS":
print("You win the game")
break
elif res=="INVALID":
print("You made an invalid choice. No winner")

Screenshots:

The screenshots are attached below for reference.

Please follow them for output and proper indentation.

Please upvote my answer. Thank you.


Related Solutions

Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play the game of rock, paper, scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the...
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions. Requirements: 1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player....
Two players play a rock-paper-scissors game. The losing player will give $1 to the winning player. If it is a draw, no payment is made. The payoff to a player is the amount of money (s)he gets. Represent the situation in a matrix form. Find all the Nash equilibria.
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT