Python Forum
Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics
#1
My assignment is to 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. 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 to be maintained for a game are wins, losses, and ties. When a game is saved the user’s name and play statistics (wins, losses, and ties) are to be saved. When a game is loaded based on the user’s name the statistics are loaded and additional game play add to the loaded statistics.

The code below are the functions that I have written thus far. I wasn't sure the best way to load a file, save a file, and calculate/display the function and haven't written much on those yet.
Loading the File Questions
My questions are to load an existing file should I just use readline or read method to bring the statistics from the file name.rps to the user or is there an easier way to do this? The instruction says "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."
Saving the File

To save the statistics generated from the file. Do I just need to write these statistics to a file? Would it be best to pickle these or is there a simpler way to do it? The instruction says"When a game is saved to an .rps file the information in the file is to include user name, Wins,Losses, and Ties.
Create/display the statistics
Finally, to create output similar to down below, should I just write win+=1 underneath when the user defeats the computer or where should this code be placed.
If the user chooses to view the statistics the following information is to be displayed:

<user name>, here are your game play statistics...
Wins: <number of wins>
Losses: <number of losses>
Ties: <number of ties>
Win/Loss Ratio: <wins divided by losses formatted to two digits
to right of decimal point>

I really appreciate your help. I included the code that I currently have down below, but am unable to run it because I am missing several parts that I need to add. I know this thread is kind of long, but I was just wanting to get some general guidance on the methods I should utilize to load, save, calculate, and display statistics. Thanks again.

# This program allows you to play rock, paper, scissors against the computer
import random
import pickle

# Global constants for menu choices
START_NEW_GAME = 1
LOAD_GAME= 2
QUIT = 3

#Global constant for the filename


# main function
def main():
    print('Welcome to Rock, Paper, Scissors!')

    #Initialize a variable for the user's choice.
    choice=0

    # process menu selections until the user wants to quit the program.

    #Get the user's menu choice.
        choice = get_menu_choice()
#The get_menu_choice function displays the menu
# and gets a validated choice from the user
def get_menu_choice():
    print()
    print('1. Start New Game')
    print('2. Load Game')
    print('3. Quit')
    print()

    # Get the user's choice.
    choice = int(input('Enter your choice: '))
    # Validate the choice.
    while choice < START_NEW_GAME or choice > QUIT:
        choice = int(input('Enter a valid choice: '))

        # return the user's choice.
        return choice
    if choice == 1:
        start_new_game()
    elif choice == 2:
        load_game()
    else:
        break

# The start_new_game function allows a player to start a new game
def start_new_game:
    name_of_player = input('What is your name? ')
    print("Hello " + name_of_player + "." + " Let's play!")

def load_game:
    name_of_player = input('What is your name? ')
    
    
    print("Welcome back" + name_of_player + "." + " Let's play!")

# Global constants for menu choices
ROCK = 1
PAPER = 2
SCISSORS = 3

# The game_menu_choice function displays the gameplay menu
# and gets a validated choice from the user
def game_menu_choice():
    print()
    print("Round",count)

    print('1. Rock')
    print('2. Paper')
    print('3. Scissors')
    print('What will it be?')
    
    print()

    # Get the user's choice.
    user_choice = int(input('Enter your choice: '))
    # Validate the choice.
    while user_choice < ROCK or user_choice > SCISSORS:
        user_choice = (int(input('Enter a valid choice: '))
                    

        # return the user's choice
        return user_choice
    
    
    
# This program will allow the user to actually play rock, paper, scissors
def play_the_game():
    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():
    if user_choice=="1"
        print("You chose Rock. The computer chose Rock. You tie!")
        try_again()
    if user_choice=="2"
        print("You chose Paper. The computer chose Rock. You win!")
        try_again()
    if user_choice=="3"
        print("You chose Scissors. The computer chose Rock. You lose!")
        try_again()
    else print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_rock():

def computer_choice_paper():
    if user_choice=="1"
        print("You chose Rock. The computer chose Paper. You lose!")
        try_again()
    if user_choice=="2"
        print("You chose Paper. The computer chose Paper. You tie!")
        try_again()
    if user_choice=="3"
        print("You chose Scissors. The computer chose Paper. You win!")
        try_again()
    else print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_paper():
def computer_choice_scissors():
    if user_choice=="1"
        print("You chose Rock. The computer chose Scissors. You win!")
        try_again()
    if user_choice=="2"
        print("You chose Paper. The computer chose Scissors. You lose!")
        try_again()
    if user_choice=="3"
        print("You chose Scissors. The computer chose Scissors. You tie!")
        try_again()
    else print("You entered an invalid value. Please try again and enter either 1,2, or 3")
        computer_choice_scissors():

# Global constants for try_again menu choice
PLAY_AGAIN = 1
VIEW_STATISTICS = 2
QUIT = 3        
# Define Try again Function
def try_again():
    print()
    print("What would you like to do?")

    print("1. Play again")
    print("2. View Statistics")
    print("3. Quit")

    print("Enter choice: ")
    print()


    # Get the user's choice.
    user_choice_2 = int(input('Enter your choice: '))
    # Validate the choice.
    while user_choice < PLAY_AGAIN or user_choice > QUIT:
        user_choice_2= (int(input('Enter a valid choice: '))

        # return the user's choice
        return user_choice

    if user_choice_2=="1":
        game_menu_choice():
    if user_choice_2=="2":
        statistics()
    if user_choice_3=="3":
        break

main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 2,865 Jun-24-2021, 08:00 PM
Last Post: deanhystad
Question Rock, paper, scissors spelling error banidjamali 6 3,138 Jan-19-2021, 02:51 PM
Last Post: banidjamali
  Advanced Algorithms and Computational Models hafedh 4 2,284 Aug-31-2020, 06:37 PM
Last Post: buran
  Collecting Average User Statistics? 1st semester programmer Jagsrs28 3 2,253 Mar-05-2020, 08:05 PM
Last Post: Jagsrs28
  Rock, Paper, Scissors Game kramon19 2 5,290 Jan-10-2020, 08:18 AM
Last Post: perfringo
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 5,746 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Statistics from text file Zatoichi 1 4,184 Feb-05-2018, 04:52 AM
Last Post: ka06059
  Advanced sorting of a built-in list Whisper40 6 4,046 Jan-11-2018, 07:27 PM
Last Post: Whisper40
  Rock Paper Scissors Warmlawpk441 4 4,969 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,305 Oct-03-2017, 07:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020