import random # Check that users have entered a valid # option based on a list def string_checker(question, valid_ans): while True: error = f"Please enter a valid option from the following list: {valid_ans}" # Get user response and make sure it's lowercase user_response = input(question).lower() for item in valid_ans: # check if the user response is a word in the list if item == user_response: return item # check if the user response is the same as # the first letter of an item in the list elif user_response == item[0]: return item # print error if user does not enter something that is valid print(error) print() # Display the instructions if the user wants to see them def instructions(): print(""" *** Instructions **** To begin, choose the number of rounds (or press for infinite mode). Then play against the computer. You need to choose R (rock), P (paper), or S (scissors). The rules are as follows: o Paper beats rock o Rock beats scissors o Scissors beats paper Press to end the game anytime. Good Luck! """) # checks for an integer more than 0 (allows ) def int_check(question): while True: error = "please enter an integer that is 1 or more." to_check = input(question) # check for infinite mode if to_check == "": return "infinite" try: response = int(to_check) # checks that the number is more than / equal to 1 if response < 1: print(error) else: return response except ValueError: print(error) # compares user / computer choice and returns # result (win / lose / tie) def rps_compare(user, comp): # If the user and the computer choice is the same, it is tie if user == comp: round_result = "tie" # There are three ways to win elif user == "paper" and comp == "rock": round_result = "win" elif user == "scissors" and comp == "paper": round_result = "win" elif user == "rock" and comp == "scissors": round_result = "win" # If it is not a win / tie, then it is a loss else: round_result = "lose" return round_result # Automated testing is below in the form (test_case, expected_value) to_test = [ ("rock", "rock", "tie"), ("rock", "paper", "lose"), ("rock", "scissors", "win"), ("paper", "rock", "win"), ("paper", "paper", "tie"), ("paper", "scissors", "lose"), ("scissors", "rock", "lose"), ("scissors", "paper", "win"), ("scissors", "scissors", "tie"), ] # run tests! for item in to_test: # retrieve test case and expected value user = item[0] comp = item[1] expected = item[2] # Main Routine Starts here # Intialise game variables mode = "regular" rounds_played = 0 rounds_tied = 0 rounds_lost = 0 rounds_won = 0 yes_no = ["yes","no"] rps_list = ["rock", "paper", "scissors", "xxx"] game_history = [] print() print("*** Welcome to Rock / Paper / Scissors Game ***") print() # ask user if they want to see the instructions # If they requested want_instructions = string_checker("Do you want to read the instructions? ", yes_no) # checks users enter yes (y) or no (n) if want_instructions == "yes": instructions() # Ask user for number of rounds / infinite mode num_rounds = int_check("How many rounds would you like? Push for infinite mode: ") if num_rounds == "infinite": mode = "infinite" num_rounds = 5 # Game loop starts here while rounds_played < num_rounds: # Rounds headings if mode == "infinite": rounds_heading = f"\n000 Round {rounds_played + 1} (Infinite Mode) 000" else: rounds_heading = f"\n000 Round {rounds_played + 1} of {num_rounds} 000" print(rounds_heading) # randomly choose from the rps list (excluding the exit code) comp_choice = random.choice(rps_list[:-1]) print("computer choice", comp_choice) # get user choice user_choice = string_checker("Choose: ", rps_list) # print ("you chose", user_choice) # If user choice is the exit code, break the loop if user_choice == "xxx": break result = rps_compare(user_choice, comp_choice) # Adjust game lost / game tied counters and add results to game history if result == "tie": rounds_tied += 1 feedback = ":> It's a tie! <:" elif result == "lose": rounds_lost += 1 feedback = ":> you lost! <:" else: feedback = ":) You won (:" # Game loop ends here # Game History / Statistics area if rounds_played > 0: #Calculate Statistics rounds_won = rounds_played - rounds_tied - rounds_lost percent_won = rounds_won / rounds_played * 100 percent_lost = rounds_lost / rounds_played * 100 percent_tied = 100 - percent_won - percent_lost # Output Game Statistics print("Game Statistics") print(f"You won {rounds_won} out of {rounds_played} rounds") print(f"Won: {percent_won:.2f} \t " f"Lost: {percent_lost:.2f} \t " f"Tied: {percent_tied:.2f}") # ask user if they want to see their game history and output it if requested see_history = string_checker("\nDo you want to see your game history? ",yes_no) if see_history == "yes": for item in game_history: print(item) print() print("Thanks for playing.")