diff --git a/Rock Paper Scissor/README.md b/Rock Paper Scissor/README.md new file mode 100644 index 0000000..68411c0 --- /dev/null +++ b/Rock Paper Scissor/README.md @@ -0,0 +1,38 @@ +# ๐ŸŽฎ Rock Paper Scissors Game (Python) + +Welcome to the **Rock Paper Scissors** Game! +This is a fun console-based Python project that simulates the classic **Rock Paper Scissors** game between **two players**, with random moves, score tracking, and a final result declaration. + +This version is designed with **clean OOP principles**, **customizable rounds**, and **robust error handling**. + +--- + +## ๐Ÿง  Game Overview + +The rules of **Rock Paper Scissors** are simple: + +| Move | Beats | +| ---------- | ---------- | +| Rock ๐Ÿชจ | Scissor โœ‚๏ธ | +| Paper ๐Ÿ“„ | Rock ๐Ÿชจ | +| Scissor โœ‚๏ธ | Paper ๐Ÿ“„ | + +- If **both players** choose the **same move**, itโ€™s a **tie** โ€” both get 1 point. +- Otherwise, the player with the winning move earns **1 point**. +- After all rounds, the player with the **highest score** wins. + +--- + +## ๐Ÿš€ Features + +โœ… Two users can join with their own names +โœ… Customizable number of rounds (default = 5) +โœ… Random move generation for both players +โœ… Automatic round evaluation +โœ… Live score tracking +โœ… Final winner announcement +โœ… Comprehensive input validation + +--- + +## ๐Ÿงฉ Project Structure diff --git a/Rock Paper Scissor/main.py b/Rock Paper Scissor/main.py new file mode 100644 index 0000000..393f256 --- /dev/null +++ b/Rock Paper Scissor/main.py @@ -0,0 +1,131 @@ +import random + + +class RockPaperScissors: + def __init__(self): + self.user1 = "" + self.user1_score = 0 + self.user2 = "" + self.user2_score = 0 + self.rounds = 5 + + def my_new_user(self): + """ + Create 2 users with default 5 rounds. + Optionally allow changing the number of rounds. + """ + self.user1, self.user2 = input("Enter Username-").split() + + change = ( + input( + "Rock Paper Scissor starting...\n" + "5 rounds are going to be played. Want to change [y/n] - " + ) + .strip() + .lower() + ) + + if change not in ["y", "n"]: + raise ValueError("Invalid decision") + + if change == "y": + rounds = int(input("Enter number of rounds you want to play- ").strip()) + if isinstance(rounds, int): + if rounds < 1: + raise ValueError("Rounds can't be smaller than 1") + self.rounds = rounds + else: + raise ValueError("Invalid round number") + + def my_action(self): + """Generate random action from Rock, Paper, Scissor.""" + return random.choice(["Rock", "Paper", "Scissor"]) + + def increase_score(self, user): + """Increase score for the given user (user1 or user2).""" + if user == "user1": + self.user1_score += 1 + else: + self.user2_score += 1 + + def evaluate(self, user1_action, user2_action, round_no): + """ + Evaluate round result based on actions and + update the scores accordingly. + """ + print(f"\n**** Round {round_no} starts ****") + print(f"{self.user1} move {user1_action}," f"{self.user2} move {user2_action}") + + if user1_action == user2_action: + self.increase_score("user1") + self.increase_score("user2") + round_won = "is tied" + else: + if user1_action == "Rock": + if user2_action == "Paper": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + elif user1_action == "Paper": + if user2_action == "Scissor": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + else: + if user2_action == "Rock": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + self.increase_score(res) + + print( + f"This round {round_won}, score: " + f"{self.user1} {self.user1_score} -" + f" {self.user2} {self.user2_score}" + ) + + def serve_final_result(self): + """Display final result after all rounds are completed.""" + + print("\n**** Final Result ****") + if self.user1_score > self.user2_score: + + print( + f"{self.user1} won this game with score " + f"{self.user1_score} - {self.user2_score}" + ) + + elif self.user1_score < self.user2_score: + + print( + f"{self.user2} won this game with score " + f"{self.user2_score} - {self.user1_score}" + ) + + else: + + print( + f"This Game ties with score " f"{self.user1_score} - {self.user2_score}" + ) + + +try: + game_instance = RockPaperScissors() + game_instance.my_new_user() + for i in range(1, game_instance.rounds + 1): + user1_action = game_instance.my_action() + user2_action = game_instance.my_action() + game_instance.evaluate(user1_action, user2_action, i) + + game_instance.serve_final_result() +except Exception as e: + print(f"Error: {e}") diff --git a/Rock Paper Scissor/requirements.txt b/Rock Paper Scissor/requirements.txt new file mode 100644 index 0000000..88ce6eb --- /dev/null +++ b/Rock Paper Scissor/requirements.txt @@ -0,0 +1,51 @@ +# ============================== +# Project: Rock Paper Scissors Game +# Author: Anurodh Kumar (idabora) +# Description: A terminal-based Rock Paper Scissors game +# ============================== + +# ------------------------------ +# Core Requirements +# ------------------------------ +# Python Standard Library modules are used (no external libs) +# So no third-party dependencies needed for core logic + +# Random module (standard library) +# Used for generating random actions between Rock, Paper, Scissor +# import random + +# ------------------------------ +# Python Version +# ------------------------------ +# Ensure compatibility with modern Python versions +python_version >= 3.8 + +# ------------------------------ +# (Optional) Development Tools +# ------------------------------ + +# For formatting code (optional) +black==24.4.2 + +# For linting / code quality checks (optional) +flake8==7.1.1 + +# For static type checking (optional) +mypy==1.11.2 + +# For running unit tests (optional) +pytest==8.3.2 + +# For code coverage reporting during testing (optional) +pytest-cov==5.0.0 + +# ------------------------------ +# (Optional) CLI Enhancements +# ------------------------------ +# For better terminal interface (optional, not used by default) +rich==13.9.1 # Pretty console printing (optional) +colorama==0.4.6 # Colorized CLI text on Windows/Linux (optional) + +# ------------------------------ +# End of File +# ------------------------------ diff --git a/RockPaperScissors b/RockPaperScissors new file mode 100644 index 0000000..393f256 --- /dev/null +++ b/RockPaperScissors @@ -0,0 +1,131 @@ +import random + + +class RockPaperScissors: + def __init__(self): + self.user1 = "" + self.user1_score = 0 + self.user2 = "" + self.user2_score = 0 + self.rounds = 5 + + def my_new_user(self): + """ + Create 2 users with default 5 rounds. + Optionally allow changing the number of rounds. + """ + self.user1, self.user2 = input("Enter Username-").split() + + change = ( + input( + "Rock Paper Scissor starting...\n" + "5 rounds are going to be played. Want to change [y/n] - " + ) + .strip() + .lower() + ) + + if change not in ["y", "n"]: + raise ValueError("Invalid decision") + + if change == "y": + rounds = int(input("Enter number of rounds you want to play- ").strip()) + if isinstance(rounds, int): + if rounds < 1: + raise ValueError("Rounds can't be smaller than 1") + self.rounds = rounds + else: + raise ValueError("Invalid round number") + + def my_action(self): + """Generate random action from Rock, Paper, Scissor.""" + return random.choice(["Rock", "Paper", "Scissor"]) + + def increase_score(self, user): + """Increase score for the given user (user1 or user2).""" + if user == "user1": + self.user1_score += 1 + else: + self.user2_score += 1 + + def evaluate(self, user1_action, user2_action, round_no): + """ + Evaluate round result based on actions and + update the scores accordingly. + """ + print(f"\n**** Round {round_no} starts ****") + print(f"{self.user1} move {user1_action}," f"{self.user2} move {user2_action}") + + if user1_action == user2_action: + self.increase_score("user1") + self.increase_score("user2") + round_won = "is tied" + else: + if user1_action == "Rock": + if user2_action == "Paper": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + elif user1_action == "Paper": + if user2_action == "Scissor": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + else: + if user2_action == "Rock": + res = "user2" + round_won = f"won by {self.user2}" + else: + res = "user1" + round_won = f"won by {self.user1}" + + self.increase_score(res) + + print( + f"This round {round_won}, score: " + f"{self.user1} {self.user1_score} -" + f" {self.user2} {self.user2_score}" + ) + + def serve_final_result(self): + """Display final result after all rounds are completed.""" + + print("\n**** Final Result ****") + if self.user1_score > self.user2_score: + + print( + f"{self.user1} won this game with score " + f"{self.user1_score} - {self.user2_score}" + ) + + elif self.user1_score < self.user2_score: + + print( + f"{self.user2} won this game with score " + f"{self.user2_score} - {self.user1_score}" + ) + + else: + + print( + f"This Game ties with score " f"{self.user1_score} - {self.user2_score}" + ) + + +try: + game_instance = RockPaperScissors() + game_instance.my_new_user() + for i in range(1, game_instance.rounds + 1): + user1_action = game_instance.my_action() + user2_action = game_instance.my_action() + game_instance.evaluate(user1_action, user2_action, i) + + game_instance.serve_final_result() +except Exception as e: + print(f"Error: {e}")