Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a menu-driven rock, paper, scissors game that a user plays against the co

ID: 3575478 • Letter: C

Question

Create a menu-driven rock, paper, scissors game that a user plays against the computer with the ability to save and load a game and its associated play statistics.

Requirements

For menus in the game, the user makes a selection from the menu by entering the number of their choice. If the user enters something other than a number or a number in the range of the choices provided in the menu they are to be given feedback that their choice is not valid and asked for the input again. Under no circumstance should input from the user crash the program. When the program is run the following title, menu, and input prompt are to be displayed: Welcome to Rock, Paper, Scissors! 1. Start New Game 2. Load Game 3. Quit Enter choice:

1. Start New Game

If the user chooses to start a new game the program is to prompt the user for their name using the following prompt: What is your name? After the name is entered the program is to respond with a line that is “Hello” followed by the name of the user, a period, and the phrase “Let’s play!” Example: Hello Justin. Let’s play! After the hello statement is presented to the user, game play is to proceed.

2. Load Game

If the user chooses to load an existing game the program is to prompt the user for their name using the following prompt: What is your name? After the name is entered the program is to attempt to load a game file that is named based on the user name with an extension of .rps. For example, the file name for Justin is to be Justin.rps. If the file is found, the information about the game and statistics are to be loaded, a welcome back message is to be presented to the user, and game play is to proceed. The round number displayed is to be based on the number of rounds previously played. (Note: number of rounds previously played is sum of wins, losses, and ties.) Game play is described below in the Game Play section. The welcome back message is to be “Welcome back “ followed by the user’s name, a period, and the phrase “Let’s Play!” Example: Welcome back Justin. Let’s play! If the file is not found, the user is to be presented with a message indicating the game could not be found and then presented with the startup menu described at the top of the requirements. The message is to be the user name followed by “, your game could not be found.” Example: Justin, your game could not be found.

3. Quit If the user chooses to quit, the program is to exit.

Game Play

For each round a line that includes the round number is to be displayed followed by a menu that let’s the user choose Rock, Paper, or Scissors as their choice for the round as shown here: Round (round number) 1. Rock 2. Paper 3. Scissors What will it be? The user makes their choice and the computer chooses randomly. The result of the round is to be displayed using the following format: You chose . The computer chose . You ! Example: You chose Paper. The computer chose Rock. You win! After the round the user is to be presented with the following prompt and menu: What would you like to do? 1. Play Again 2. View Statistics 3. Quit Enter choice: If the user chooses 1 to play again, the user is to play another round. If the user chooses 2 to view statistics, the current statistics for the game are to be displayed. If the user chooses 3 to quit, the game and associated statistics are to be saved to a file and the program is to exit.

1. Play Again If the user chooses to play again the next round number is to be displayed and the user is to again choose Rock, Paper, or Scissors to play the round as described above. 2. View Statistics If the user chooses to view the statistics the following information is to be displayed: , here are your game play statistics... Wins: Losses: Ties: Win/Loss Ratio: Example: Justin, here are your game play statistics... Wins: 8 Losses: 7 Ties: 2 Win/Loss Ratio: 1.14 After the statistics are displayed the user is to be presented with the menu shown above that is presented after playing a round: What would you like to do? 1. Play Again 2. View Statistics 3. Quit Enter choice:

3. Quit

If the user chooses to quit the game the program is to automatically save the game and associated statistics in a game file that is named based on the user name with an extension of .rps. For example, the file name for Justin is to be Justin.rps. If there is an exception saving the file an error message reporting the error is to be presented to the user. The message is to be “Sorry “ followed by the user’s name and “, the game could not be saved.” Additionally the actual error provided by the Exception object is to be displayed on the next line. Example: Sorry Justin, the game could not be saved. If the file saves successfully the user is to be presented with a message indicating the success of the file save. The message is to be the user’s name followed by “, your game has been saved.” Example: Justin, your game has been saved. Finally, the program is to exit.

