The classic game for two.

★★★

Rock paper scissors is a hand game originating from China, played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock" (a closed fist), "paper" (a flat hand), and "scissors" (a fist with the index finger and middle finger extended, forming a V). The rock beats the scissors. The paper beasts the rock and the scissors beats the paper. This version of the game is played between a user and the computer over five rounds. The first player to win five rounds wins the game.

Rock, paper, scissors

Make

Write a program that simulates the game rock, paper, scissors to be played by the user against the computer.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Create a subprogram called get_player_choice that:

  1. Asks the user to enter their choice of rock, paper or scissors as a letter: r, p or s.
  2. Returns the choice only when it is a valid letter.

Create a subprogram called convert that:

  1. Takes a string parameter called rps.
  2. If rps is “r” it returns “rock”.
  3. If rps is “p” it returns “paper”.
  4. If rps is “s” it returns “scissors”.

This is a helper function that converts a single character input from the user (their choice) into something more readable for output.

Create a subprogram called cpu_choice that:

  1. Generates a random number between 1 and 3.
  2. Returns the choice as “r”, “p” or “s” instead of 1, 2 or 3.

Create a subprogram called who_won_round that:

  1. Takes two parameters, player and cpu which are the two choices made: “r”, “p” or “s”.
  2. Determines who won the round.
  3. Returns the winner of the round as “player”, “cpu” or “draw”.

Create a subprogram called play_game that:

  1. Plays the game as described using the subprograms above.
  2. Outputs the current score of each player before each round. (Winning a round scores one point.)
  3. Outputs the choice of both players, including who beat whom, or if the round was a draw.
  4. Outputs who won the game after one player reaches five points.

Complete the main program so that:

  1. The play_game subprogram is called.

Typical inputs and outputs from the program would be:

player score: 0  cpu score: 0
Enter rock, paper, scissors (r/p/s): g
Enter rock, paper, scissors (r/p/s): h
Enter rock, paper, scissors (r/p/s): r
player's rock beats cpu's scissors

player score: 1  cpu score: 0
Enter rock, paper, scissors (r/p/s): p
cpu's scissors beats player's paper

player score: 1  cpu score: 1
Enter rock, paper, scissors (r/p/s): s
cpu's rock beats player's scissors

player score: 1  cpu score: 2
Enter rock, paper, scissors (r/p/s): r
rock - rock - it's a draw.

player score: 1  cpu score: 2
Enter rock, paper, scissors (r/p/s): p
player's paper beats cpu's rock

player score: 2  cpu score: 2
Enter rock, paper, scissors (r/p/s): s
scissors - scissors - it's a draw.

player score: 2  cpu score: 2
Enter rock, paper, scissors (r/p/s): r
rock - rock - it's a draw.

player score: 2  cpu score: 2
Enter rock, paper, scissors (r/p/s): p
cpu's scissors beats player's paper

player score: 2  cpu score: 3
Enter rock, paper, scissors (r/p/s): s
cpu's rock beats player's scissors

player score: 2  cpu score: 4
Enter rock, paper, scissors (r/p/s): r
cpu's paper beats player's rock

CPU WINS!
🆘 If you're really stuck, use this Parsons code sorting exercise
convert
# Function to convert r/p/s to rock, paper, scissors
# Input is less hassle for the user and output is nice
def convert(rps):
---
    match rps:
---
        case "r":
---
            return "rock"
---
        case "p":
---
            return "paper"
---
        case "s":
---
            return "scissors"
get_player_choice
# Function to return valid player choice
def get_player_choice():
---
    valid = False
---
    # Input must be r, p or s
    while not valid:
---
        choice = input("Enter rock, paper, scissors (r/p/s): ")
---
        if choice == "r" or choice == "p" or choice == "s":
---
            valid = True
---
    return choice
get_cpu_choice
# Function to return cpu choice
def get_cpu_choice():
---
    cpu = random.randint(1, 3)
---
    # convert random number to cpu choice
    match cpu:
---
        case 1:
---
            return "r"
---
        case 2:
---
            return "p"
---
        case 3:
---
            return "s"
who_won_round
# Function to determine the winner of a round
def who_won_round(player, cpu):
---
    winner = "draw"
    # Check who wins the round
---
    if player == "r" and cpu == "s":
---
        winner = "player"
---
    if player == "p" and cpu == "r":
---
        winner = "player"
---
    if player == "s" and cpu == "p":
---
        winner = "player"
---
    if cpu == "r" and player == "s":
---
        winner = "cpu"
---
    if cpu == "p" and player == "r":
---
        winner = "cpu"
---
    if cpu == "s" and player == "p":
---
        winner = "cpu"
---
    return winner
play_game
# Procedure to play the game - first to 5 points
def play_game():
---
    player_score = 0
    cpu_score = 0
    random.seed()
---
    # Play until a score of 5 is reached
    while player_score < 5 and cpu_score < 5:
---
        print("player score:", player_score, " cpu score:", cpu_score)
        player_choice = get_player_choice()
        cpu_choice = get_cpu_choice()
---
        round_winner = who_won_round(player_choice, cpu_choice)
        player_shape = convert(player_choice)
        cpu_shape = convert(cpu_choice)
---
        # Add to the score
---
        if round_winner == "player":
---
            print("player's", player_shape, "beats cpu's", cpu_shape)
---
            player_score = player_score + 1
---
        elif round_winner == "cpu":
---
            print("cpu's", cpu_shape, "beats player's", player_shape)
---
            cpu_score = cpu_score + 1
---
        else:
---
            print(player_shape, "-", cpu_shape, "- it's a draw.")
---
        print()
---
    if player_score == 10:
---
        print("Player WINS!")
---
    else:
---
        print("CPU WINS!")