During a round of play the user chooses rock, paper, or scissors from a menu and
ID: 3577271 • Letter: D
Question
During a round of play the user chooses rock, paper, or scissors from a menu and the computer makes its choice randomly. For each round it is possible for the user to win, lose, or tie. The statistics that are maintained within a user object for a game are wins, losses, and ties. When a game is exited, the user play statistics (wins, losses, and ties) are to be updated in the users class and then will be saved. Whenever a new User object is created, add it to your Users objects list.
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 Rock,
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)
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”
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.
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.
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 John. Let’s play! If the user already exists in the list of users, the user is to be presented with a message indicating that the username given already exists and then presented with the startup menu described at the top of the requirements. The message is to be an error message Example: [!]Error: This User already exists. After the hello statement is presented to the user, game menu is to proceed. Game menu is described below in the Game menu section.
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 user by finding it in the list of users located in Users class. If the user object is found, the information about the game and statistics are to be loaded locally, 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 plus one. (Note: number of rounds previously played is sum of wins, losses, and ties plus one) Game menu is described below in the Game menu 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 John. Let’s play! If the user is not found, the user is to be presented with a message indicating the users data could not be found and then presented with the startup menu described at the top of the requirements. The message is to be an error message Example: [!]Error: This User doesn't exist.
3. Display High Scores If the user chooses display High Scores the program will display a list of the high scores rank by the number of wins divided by the total number of rounds in descending order. The text can be formatted using the “ ” tab or however else you would like. For the name I used a "{:<15}".format(cur_max_user.get_username()) and then you should also round the Win% to 2 decimal places. Rank Name Win% Wins Ties Losses 1 Stephanie 0.50 1 0 1 2 John 0.33 2 1 3
4. Exit If the user chooses to exit, 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 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 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! Example: Scissors beat Paper! You Win!
For Ties You both picked paper! It's a tie Example: You both picked Paper! It's a tie For losses<computer choice> beats <user choice> ..You lose =( Example: Paper beats Rock..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. “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. 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.
The error message should be: [!]Error: Could not find previous users game data.
Example Output If rps.dat doesn’t exist ===== RESTART: /Users/jtwyp6/Desktop/IT1040 - RPS Manager Soltion/RPS.py ===== [!]Error: Could not find previous users game data.
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 rps.dat does 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
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 Rock,
[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
User.py
Users.py
RPS.py
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?
jolly
Hello jolly.Let's play!
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do?
2
Username: jolly
Win Losses Ties Age
0 0 0 3s
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?
1
Both of you had same choice as Rock! So its tie
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?
2
You win :), You chose 2 and computer chose 1
RPS Game Menu
-------------
[1] Play a Round
[2] View Stats
[3] Exit
What would you like to do?
2
Username: jolly
Win Losses Ties Age
1 0 1 14s
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 j 0 1 2
2 jolly 1 1 0
Rock Paper Scissors
-------------------
[1] Start a New Game
[2] Load a Game
[3] Display High scores
[4] Exit
What would you like to do?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.