Skip to content

Rock Paper Scissors Python: Mastering the Classic Game

[

Make Your First Python Game: Rock Paper Scissors!

by Chris Wilkerson

Game programming is a great way to learn how to program. You use many tools that you’ll see in the real world, plus you get to play a game to test your results! An ideal game to start your Python game programming journey is rock paper scissors.

In this tutorial, you’ll learn how to:

  • Code your own rock paper scissors game
  • Take in user input with input()
  • Play several games in a row using a while loop
  • Clean up your code with Enum and functions
  • Define more complex rules with a dictionary

What Is Rock Paper Scissors?

You may have played rock paper scissors before. Maybe you’ve used it to decide who pays for dinner or who gets first choice of players for a team.

If you’re unfamiliar, rock paper scissors is a hand game for two or more players. Participants say “rock, paper, scissors” and then simultaneously form their hands into the shape of a rock (a fist), a piece of paper (palm facing downward), or a pair of scissors (two fingers extended). The rules are straightforward:

  • Rock smashes scissors.
  • Paper covers rock.
  • Scissors cut paper.

Now that you have the rules down, you can start thinking about how they might translate to Python code.

Play a Single Game of Rock Paper Scissors in Python

Using the description and rules above, you can make a game of rock paper scissors. Before you dive in, you’re going to need to import the module you’ll use to simulate the computer’s choices:

import random

Awesome! Now you’re able to use the different tools inside random to randomize the computer’s actions in the game. Now what? Since your users will also need to be able to choose their actions, the first logical thing you need is a way to take in user input.

Take User Input

Taking input from a user is pretty straightforward in Python. The goal here is to ask the user what they would like to choose as an action and then assign that choice to a variable:

user_action = input("Enter a choice (rock, paper, scissors): ")

This will prompt the user to enter a selection and save it to a variable for later use. Now that the user has selected an action, the computer needs to decide what to do.

Make the Computer Choose

A competitive game of rock paper scissors involves strategy. Rather than trying to develop a model for that, though, you can simply randomize the computer’s choice using the random.choice() function:

possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)

This code sets up a list of possible actions for the computer and then randomly selects one using random.choice(). Now that both the user and the computer have selected their actions, the next step is to determine the winner.

Determine a Winner

To determine the winner, you can use a series of conditional statements that compare the choices made by the user and the computer. Remember the rules: rock smashes scissors, paper covers rock, and scissors cut paper.

Here’s an example code snippet that determines the winner based on the choices:

if user_action == computer_action:
print(f"Both players chose {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cut paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cut paper! You win!")
else:
print("Rock smashes scissors! You lose.")

This code compares the user and computer choices using conditional statements and prints the appropriate message based on the outcome. With this code, you can determine the winner of a single game of rock paper scissors!

Play Several Games in a Row

Playing multiple games in a row can be done by placing the game logic inside a while loop. You can use a counter variable to keep track of the number of games played and add a condition to exit the loop when a certain number of games have been played.

Here’s an example of how you can modify the code to play multiple games:

games_to_play = 3
games_played = 0
while games_played < games_to_play:
# Game logic goes here
games_played += 1

This code sets a variable games_to_play to the desired number of games and initializes the counter games_played to 0. The game logic is placed inside the while loop, and after each game, the counter is incremented. The loop will exit once the counter reaches the number of games to play.

By placing the game logic inside a loop, you can play several games of rock paper scissors in a row!

Describe an Action With Enum

Now that you have the basic game logic working, you can improve the readability of your code by using the Enum class from the enum module to represent the possible actions.

Here’s an example of how you can use Enum to define the actions:

from enum import Enum
class Action(Enum):
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"

With this code, you can now refer to the possible actions using the Action class, which provides more readable and less error-prone code. For example, you can compare actions like this:

if user_action == Action.ROCK:
# Rock logic goes here

Using Enum can improve the readability and maintainability of your code.

The Flow(chart) of Your Program

To visualize the flow of your program, you can create a flowchart that outlines the different steps and branches. This can help you understand the logic and spot any potential issues.

This flowchart demonstrates the different branches and steps involved in a game of rock paper scissors. By following the arrows, you can see how the game progresses and how the winner is determined. Having a flowchart can be a helpful tool for understanding and debugging your code.

Split Your Code Into Functions

To make your code more modular and reusable, you can split it into functions. This will allow you to organize your code better and make it easier to test and debug.

Here’s an example of how you can refactor your code into functions:

def get_user_action():
user_action = input("Enter a choice (rock, paper, scissors): ")
return Action(user_action)
def get_computer_action():
possible_actions = list(Action)
computer_action = random.choice(possible_actions)
return computer_action
def determine_winner(user_action, computer_action):
if user_action == computer_action:
return "tie"
elif user_action == Action.ROCK:
if computer_action == Action.SCISSORS:
return "user"
else:
return "computer"
elif user_action == Action.PAPER:
if computer_action == Action.ROCK:
return "user"
else:
return "computer"
elif user_action == Action.SCISSORS:
if computer_action == Action.PAPER:
return "user"
else:
return "computer"
def play_game():
user_action = get_user_action()
computer_action = get_computer_action()
winner = determine_winner(user_action, computer_action)
if winner == "tie":
print(f"Both players chose {user_action}. It's a tie!")
else:
print(f"{winner.capitalize()} wins!")

In this refactored code, each step of the game logic has been extracted into separate functions. This allows for better organization and encapsulation of the code. The play_game() function serves as the entry point and orchestrates the flow of the game. By splitting your code into functions, you can make it more modular and easier to work with.

Rock Paper Scissors … Lizard Spock

If you want to take your rock paper scissors game to the next level, you can introduce additional actions inspired by the TV show “The Big Bang Theory.” In the show, an extended version of rock paper scissors is played, adding two more actions: lizard and Spock.

Here’s an example of how you can add the lizard and Spock actions to your game:

class Action(Enum):
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"
LIZARD = "lizard"
SPOCK = "spock"
def determine_winner(user_action, computer_action):
if user_action == computer_action:
return "tie"
elif user_action == Action.ROCK:
if computer_action == Action.SCISSORS or computer_action == Action.LIZARD:
return "user"
else:
return "computer"
elif user_action == Action.PAPER:
if computer_action == Action.ROCK or computer_action == Action.SPOCK:
return "user"
else:
return "computer"
elif user_action == Action.SCISSORS:
if computer_action == Action.PAPER or computer_action == Action.LIZARD:
return "user"
else:
return "computer"
elif user_action == Action.LIZARD:
if computer_action == Action.PAPER or computer_action == Action.SPOCK:
return "user"
else:
return "computer"
elif user_action == Action.SPOCK:
if computer_action == Action.ROCK or computer_action == Action.SCISSORS:
return "user"
else:
return "computer"

With this addition, your game of rock paper scissors becomes more versatile and allows for a wider range of actions and strategies.

Conclusion

Congratulations! You’ve created your own rock paper scissors game using Python. By following the steps in this tutorial, you’ve learned how to code the game logic, take user input, play multiple games in a row, clean up your code with Enum and functions, and even extend the game with additional actions.

Game programming is a fun and rewarding way to improve your Python skills. Now that you have the basic knowledge and understanding of game development, you can further explore and expand your programming abilities. Keep experimenting, learning, and creating new games!

Remember, practice makes perfect, so keep coding and have fun!