Using Python 3.4 - Create a menu-driven rock, paper, scissors game that a user p
ID: 665970 • Letter: U
Question
Using Python 3.4 - 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.
Explanation / Answer
Here is the first approach :
Step 1 : in order to to generate a random computer choice we need to import the random module
import random;
user choice :
player = input("Enter your choice (rock/paper/scissors): ");
player = player.lower();
while (player != "rock" and player != "paper" and player != "scissors"):
print(player);
Computer choice :
computerInt = random.randint(0,2);
if (computerInt == 0):
computer = "rock";
elif (computerInt == 1):
computer = "paper";
elif (computerInt == 2):
computer = "scissors";
else:
computer = "Huh? Error...";
Winner :
if (player == computer):
print("Draw!");
elif (player == "rock"):
if (computer == "paper"):
print("Computer wins!");
else:
print("You win!");
elif (player == "paper"):
if (computer == "rock"):
print("You win!");
else:
print("Computer wins!")
elif (player == "scissors"):
if (computer == "rock"):
print("Computer wins!");
else:
print("You win!");
Output :
print("Your choice: " + player + " Computer choice: " + computer + " Thank you for playing!");
input("Enter any key to exit.");
Another approach :
import random
def RPS():
print("You are playing Rock Paper Scisscors")
comp_possible = 1,2,3
score = [0,0]
flag = 0
while True:
print("Enter your choice:")
while True:
print("Enter your choice:")
while True:
choice = input('->')
if choice == 'r' or choice == 'R' or choice == 'Rock' or choice == 'rock' or choice == '1':
choice_identifier = 1
break
elif choice == 'S' or choice == 's' or choice == 'Scissors' or choice == 'sciccors' or choice == '2':
choice_identifier = 2
break
elif choice == 'P' or choice == 'p' or choice == 'Paper' or choice == 'paper' or choice == '3':
choice_identifier = 3
break
else:
print('That's not an option in this game :)')
print('Try again:')
continue
comp_choice = random.choice(comp_possible)
if choice_identifier == comp_choice:
print('It's a draw!')
score[0] = score[0] + 1
score[1] = score[1] + 1
elif (choice_identifier == 1 and comp_choice == 2) or (choice_identifier == 2 and comp_choice == 3) or (choice_identifier == 3 and comp_choice == 1):
print('You win!')
score[0] = score[0] + 1
else:
print('You lose...')
score[1] = score[1] + 1
while True:
answer = input('Play another round?')
if answer == 'y' or answer == 'Y' or answer == 'yes' or answer == 'Yes' or answer == 'ye' or answer == 'Ye' or answer == 'sure' or answer == 'Sure':
print(' Current score: You - ',score[0],' Computer - ', score[1])
flag = 0
break
elif answer == 'n' or answer == 'N' or answer == 'no' or answer == 'No' or answer == 'nah' or answer == 'Nah':
print('Thanks for playing! Final score: You - ',score[0],' Computer - ', score[1])
flag = 1
break
else:
print('Yay or nay...')
continue
if flag == 0:
continue
else:
break
choice = " "
while choice.upper() is not "R" and choice.upper() is not "P" and choice.upper() is not "S":
choice = input("Enter your choice - R, P, or S: ")
if choice.upper() is "R":
choice_identifier = 1
elif choice.upper() is "P":
choice_identifier = 2
elif choice.upper() is "S":
choice_identifier = 3
RPS()
Specification :
# Some Useful Names
null = "null"
rock = "rock"
paper = "paper"
scissors = "scissors"
# A Thesaurus (implemented as a dictionary)
synonyms = {"rock": rock,
"paper": paper,
"scissors": scissors,
"stone": rock,
"vellum": paper,
"shears": scissors}
# Final States
game_is_draw = "Game is a Draw"
playerA_wins = "Player A Wins"
playerB_wins = "Player B Wins"
# Initial State
both_players_must_choose = "Both Players Must Choose"
# Transition States
playerA_must_choose = "Player A Must Choose"
playerB_must_choose = "Player B Must Choose"
# Transition Table (implemented as a dictionary)
transitions = {(null, null): both_players_must_choose,
(null, rock): playerA_must_choose,
(null, paper): playerA_must_choose,
(null, scissors): playerA_must_choose,
(rock, null): playerB_must_choose,
(rock, rock): game_is_draw,
(rock, paper): playerB_wins,
(rock, scissors): playerA_wins,
(paper, null): playerB_must_choose,
(paper, rock): playerA_wins,
(paper, paper): game_is_draw,
(paper, scissors): playerB_wins,
(scissors, null): playerB_must_choose,
(scissors, rock): playerB_wins,
(scissors, paper): playerA_wins,
(scissors, scissors): game_is_draw}
# Simulate Initialization
playerA_choice = null
playerB_choice = null
# Simulate Players Choosing
playerA_choice = synonyms["stone"]
playerB_choice = synonyms["shears"]
# Main Logic
state = (playerA_choice, playerB_choice)
print(outcomes[state])
Hope it helps
player = input("That choice is not valid. Enter your choice (rock/paper/scissors): ");
player = player.lower();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.