Create a Rock, Paper, Scissors game in Python named rps.py according to the requ
ID: 3576178 • Letter: C
Question
Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in this document. Description: 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:
User Class:
Write a class named user and save it as a file named User.py. The user class should have the following data attributes:
__username To store the name of the player
__wins To store the number of wins
__ties To store the number of ties
__losses To store the number of losses
__created To store when the user account was created
The User class should also have the following methods:
__init__ This method takes 4 parameters, the username, wins, ties, and losses (when initializing the last 3 will be zeroes eg. (John,0,0,0)) It should create __username, __wins, __ties, and __losses based on those parameters and it should create __created based on the current time.
get_username Returns the value of __username field
get_user_wins Return the value of the __wins field
set_user_wins Takes in 1 parameter and sets __wins to that parameter
get_user_ties Returns the value of the __ties field
set_user_ties Takes in 1 parameters and sets __ties to that parameter
get_user_losses Returns the value of the __losses field
set_user_losses Takes in 1 parameter and sets __losses to that parameter
get_round Returns the current round number (not the next) HINT: return (wins + ties + losses)
get_age This method calculates the difference between the current time and the value of the __age field. It should return a string based on the age of the user. For example, this method should return the following: 30s if the user was created 30 seconds ago 15m if the user was created 15 minutes ago 1h if the created was posted 1 hour, 10 minutes, and 42 seconds ago
display_user Displays the username and stats (check sample output for formatting)
Users Class:
Write a class named users and save it as a file named Users.py. The users class should have the following data attributes:
__user_list Is a list that will store each user
__rps_file_name To store the name of the file in which we read and write to. “rps.dat” so simply self.__rps_file_name = “rps.dat”
The Users class should also have the following methods:
def load_users will attempt to use pickle to read the list of users into user_list from the file “rps.dat”
def save_users will attempt to use pickle to write the list of users, user_list, to the file “rps.dat”
##CRUD OPERATIONS##
#create, read, update#
def create_user Takes in a parameter, username, and checks to see if it exists. If it exists we return False, if it doesn’t exist, we add it to the list setting the wins, ties, losses to 0 and return True. If the name exists, we print the error: [!]Error: This User already exists.
def read_user Takes in a parameter, username, and checks to see if it exists. If it exists, we return the True AND the user object which has the same name. If it doesn’t exist, we return False and the user object. At the beginning of the function set found_user = None. We will return True, found_user or return False, found_user. To check if a user exists, we will compare each user name in the list (for user in self.__user_list:) to username. So compare each user.get_name() to username If the name doesn’t exist, we print the error: [!]Error: This User doesn't exist.
def update_user Takes in a parameter, a user, We find the user in the list and update users wins, losses, and ties using the accessor methods. We will get the current loaded users stats and update the user that is saved in the list to these new stats Eg. user.set_user_wins(current_user.get_user_wins) If the name doesn’t exist, we print the error: [!]Error: This User doesn't exist.
def display_highscore Takes in no parameters. Uses the list of users and creates a ranking of the Win percentage based on the wins divided by the rounds. It ranks everyone on the list from 1-N, N being the length of the list. Don’t worry about ranking ties in the highscore. Eg. if 2 people have a .50 Win percentage either can be first HINT: This is one way to do this Start with making separate duplicate list temp_list = list(self.__user_list) With this temp_list we can use temp_list.remove(user) to remove a user. Think about the when we did max and first_pass… cur_max_user = user. cur_max_ratio = (user.get_user_wins/user.get_round()) If the next users ratio is greater than the max..make it the new user and the ratio..then at the end delete it..do this until the temp_list length is 0
RPS.py
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 outside 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.
HINT: To help clean up code and prevent duplicate code making these functions will help.
1. It would be easy to have 3 functions to display each menu.
2. It would also be easy to have a function that takes in the range of numbers for a menu, makes sure they are valid, and returns a choice.
Examples:
#main menu
choice = get_choice(1,4)
#game menu
choice = get_choice(1,3)
#round menu
choice = get_choice(1,3)
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:
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High scores
[4] Exit
What would you like to do?
If the user chooses 1 to start a new game they are to be prompted for their name and game play is to proceed after their name is entered. If the user chooses 2 to load a game then they should be taken to a prompt to enter a name that is to be used to load a saved game. If the user chooses 3 to display the high scores the program will display a list of the high scores in wins per number of rounds ratio is descending order. For example
John has 2 wins, 1 tie, 3 loss, is 2 win / 6 rounds = .33
Stephanie1 win, 0 ties, 1 losses, is 1 win / 2 round = .5
So when presenting High Scores:
What would you like to do? 3
Rank Name Win% Wins Ties Losses
1 Stephanie 0.50 1 0 1
2 John 0.33 2 1 3
If the user chooses 4 to quit the game the program is to exit
Game Menu
This section describes the game menu. When at the game menu the following title, menu, and input prompt are to be displayed:
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do?
1. Play A Round
If the user chooses to Play a Round the program will move to the Game Play portion of the program. This is described below
2. View Stats If the user chooses to View Stats the program will display the currents users stats and how long its been since their account has been created
Example:
Username: John
Wins Ties Losses Age
3 1 3 57h
3. Exit If the user chooses to exit, the program is to exit.
Game Play
This section describes game play. For each round a line that includes the round number is to be displayed followed by a menu that lets 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 would you like to do?
The user makes their choice and the computer chooses randomly. The result of the round is to be displayed using the following format:
For Wins: <User Choice > beats <Computer Choice>! You Win!
For Ties: You both picked <Choice> ! It's a tie
For losses: <Computer Choice> beats <User Choice. You lose
After the each round the user is to be presented with the Game Menu:
Saving / Loading:
When the program ends, we don’t want to lose our users. Each time you run the program, it should be possible to add new users and load previous users. To accomplish that, we’ll need to save and load our list of users.
You can develop your own strategy of doing that if you’d like. Here are a few of our suggestions: I suggest using pickle to save and load user information. Whenever the game is run and then exited, update that User object in the list. Then serialize your list and save it to a file. Section 9.3 in the textbook provides information on serializing objects. “Serializing an object is the process of converting the object to a stream of bytes that can be saved to a file for later retrieval.” Then each time the program starts, check for that file of users. If it does not exist, start with a new, empty list. If it does exist, you can read that file and de-serialize it back into a list of User objects. Study 9.3 Serializing Objects to learn how serialization works. Also, refer to 10.3 Working with Instances for examples on how to serialize objects. Your users should be saved in a file named rps.dat. When running the program if it cannot find rps.dat print out the following error message and then start the name. It will create a new rps.dat file with new users.
Example
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 1
What is your name? Johnny
Hello Johnny. Let's play!
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit What would you like to do? 2
Username: Johnny
Wins Ties Losses Age
0 0 0 4s
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit What would you like to do? 1
Round #1
[1] Rock
[2] Paper
[3] Scissors
What would you like to do? 3
Rock beats Scissors. You lose =(
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit What would you like to do? 3
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 3
Rank Name Win% Wins Ties Losses
1 Stephanie 0.50 1 0 1
2 John 0.33 2 1 3
3 Johnny 0.00 0 0 1
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 2
What is your name? Jack
[!]Error: This User doesn't exist.
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 1
What is your name? Johnny
[!]Error: This User already exists.
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 2
What is your name? Johnny
Welcome back Johnny.
Let's play!
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do? 2
Username: Johnny
Wins Ties Losses Age
0 0 1 38s
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do? 1
Round #2
[1] Rock
[2] Paper
[3] Scissors
What would you like to do? 1
Rock beats Scissors! You Win!
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do? 2
Username: Johnny
Wins Ties Losses Age
1 0 1 43s
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do? 3
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit What would you like to do? 3
Rank Name Win% Wins Ties Losses
1 Stephanie 0.50 1 0 1
2 Johnny 0.50 1 0 1
3 John 0.33 2 1 3
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High Scores
[4] Exit
What would you like to do? 4
Thank you for playing Rock Paper Scissors!
Explanation / Answer
import random
def rps():
computer_choice = random.randint(1,3)
if computer_choice==1:
computer_choice_rock()
elif computer_choice ==2:
computer_choice_paper()
else:
computer_choice_scissors()
def computer_choice_rock():
user_choice = raw_input("1is rock, 2 is paper, 3 is Scissors")
if user_choice =="1":
print "Tied, you choose rock and the computer choose rock."
try_again()
if user_choice =="2":
print "you wwin chose paper and computer chose rock"
try_again()
if user_choice =="3":
print "you lose, you choose scissors and the computer choose rock"
try_again()
else:
print "try again"
computer_choice_rock()
def computer_choice_paper():
user_choice = raw_input("1is rock, 2 is paper, 3 is Scissors")
if user_choice =="1":
print "Loose, you choose rock and the computer choose paper."
try_again()
if user_choice =="2":
print "you tie chose paper and computer chose Paper"
try_again()
if user_choice =="3":
print "you Win, you choose scissors and the computer choose Paper"
try_again()
else:
print "try again"
computer_choice_paper()
def computer_choice_scissors():
user_choice = raw_input("1is rock, 2 is paper, 3 is Scissors")
if user_choice =="1":
print "Win you choose rock and the computer choose Scissors."
try_again()
if user_choice =="2":
print "you Loose chose paper and computer chose Scissors"
try_again()
if user_choice =="3":
print "you Tie, you choose scissors and the computer choose scissors"
try_again()
else:
print "try again"
computer_choice_scissors()
def try_again():
choice = raw_input("Are you paly again? press y for yes or n for no")
if choice == "y" or choice == "yes" or choice =="y":
rps()
elif choice =="n":
print "thanks for playing"
quit()
else:
print "try again()
rps()