Homework 5

NOTE: there are many possible solutions!

from random import choice

ROCK = "🪨"
PAPER = "📄"
SCISSORS = "✂️"
CHOICES = (ROCK, PAPER, SCISSORS)


def runSimulation():
    # ask user how many simulations
    number_of_simulations = int(input("How many simulations? "))
    number_of_playing_1_wins = 0
    number_of_playing_2_wins = 0
    number_of_ties = 0

    for i in range(number_of_simulations):
        p1_choice = choice(CHOICES)
        p2_choice = choice(CHOICES)
        winner = gameWinner(p1_choice, p2_choice)
        if winner == 1:
            number_of_playing_1_wins += 1
        elif winner == 2:
            number_of_playing_2_wins += 1
        else:
            number_of_ties += 1

    print(f"Played {number_of_simulations} games.")
    print("Player 1 won {} times".format(number_of_playing_1_wins))
    print("Player 2 won {} times".format(number_of_playing_2_wins))
    print("Players tied {} times".format(number_of_ties))
    print(f"Win percentage: {number_of_playing_1_wins/number_of_simulations * 100:.2f}%")
    print(f"Lose percentage: {number_of_playing_2_wins/number_of_simulations * 100:.2f}%")
    print(f"Tie percentage: {number_of_ties/number_of_simulations * 100:.2f}%")

def gameWinner(player_one_choice, player_two_choice):
    if player_one_choice == player_two_choice:
        # TIE!
        return 0
    if player_one_choice == ROCK:
        # Player two loses with scissors, wins with paper
        if player_two_choice == SCISSORS:
            return 1
        else:
            return 2
    if player_one_choice == PAPER:
        # Player two loses with rock, wins with scissors
        if player_two_choice == ROCK:
            return 1
        else:
            return 2
    # Player two loses with paper, wins with rock
    if player_two_choice == PAPER:
        return 1
    else:
        return 2

def runSingleGame():
    # ask user for their input
    user_choice = input(f"Pick a choice: {CHOICES} (0, 1, 2): ")
    user_choice = CHOICES[int(user_choice)]

    # generate computer's input
    computer_choice = choice(CHOICES)

    # print out user's choice and computer's choice
    print(f"User choice:{user_choice}\tComputer Choice:{computer_choice}")

    # get winner
    winner = gameWinner(user_choice, computer_choice)
    if winner == 0:
        print("It's a tie!")
    elif winner == 1:
        print("You win!")
    else:
        print("You lose!Ha!")

def main():
    print("Playing Rock/Paper/Scissors!")
    play_again = "y"

    while play_again == "y":
        user_choice = input("Would you like to play a single game (a) or run a simulation (b): ")

        if user_choice == "a":
            runSingleGame()
        else:
            runSimulation()

        play_again = input("Would you like to play again? (y/n) ")

if __name__ == "__main__":
    main()