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

Using Python 3.4 During a round of play the user chooses rock, paper, or scissor

ID: 3574739 • Letter: U

Question

Using Python 3.4 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.

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 __wins __ties __losses __created

To store the name of the player
To store the number of wins
To store the number of ties
To store the number of losses
To store when the user account was created

The User class should also have the following methods:

__init__

get_username

get_user_wins set_user_wins

get_user_ties set_user_ties

get_user_losses

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.

Returns the value of __username field

Return the value of the __wins field
Takes in 1 parameter and sets __wins to that parameter

Returns the value of the __ties field
Takes in 1 parameters and sets __ties to that parameter

Returns the value of the __losses field

set_user_losses get_round

get_age

Takes in 1 parameter and sets __losses to that parameter Returns the current round number (not the next)

HINT: return (wins + ties + losses)

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, delete (we don’t need to worry about delete)#

def create_user

def read_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.

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.

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.

Takes in a parameter, a username, and deletes that user from the list.
If the name doesn’t exist, we print the error:
[!]Error: This User doesn't exist.

def update_user

#This isn’t needed

def delete_user

RPS.py

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

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

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 2John 0.33213

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 2John 0.33213

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?

Explanation / Answer

#!/usr/bin/env python3 # rps.py -- Rock, Paper, Scissors game from random import choice # Global constants WIN = 0 LOSE = 1 DRAW = 2 def get_result(h, c): """Determine who wins this round Parameters: h: Human's selection (r, p or s) c: Computer's selection (r, p or s) Returns a tuple of the WIN/LOSE/DRAW value and the string describing the results """ # Strings used in results pr = 'Paper covers Rock. {}' rs = 'Rock smashes Scissors. {}' sp = 'Scissors cuts Paper. {}' ti = 'We both have {}. {}' # Win/lose/draw strings wins = ('You win', 'I win', "It's a draw") # Dictionary defining the results res = { 'rr' : (DRAW, ti.format('rocks', wins[DRAW])), 'rp' : (LOSE, pr.format(wins[LOSE])), 'rs' : (WIN, rs.format(wins[WIN])), 'pr' : (WIN, pr.format(wins[WIN])), 'pp' : (DRAW, ti.format('paper', wins[DRAW])), 'ps' : (LOSE, sp.format(wins[LOSE])), 'sr' : (LOSE, rs.format(wins[LOSE])), 'sp' : (WIN, sp.format(wins[WIN])), 'ss' : (DRAW, ti.format('scissors', wins[DRAW])) } # Return the result tuple return res[h + c] def get_rps(): """Get Rock/Paper/Scissor choice.""" while True: select = input(' Do you choose Rock, Paper or Scissors ').lower() # Check for empty input or quit command if not select or select in ['q', 'quit']: return 'q' # return quit code # Check for valid input if select in ['r', 'rock', 'p', 'paper', 's', 'scissors']: return select[0] # return first character print('What did you say?? Try again please') #================= Main Program starts here ============== # Keep track of results: # scores[0] = number of human wins # scores[1] = number of computer wins # scores[2] = number of draws scores = [0] * 3 things = {'r':'a rock', 'p':'paper', 's':'scissors'} print("Let's play a game or Rock, Paper, Scissors. ") print('Enter "r", "p", "s", "rock", "paper", or "scissors" for your choice.') print('Use empty input, "q" or "quit" to end the game.') while True: computer = choice('rps') # Computer selects human = get_rps() # Human selects if human == 'q': break print('You have {}, I have {}. '.format( things[human], things[computer]), end='') scr, res = get_result(human, computer) scores[scr] += 1 # Count win/loss/draw print(res) # And show results # Show final scores print(' Total scores:') print(' You won {} games'.format(scores[WIN])) print(' Computer won {} games'.format(scores[LOSE])) print(' There were {} tie games'.format(scores[DRAW])) print(' Thanks for playing with me. Bye now.')

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