Rock Paper Scissors Game Python Code

This code uses ASCII art to create a simple and visually appealing layout for the Rock Paper Scissors game. It prompts the player to enter their choice, generates a random choice for the computer, compares the choices, and determines the winner. The player can choose to play again or exit the game.

Here are the steps to run the Rock Paper Scissors game program

  1. Make sure you have Python installed on your computer. If you don’t have it, you can download and install it from the official Python website (https://www.python.org).
  2. Open a text editor and copy the provided code into a new file.
  3. Save the file with a .py extension, for example, “rock_paper_scissors.py”.
  4. Open a terminal or command prompt and navigate to the directory where you saved the Python file.
  5. Run the program by typing python rock_paper_scissors.py in the terminal or command prompt and press Enter.
  6. The game will start, and you will see a beautiful ASCII art layout with the game’s title and options.
  7. Choose an option by entering a number between 1 and 3 based on your choice of Rock, Paper, or Scissors.
  8. The program will generate a random choice for the computer and display both choices on the screen.
  9. The program will determine the winner and display the result.
  10. After the result is shown, the program will ask if you want to play again. Enter ‘y’ to play again or ‘n’ to exit the game.
  11. If you choose to play again, the game will restart from Step 7. If you choose to exit, the program will display a goodbye message and terminate.

Code

import random

def print_header():
    print("===================================")
    print("        Rock Paper Scissors        ")
    print("===================================")

def print_choices():
    print("Choose one of the following:")
    print("1. Rock")
    print("2. Paper")
    print("3. Scissors")

def get_player_choice():
    while True:
        choice = input("Enter your choice (1-3): ")
        if choice.isdigit() and 1 <= int(choice) <= 3:
            return int(choice)
        print("Invalid input. Please enter a number between 1 and 3.")

def get_computer_choice():
    return random.randint(1, 3)

def print_choice(player_choice, computer_choice):
    choices = ["Rock", "Paper", "Scissors"]
    print(f"You chose: {choices[player_choice - 1]}")
    print(f"Computer chose: {choices[computer_choice - 1]}")

def print_result(player_choice, computer_choice):
    if player_choice == computer_choice:
        print("It's a tie!")
    elif (player_choice == 1 and computer_choice == 3) or (player_choice == 2 and computer_choice == 1) or (player_choice == 3 and computer_choice == 2):
        print("You win!")
    else:
        print("Computer wins!")

def play_again():
    choice = input("Do you want to play again? (y/n): ")
    return choice.lower() == "y"

def print_goodbye():
    print("Thanks for playing. Goodbye!")

# Main game loop
while True:
    print_header()
    print_choices()

    player_choice = get_player_choice()
    computer_choice = get_computer_choice()

    print_choice(player_choice, computer_choice)
    print_result(player_choice, computer_choice)

    if not play_again():
        break

print_goodbye()

Output

Note:- Feel free to customize the layout or add more features to the game as per your requirements.