Python Project: Rock, Paper, Scissors Game

Last week we learned how to make a Guessing Game.

This week we will look at another beginner-friendly and famous game: Rock, Paper, Scissors.

The game works like this:

  • You choose how many turns you want: best out of 3 (you have to win two times) or 5 (you have to win three times).
  • Then you type one of the options: rock, paper, scissors.
  • The computer will randomly select one of the options.
  • The first to reach the minimum number of necessary wins (either 2 or 3 depending on the number of turns you chose) wins the game overall.

Prerequisites

Here are the things you need to know to understand the code of this game:

The Game

We initialize the game with a list of the options: rock, paper, and scissors.

Choose the number of turns

The input() function is inside a while loop that only breaks if you pick a valid integer, otherwise, it will catch the error with the try/except block and print "Invalid choice", restarting the while loop so the player can choose the number of turns again.

If the input is valid, but not 3 or 5, the loop will restart using continue so the player can the number of turns again.

If the input is valid and either 3 or 5, the program moves on.

The expression necessary_wins = int(turns/2) + 1 gives the minimum amount of victories to win the game.

Notice that 5/2 is 2.5 and we can’t win half a game, so we use int() to round down to 2 and add 1, giving us 3 as the number of wins needed out of 5 turns.

The same logic applies when you choose 3 turns.

The Core of the Game

player_wins and computer_wins are initialized with 0 and are the counters of how many victories the player and the computer have respectively.

The while loop is infinite and breaks only when either player gets the minimum amount of wins needed (2 or 3 depending on your choice of the number of turns).

Inside there is another while loop to get the player input (rock, paper, or scissors), and this loop breaks only if the player typed a valid option contained in the list options.

computer = random.choice(options) uses the random module and the choice() function to randomly choose an option from the list options.

The series of if and elif statements will test all the possible combinations of the player’s option against the computer’s option.

The number of either player_wins or computer_wins is increased by 1 each turn, except when there is a tie.

At the end of the loop, another if statement will check at each turn if either player reached the necessary points to win and breaks the loop if this is true.

The last if/else block make a simple test to check which one scored the most points and prints the winner.

print(f'>>> You scored: {player_wins} point(s) <<<') will display the number of points of the player.

The Code

import random

options = ['rock', 'paper', 'scissors']

while True:
    try:
        turns = int(input("Best out of (3 or 5): "))
        if turns == 3 or turns == 5:
            break
        continue
    except ValueError:
        print("Invalid choice.")

necessary_wins = int(turns/2) + 1

player_wins = 0
computer_wins = 0

while True:

    while True:
        player = input(">>> rock, paper, scissors: ")
        if player in options:
            break

    computer = random.choice(options)

    if player == computer:
        print('It is a tie')
    elif player == 'rock' and computer == 'paper':
        print('Computer wins, paper covers rock')
        computer_wins += 1
    elif player == 'rock' and computer == 'scissors':
        print('You win, rock smashes scissors')
        player_wins += 1
    elif player == 'paper' and computer == 'rock':
        print('You win, paper covers rock')
        player_wins += 1
    elif player == 'paper' and computer == 'scissors':
        print('Computer wins, scissors cut paper')
        computer_wins += 1
    elif player == 'scissors' and computer == 'rock':
        print('Computer wins, rock smashes scissors')
        computer_wins += 1
    elif player == 'scissors' and computer == 'paper':
        print('You win, scissors cut paper')
        player_wins += 1

    if player_wins == necessary_wins or computer_wins == necessary_wins:
        break

if player_wins > computer_wins:
    print(f'>>> You win! <<<')
else:
    print(f'>>> Computer wins! <<<')

print(f'>>> You scored: {player_wins} point(s) <<<')

Testing the Game

There is no strategy to beat the computer in this game since the options are chosen randomly, so it is pure luck!

To run the game, copy and paste the code in a file with any name, I chose the name "rock_paper_scissors.py", and then run:

python rock_paper_scissors.py

Or, depending on your Python installation:

python3 rock_paper_scissors.py

Here is a sample output of when you beat the computer in a game of best out of 3:

Best out of (3 or 5): 3
>>> rock, paper, scissors: rock
It is a tie
>>> rock, paper, scissors: paper
You win, paper covers rock
>>> rock, paper, scissors: scissors
It is a tie
>>> rock, paper, scissors: rock
Computer wins, paper covers rock
>>> rock, paper, scissors: paper
You win, paper covers rock
>>> You win! <<<
>>> You scored: 2 point(s) <<<

Here is a sample output of when the computer wins in a game of best out of 5:

Best out of (3 or 5): 5
>>> rock, paper, scissors: rock
It is a tie
>>> rock, paper, scissors: rock
It is a tie
>>> rock, paper, scissors: rock
You win, rock smashes scissors
>>> rock, paper, scissors: rock
It is a tie
>>> rock, paper, scissors: rock
Computer wins, paper covers rock
>>> rock, paper, scissors: rock
Computer wins, paper covers rock
>>> rock, paper, scissors: rock
It is a tie
>>> rock, paper, scissors: scissors
Computer wins, rock smashes scissors
>>> Computer wins! <<<
>>> You scored: 1 point(s) <<<

If you want to truly understand what is going on in this code, the best thing you can is to modify it and see what happens.

Try to change the messages in the print() functions, change the number of possible turns.

You could add other variations of the game like Rock, Paper, Scissors, Lizard, Spock.

The logic is "simple": Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors, scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, and as it always has, rock crushes scissors.

You just need to increase the number of elif statements to cover all the possibilities.

It might take some time, but if you want to practice, I highly recommend you to give it a try.