top of page

25% Discount For All Pricing Plans "welcome"

Rock-Paper-Scissors Game


What is Rock-Paper-Scissors?


Rock-Paper-Scissors is a simple hand game played between two players. Players simultaneously choose one of three shapes: rock, paper, or scissors. The winner is determined by the following rules:

  • Rock breaks scissors (rock wins)

  • Scissors cut paper (scissors win)

  • Paper wraps rock (paper wins) If both players choose the same shape, the game is a tie. This game can also be used for decision-making or resolving disputes.


Project Objectives and Student Benefits

With this project, students will reinforce their skills in developing simple and functional programs using basic Python knowledge. They will also gain experience in game simulations and user interaction.


Project Goals

  • Creating a game simulation using basic Python structures and control flows.

  • Receiving data from the user and managing the game flow by processing this data.

  • Writing more modular and readable code with the help of functions.


Step-by-Step Tasks of the Project

  1. Define possible moves.

  2. Get a move from the user.

  3. Select a random move from the computer.

  4. Compare the moves of the user and the computer to determine the result.

  5. Print the result of the game.

  6. Make the code more modular using functions.


Step 1: Define Possible Moves

As the first step, the possible moves to be used in the game will be defined.

# Possible moves
moves = ["rock", "paper", "scissors"]

This code snippet defines the possible moves to be used in the game.


Step 2: Get a Move from the User

In the second step, a move will be taken from the user.

# Get a move from the user
user_move = input("Choose rock, paper, or scissors: ").lower()
while user_move not in moves:
    user_move = input("Invalid move! Choose rock, paper, or scissors: ").lower()

This code snippet takes a move from the user and checks if it is a valid move. It keeps asking for a move until the user selects a valid one.


Step 3: Select a Random Move from the Computer

In the third step, a random move will be selected from the computer.

import random

# Select a random move from the computer
computer_move = random.choice(moves)
print(f"The computer's move: {computer_move}")

This code snippet selects a random move from the computer and prints it. The random.choice() function is used to select a random element from the moves list.


Step 4: Compare the Moves of the User and the Computer to Determine the Result

In the fourth step, the moves of the user and the computer will be compared to determine the result of the game.

# Compare the moves of the user and the computer to determine the result
if user_move == computer_move:
    result = "It's a tie!"
elif (user_move == "rock" and computer_move == "scissors") or \\\\
     (user_move == "scissors" and computer_move == "paper") or \\\\
     (user_move == "paper" and computer_move == "rock"):
    result = "You win!"
else:
    result = "You lose!"

This code snippet compares the moves of the user and the computer to determine the result of the game. The result is determined based on the match of the user and computer moves.


Step 5: Print the Result of the Game

In the fifth step, the result of the game will be printed.

# Print the result of the game
print(result)

This code snippet prints the result of the game.


Step 6: Solution with Functions


Function to Get User Move

This function gets a valid move from the user.

def get_user_move():
    """Gets a valid move from the user."""
    moves = ["rock", "paper", "scissors"]
    user_move = input("Choose rock, paper, or scissors: ").lower()
    while user_move not in moves:
        user_move = input("Invalid move! Choose rock, paper, or scissors: ").lower()
    return user_move

This function gets a valid move from the user and returns it.


Function to Select Computer Move

This function selects a random move from the computer.

def select_computer_move(moves):
    """Selects a random move from the computer."""
    return random.choice(moves)

This function selects a random move from the computer and returns it.


Function to Determine the Result of the Game

This function compares the moves of the user and the computer to determine the result of the game.

def determine_result(user_move, computer_move):
    """Compares the moves of the user and the computer to determine the result of the game."""
    if user_move == computer_move:
        return "It's a tie!"
    elif (user_move == "rock" and computer_move == "scissors") or \\\\
         (user_move == "scissors" and computer_move == "paper") or \\\\
         (user_move == "paper" and computer_move == "rock"):
        return "You win!"
    else:
        return "You lose!"

This function compares the moves of the user and the computer to determine the result of the game and returns it.


Function to Start the Game

This function starts the Rock-Paper-Scissors game and manages the flow of the game.

def start_game():
    """Starts the Rock-Paper-Scissors game."""
    moves = ["rock", "paper", "scissors"]

    user_move = get_user_move()
    computer_move = select_computer_move(moves)

    print(f"The computer's move: {computer_move}")

    result = determine_result(user_move, computer_move)

    print(result)

# Start the game
start_game()

This function starts the Rock-Paper-Scissors game and manages the flow of the game. It gets the moves of the user and the computer, compares the moves, and prints the result of the game.


Topics Covered

  • Data Types: In this project, lists (moves) and strings (user move, computer move) were used.

  • Lists: Possible moves were kept in the form of lists.

  • Conditional Statements: if-else structures were used to compare the moves of the user and the computer to determine the result of the game.

  • Functions: Functions ensured that the code was modular and reusable.

  • Random Module: The random.choice() function was used to select a random element from the moves list.

Comments


Commenting has been turned off.
bottom of page