diff --git a/projects/rock-paper-scissors/README.md b/projects/rock-paper-scissors/README.md new file mode 100644 index 0000000..2103ed2 --- /dev/null +++ b/projects/rock-paper-scissors/README.md @@ -0,0 +1,51 @@ +This is a simple Python-based implementation of the classic game where you play against the computer. The game includes features like scorekeeping and multiple rounds. + +## Features + +- Play multiple rounds against the computer. +- Keep track of the score for both the user and the computer. +- Input validation to ensure only valid choices are accepted. +- Display the final result at the end of the game. + + +## How to Play + +### Start the Game + +When you run the script, you'll be prompted to enter the number of rounds you wish to play. + +### Make Your Choice + +For each round, you will be asked to choose one of the following options: `rock`, `paper`, or `scissors`. + +### Computer's Choice + +The computer will randomly select its choice. + +### Determine the Winner + +The winner of each round is determined based on the classic rules of Rock, Paper, Scissors: + +- **Rock** crushes **Scissors** +- **Scissors** cuts **Paper** +- **Paper** covers **Rock** + +### View Scores + +After each round, the scores will be updated and displayed. + +### End of Game + +At the end of all rounds, the final scores will be displayed, and the winner will be announced. + + +```bash +Welcome to Rock, Paper, Scissors! +How many rounds would you like to play? 3 +Enter rock, paper, or scissors: rock +Computer chose: paper +Computer wins this round! +Scores => You: 0 | Computer: 1 +... +Congratulations! You won the game with a score of 2 to 1. +``` \ No newline at end of file diff --git a/projects/rock-paper-scissors/rock_paper_scissors.py b/projects/rock-paper-scissors/rock_paper_scissors.py new file mode 100644 index 0000000..5583a54 --- /dev/null +++ b/projects/rock-paper-scissors/rock_paper_scissors.py @@ -0,0 +1,54 @@ +import random + +def get_computer_choice(): + choices = ['rock', 'paper', 'scissors'] + return random.choice(choices) + +def determine_winner(user_choice, computer_choice): + if user_choice == computer_choice: + return 'draw' + elif (user_choice == 'rock' and computer_choice == 'scissors') or \ + (user_choice == 'paper' and computer_choice == 'rock') or \ + (user_choice == 'scissors' and computer_choice == 'paper'): + return 'user' + else: + return 'computer' + +def play_game(): + print("Welcome to Rock, Paper, Scissors!") + + user_score = 0 + computer_score = 0 + rounds = int(input("How many rounds would you like to play? ")) + + for _ in range(rounds): + user_choice = input("Enter rock, paper, or scissors: ").lower() + if user_choice not in ['rock', 'paper', 'scissors']: + print("Invalid choice, please choose rock, paper, or scissors.") + continue + + computer_choice = get_computer_choice() + print(f"Computer chose: {computer_choice}") + + winner = determine_winner(user_choice, computer_choice) + + if winner == 'user': + print("You win this round!") + user_score += 1 + elif winner == 'computer': + print("Computer wins this round!") + computer_score += 1 + else: + print("This round is a draw!") + + print(f"Scores => You: {user_score} | Computer: {computer_score}") + + if user_score > computer_score: + print(f"Congratulations! You won the game with a score of {user_score} to {computer_score}.") + elif user_score < computer_score: + print(f"Sorry, you lost the game. Final score is You: {user_score} | Computer: {computer_score}.") + else: + print(f"The game is a draw with both scoring {user_score}.") + +if __name__ == "__main__": + play_game()