Skip to content

Rock paper scissors #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions projects/rock-paper-scissors/README.md
Original file line number Diff line number Diff line change
@@ -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.
```
54 changes: 54 additions & 0 deletions projects/rock-paper-scissors/rock_paper_scissors.py
Original file line number Diff line number Diff line change
@@ -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()