From 6812921fe2ae463fb367c181559fbec137840bdd Mon Sep 17 00:00:00 2001 From: Ysgsvsv65 <190869366+ankitha-km@users.noreply.github.com> Date: Sat, 14 Feb 2026 18:43:20 +0530 Subject: [PATCH] Add Rock, Paper, Scissors game --- rock-paper-scissors/README.md | 80 +++++++++++++++ rock-paper-scissors/rock_paper_scissors.py | 110 +++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 rock-paper-scissors/README.md create mode 100644 rock-paper-scissors/rock_paper_scissors.py diff --git a/rock-paper-scissors/README.md b/rock-paper-scissors/README.md new file mode 100644 index 0000000..1c67a25 --- /dev/null +++ b/rock-paper-scissors/README.md @@ -0,0 +1,80 @@ +# Rock, Paper, Scissors Game + +A simple command-line implementation of the classic Rock, Paper, Scissors game in Python. + +## Description + +Play the timeless game of Rock, Paper, Scissors against the computer! This beginner-friendly project demonstrates basic Python concepts including: +- Functions and modular code structure +- User input validation +- Random number generation +- Conditional logic +- Score tracking + +## How to Play + +1. Run the program +2. Enter your choice: `rock`, `paper`, or `scissors` +3. The computer will randomly select its choice +4. The winner is determined by the classic rules: + - Rock beats Scissors + - Scissors beats Paper + - Paper beats Rock +5. Keep playing rounds until you type `quit` +6. Your score is tracked throughout the game! + +## Installation + +No external libraries required! Just Python 3.x. +```bash +python rock_paper_scissors.py +``` + +## Example Gameplay +``` +================================================== +Welcome to Rock, Paper, Scissors! +================================================== + +Game Rules: +- Rock beats Scissors +- Scissors beats Paper +- Paper beats Rock + +Enter your choice (rock/paper/scissors) or 'quit' to exit: rock + +You chose: rock +Computer chose: scissors +You win this round! 🎉 + +Score - You: 1 | Computer: 0 | Ties: 0 +``` + +## Features + +- ✅ Input validation +- ✅ Score tracking +- ✅ Multiple rounds +- ✅ Clean, readable code +- ✅ User-friendly interface + +## Learning Objectives + +This project is great for beginners learning: +- Python basics +- Control flow (if/else statements, loops) +- Functions and return values +- Dictionary data structures +- Random module usage + +## Contributing + +Feel free to fork this project and add your own improvements! Some ideas: +- Add a best-of-5 mode +- Include lizard and spock (Rock, Paper, Scissors, Lizard, Spock) +- Add ASCII art for each choice +- Create a graphical interface with tkinter + +## License + +This project is open source and available for educational purposes. \ No newline at end of file diff --git a/rock-paper-scissors/rock_paper_scissors.py b/rock-paper-scissors/rock_paper_scissors.py new file mode 100644 index 0000000..2f7a87b --- /dev/null +++ b/rock-paper-scissors/rock_paper_scissors.py @@ -0,0 +1,110 @@ +""" +Rock, Paper, Scissors Game +A simple command-line game where you play against the computer. +""" + +import random + + +def get_computer_choice(): + """Generate a random choice for the computer.""" + choices = ['rock', 'paper', 'scissors'] + return random.choice(choices) + + +def get_user_choice(): + """Get and validate the user's choice.""" + while True: + user_input = input("\nEnter your choice (rock/paper/scissors) or 'quit' to exit: ").lower() + + if user_input == 'quit': + return None + + if user_input in ['rock', 'paper', 'scissors']: + return user_input + + print("Invalid choice! Please enter 'rock', 'paper', or 'scissors'.") + + +def determine_winner(user_choice, computer_choice): + """Determine the winner of the round.""" + if user_choice == computer_choice: + return "tie" + + winning_combinations = { + 'rock': 'scissors', + 'scissors': 'paper', + 'paper': 'rock' + } + + if winning_combinations[user_choice] == computer_choice: + return "user" + else: + return "computer" + + +def display_result(user_choice, computer_choice, winner): + """Display the result of the round.""" + print(f"\nYou chose: {user_choice}") + print(f"Computer chose: {computer_choice}") + + if winner == "tie": + print("It's a tie!") + elif winner == "user": + print("You win this round! 🎉") + else: + print("Computer wins this round!") + + +def play_game(): + """Main game loop.""" + print("=" * 50) + print("Welcome to Rock, Paper, Scissors!") + print("=" * 50) + print("\nGame Rules:") + print("- Rock beats Scissors") + print("- Scissors beats Paper") + print("- Paper beats Rock") + + # Score tracking + user_score = 0 + computer_score = 0 + ties = 0 + + while True: + user_choice = get_user_choice() + + if user_choice is None: + break + + computer_choice = get_computer_choice() + winner = determine_winner(user_choice, computer_choice) + display_result(user_choice, computer_choice, winner) + + # Update scores + if winner == "user": + user_score += 1 + elif winner == "computer": + computer_score += 1 + else: + ties += 1 + + # Display current score + print(f"\nScore - You: {user_score} | Computer: {computer_score} | Ties: {ties}") + + # Display final results + print("\n" + "=" * 50) + print("Thanks for playing!") + print(f"Final Score - You: {user_score} | Computer: {computer_score} | Ties: {ties}") + + if user_score > computer_score: + print("Congratulations! You won overall! 🏆") + elif computer_score > user_score: + print("Computer won overall. Better luck next time!") + else: + print("It's an overall tie!") + print("=" * 50) + + +if __name__ == "__main__": + play_game() \ No newline at end of file