Rock, Paper, Scissors Game Project (Use Python 3.4.2) For this project you are t
ID: 3831349 • Letter: R
Question
Rock, Paper, Scissors Game Project (Use Python 3.4.2)
For this project you are to 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.
This Wikipedia article describes the game play:
http://en.wikipedia.org/wiki/Rock-paper-scissors
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
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 __created 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)
* Note that the string returned from __get_age is only concerned with the largest unit of time. If the user is 0 to 59 seconds old, the value returned from __get_age will end with an “s” for “seconds”. If the user is some number of minutes old, it will end with an “m” and ignore the seconds. Likewise, if the user is 1 or more hours old, it will ignore both the minutes and seconds. To work with time, you’ll need to use Python’s time module. The documentation for that module is available here: https://docs.python.org/3.4/library/time.html
HINT: If we have a user that wins a round and want to increment their wins stat.
user.get_user_wins() will get the current number of wins (it is an integer) we add one with + 1
we pass that number as a parameter to user.set_user_wins(new incremented wins)
in working code:
user.set_user_wins(user.get_user_wins()+1)
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:
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 delete_user:
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 display_users:
This method will print information about each of the User objects within __user_list. If no users have been added to the list, it should print out a message saying that there are no users
##Extra##
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
Rock, Paper, Scissors Game
Final Project
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
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.
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
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:
Hint: round number = wins + ties + losses + 1
Round
[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
beats ! You Win!
Example:
Scissors beat Paper! You Win!
For Ties
You both picked ! It's a tie
Example:
You both picked Paper! It's a tie
For losses
beats ..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. 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.
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
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
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
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
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
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!
Rank Name Win% Wins Ties Losses 1 Stephanie 0.50 1 0 1 2 John 0.33 2 1 3Explanation / Answer
from random import randint
#create a list of play options
t = ["Rock", "Paper", "Scissors"]
#assign a random play to the computer
computer = t[randint(0,2)]
#set player to False
player = False
while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!")
#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(0,2)]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.