100 Days of Code: Day 4 - Rock-Paper-Scissors Game

Challenge: 100 Days of Code with Python

Day: 4

Introduction

Welcome to Day 4 of my 100 Days of Code challenge with Python! Today, I created a classic and fun project: a Rock-Paper-Scissors game. This interactive game pits the player against the computer in a battle of Rock, Paper, and Scissors. It's a great way to practice user input handling, control flow, and randomization in Python.

Project: Rock-Paper-Scissors Game

The Rock-Paper-Scissors game allows players to choose their weapon and then generates a random choice for the computer. The game then determines the winner based on the classic rules of Rock, Paper, Scissors.

The Code

Here is the Python code for the Rock-Paper-Scissors game:

import random

rock = '''

_______

---' ____)

(_____)

(_____)

(____)

---.__(___)

'''

paper = '''

_______

---' ____)____

______)

_______)

_______)

---.__________)

'''

scissors = '''

_______

---' ____)____

______)

__________)

(____)

---.__(___)

'''

game_images = [rock, paper, scissors]

user_choice = int(input("Choose your weapon! Type 0 for Rock 🪨, 1 for Paper 📄, or 2 for Scissors ✂️:\n"))

# Check if the user input is valid before accessing the list

if user_choice >= 3 or user_choice < 0:

print("You typed an invalid number. Please choose 0, 1, or 2 next time. You lose!")

else:

print("You chose:")

print(game_images[user_choice])

computer_choice = random.randint(0, 2)

print("Computer chose:")

print(game_images[computer_choice])

if user_choice == 0 and computer_choice == 2:

print("Rock crushes Scissors. You win! 🎉")

elif computer_choice == 0 and user_choice == 2:

print("Rock crushes Scissors. You lose. 😢")

elif computer_choice > user_choice:

print(f"{game_images[computer_choice]} beats {game_images[user_choice]}. You lose. 😢")

elif user_choice > computer_choice:

print(f"{game_images[user_choice]} beats {game_images[computer_choice]}. You win! 🎉")

elif computer_choice == user_choice:

print("It's a draw. 🤝")

Key Functions Explained

1. User Input Handling

The game starts by asking the player to choose their weapon. The player's choice is captured as an integer and checked to ensure it's within the valid range (0, 1, or 2).

user_choice = int(input("Choose your weapon! Type 0 for Rock 🪨, 1 for Paper 📄, or 2 for Scissors ✂️:\n"))

# Check if the user input is valid before accessing the list

if user_choice >= 3 or user_choice < 0:

print("You typed an invalid number. Please choose 0, 1, or 2 next time. You lose!")

2. Displaying Choices

The player's choice and the computer's choice are displayed using ASCII art. This adds a visual element to the text-based game, making it more engaging.

else:

print("You chose:")

print(game_images[user_choice])

computer_choice = random.randint(0, 2)

print("Computer chose:")

print(game_images[computer_choice])

3. Determining the Winner

The game logic determines the winner based on the classic rules of Rock, Paper, Scissors. It compares the player's choice with the computer's choice and prints the result.

if user_choice == 0 and computer_choice == 2:

print("Rock crushes Scissors. You win! 🎉")

elif computer_choice == 0 and user_choice == 2:

print("Rock crushes Scissors. You lose. 😢")

elif computer_choice > user_choice:

print(f"{game_images[computer_choice]} beats {game_images[user_choice]}. You lose. 😢")

elif user_choice > computer_choice:

print(f"{game_images[user_choice]} beats {game_images[computer_choice]}. You win! 🎉")

elif computer_choice == user_choice:

print("It's a draw. 🤝")

Example

Let's say the player chooses:

  • Rock (0)

The program will output something like:

Choose your weapon! Type 0 for Rock 🪨, 1 for Paper 📄, or 2 for Scissors ✂️:

0

You chose:

_______

---' ____)

(_____)

(_____)

(____)

---.__(___)

Computer chose:

_______

---' ____)____

______)

_______)

_______)

---.__________)

Paper covers Rock. You lose. 😢

Conclusion

This Rock-Paper-Scissors game project was a fun and interactive way to practice basic Python concepts like user input, control flow, and randomization. I hope you enjoy playing the game as much as I enjoyed creating it. Stay tuned for more exciting projects in the upcoming days!

Stay Connected

Follow my progress and join me on this coding journey:

Happy coding!

Previous
Previous

100 Days of Code: Day 5 - Password Generator

Next
Next

100 Days of Code: Day 3 - Cyber-Security-Breach Maze Game