RESTART: /Usersjtwyp6/Desktop/TT1040 -RPS Manag []Error: Could not find previous users game data. Rock Paper Scissors [1] Start a New Game Load a Game [3] Display High Scores 41 Exit What would you like to do? If rps.dat does exist Rock Paper Scissors [1] Start a New Game Load a Game [3] Display High Scores 41 Exit What would you like to do? What is your name Johnny Hello Johnny. Let's play! RPS Game Menu [1] Play a Round [2] View Stats [31 Exi What would you like to do? 2 Username: Johnny Wins Ties Losses Age 4s RPS Game Menu Play a Round 21 View Stats 31 Ex What would you like to do? 1 Round 1 Rock 2] Paper 31 Scissors What would you like to do? 3 Rock beats Scissors. You lose RPS Game Menu [1] Play a Round 21 View Stats 31 Exit What would you like to do? 3 Rock Paper Scissors [1] Start a New Game [2] Load a Game [3] Display High Scores 41 Ex What would you like to do? 3 Win% Wins Ties Losses Rank Name Stephanie 0.50 1 0 1 ohn 0.33 ohnny 0.00 Rank N Win% W Losses Stephanie 0.50 0 0.33 2 0.00 0 Rock Paper Scissors Lij Start a New Game [2] Load a Game Display High Scores Rock, Paper, Scissors Game Final Project 41 Exit What would you like to do? 2 What is your name? [!]Error: This User doesn't exist. Rock Paper Soiss [lj Start a New Game [2] Load a Game [3] Display High Scores 4 Exit What would you like to do? What is your name []Error: This User already exists. Rock Paper Scissors [1] Start a New Game (2) Load a G [3] Display High Scores

Explanation / Answer

HI, Try this program...

import random
import pickle

#I'll use class for easy load, easy dump.
class GameStatus():
def __init__(self, name):
self.tie = 0
self.playerWon = 0
self.pcWon = 0
self.name = name

def get_round(self):
return self.tie + self.playerWon + self.pcWon + 1

# Displays program information, starts main play loop
def main():
print "Welcome to a game of Rock, Paper, Scissors!"
print "What would you like to do?"
print ""
game_status = welcomemenu()
while True:
play(game_status)
endGameSelect(game_status)

#prompt user's choice and return GameStatus instance
def welcomemenu():
#changed a logic to handle unexpected user input.
while True:
print "[1]: Start New Game"
print "[2]: Load Game"
print "[3]: Quit"
print ""
menuselect = input("Enter choice: ")
if menuselect in [1, 2, 3]:
break
else:
print "Wrong choice. select again."

if menuselect == 1:
name = raw_input("What is your name?: ") # raw_input for string
print "Hello %s." % name
print "Let's play!"
game_status = GameStatus(name) #make a new game status
elif menuselect == 2:
while True:
name = raw_input("What is your name?: ")
try:
player_file = open('%s.rsp' % name, 'r')
except IOError:
print "There's no saved file with name %s" % name
continue
break
print "Welcome back %s." % name
print "Let's play!"
game_status = pickle.load(player_file)

#load game status
displayScoreBoard(game_status)
player_file.close()
elif menuselect == 3:
print "Bye~!"
exit()
return

return game_status


# displays the menu for user, if input == 4, playGame in the calling function main() is False, terminate the program.
# Generate a random integer 1-3, evaluate the user input with the PC input, update globals accordingly, returning True
# to playGame, resulting in the loop in the calling function main to continue.
def play(game_status):
playerChoice = int(playerMenu())
#this if statement is unnecessary. playerMenu() already checked this.
#if playerChoice == 4:
# return 0
pcChoice = pcGenerate()

outcome = evaluateGame(playerChoice, pcChoice)
updateScoreBoard(outcome, game_status)


# prints the menu, the player selects a menu item, the input is validated, if the input is valid, return the input, if
# the input is not valid, continue to prompt for a valid input......
# 1 - rock
# 2 - paper
# 3 - scissors

def playerMenu():
print "Select a choice: [1]: Rock [2]: Paper [3]: Scissors "
menuSelect = input("What will it be? ")
while not validateInput(menuSelect):
invalidChoice(menuSelect) #I think this function is un necessary. just use print.
menuSelect = input("Enter a correct value: ")
return menuSelect


# if the user doesn't input a 1-3 then return false, resulting in prompting the user for another value.
#If the value is valid, return statement True ..
# takes one argument
# MenuSelection - value is user entered priority
def validateInput(menuSelection):
if menuSelection in [1, 2, 3]: # more readable.
return True
else:
return False


# return a random integer 1-3 to determine pc selection
# 1 - rock
# 2 - paper
# 3 - scissors
def pcGenerate():

#computer choice
pcChoice = random.randint(1,3)
return pcChoice


# evaluate if the winner is computer or player or tie, return value accordingly
# 0 - tie
# 1 - player won
# 2 - computer won
def evaluateGame(playerChoice, pcChoice):

rsp = ['rock', 'paper', 'scissors']
#winning statement
win_statement = ['Rock breaks scissors', 'Paper covers rock', 'Scissors cut paper']
# if player win, win_status = 1 (ex. rock vs scissors -> (1 - 3 == -2) -> (-2 % 3 == 1))
# if pc win, win_status = 2
# if tie, win_status = 0
win_status = (playerChoice - pcChoice) % 3
print "You have chosen %s" % rsp[playerChoice - 1]
what_to_say = "Computer has chose %s" % rsp[pcChoice - 1]
if win_status == 0:
what_to_say += " as Well. TIE!"
elif win_status == 1:
what_to_say += ". %s. You WIN!" % win_statement[playerChoice - 1]
else:
what_to_say += ". %s. You LOSE!" % win_statement[pcChoice - 1]
print what_to_say
return win_status

# Update track of ties, player wins, and computer wins
def updateScoreBoard(outcome, game_status):
# in case of playerWon
if outcome == 0: game_status.tie += 1
elif outcome == 1:
game_status.playerWon += 1
#in case of computer win
else:
game_status.pcWon += 1

# If user input is invalid, let them know.
def invalidChoice(menuSelect):
print menuSelect, "is not a valid option. Please use 1-3"


# Print the scores before terminating the program.
def displayScoreBoard(game_status):
print ""
print "Statistics:"
print "Ties: %d" % game_status.tie
print "Player Wins: %d" % game_status.playerWon
print "Computer Wins: %d" % game_status.pcWon
if game_status.pcWon > 0:
#if you don't use float, '10 / 4' will be '2', not '2.5'.
print "Win/Loss Ratio: %f" % (float(game_status.playerWon) / game_status.pcWon)
else:
print "Win/Loss Ratio: Always Win."
print "Rounds: %d" % game_status.get_round()

def endGameSelect(game_status):
print ""
print "[1]: Play again"
print "[2]: Show Statistics"
print "[3]: Save Game"
print "[4]: Quit"
print ""
while True:
menuselect = input("Enter choice: ")
if menuselect in [1, 2, 3, 4]:
break
else:
print "Wrong input."

if menuselect == 2:
displayScoreBoard(game_status)
endGameSelect(game_status)
elif menuselect == 3:
f = open("%s.rsp" % game_status.name, 'w')
pickle.dump(game_status, f)
f.close()
print "Saved."
endGameSelect(game_status)
elif menuselect == 4:
print "Bye~!"
exit()
main()

Thank You.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote