Skip to content

Commit 6fbf4c5

Browse files
authored
Merge pull request #19 from codeasarjun/rock_paper_scissors
Rock paper scissors
2 parents 77b6201 + c714c48 commit 6fbf4c5

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
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.
2+
3+
## Features
4+
5+
- Play multiple rounds against the computer.
6+
- Keep track of the score for both the user and the computer.
7+
- Input validation to ensure only valid choices are accepted.
8+
- Display the final result at the end of the game.
9+
10+
11+
## How to Play
12+
13+
### Start the Game
14+
15+
When you run the script, you'll be prompted to enter the number of rounds you wish to play.
16+
17+
### Make Your Choice
18+
19+
For each round, you will be asked to choose one of the following options: `rock`, `paper`, or `scissors`.
20+
21+
### Computer's Choice
22+
23+
The computer will randomly select its choice.
24+
25+
### Determine the Winner
26+
27+
The winner of each round is determined based on the classic rules of Rock, Paper, Scissors:
28+
29+
- **Rock** crushes **Scissors**
30+
- **Scissors** cuts **Paper**
31+
- **Paper** covers **Rock**
32+
33+
### View Scores
34+
35+
After each round, the scores will be updated and displayed.
36+
37+
### End of Game
38+
39+
At the end of all rounds, the final scores will be displayed, and the winner will be announced.
40+
41+
42+
```bash
43+
Welcome to Rock, Paper, Scissors!
44+
How many rounds would you like to play? 3
45+
Enter rock, paper, or scissors: rock
46+
Computer chose: paper
47+
Computer wins this round!
48+
Scores => You: 0 | Computer: 1
49+
...
50+
Congratulations! You won the game with a score of 2 to 1.
51+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import random
2+
3+
def get_computer_choice():
4+
choices = ['rock', 'paper', 'scissors']
5+
return random.choice(choices)
6+
7+
def determine_winner(user_choice, computer_choice):
8+
if user_choice == computer_choice:
9+
return 'draw'
10+
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
11+
(user_choice == 'paper' and computer_choice == 'rock') or \
12+
(user_choice == 'scissors' and computer_choice == 'paper'):
13+
return 'user'
14+
else:
15+
return 'computer'
16+
17+
def play_game():
18+
print("Welcome to Rock, Paper, Scissors!")
19+
20+
user_score = 0
21+
computer_score = 0
22+
rounds = int(input("How many rounds would you like to play? "))
23+
24+
for _ in range(rounds):
25+
user_choice = input("Enter rock, paper, or scissors: ").lower()
26+
if user_choice not in ['rock', 'paper', 'scissors']:
27+
print("Invalid choice, please choose rock, paper, or scissors.")
28+
continue
29+
30+
computer_choice = get_computer_choice()
31+
print(f"Computer chose: {computer_choice}")
32+
33+
winner = determine_winner(user_choice, computer_choice)
34+
35+
if winner == 'user':
36+
print("You win this round!")
37+
user_score += 1
38+
elif winner == 'computer':
39+
print("Computer wins this round!")
40+
computer_score += 1
41+
else:
42+
print("This round is a draw!")
43+
44+
print(f"Scores => You: {user_score} | Computer: {computer_score}")
45+
46+
if user_score > computer_score:
47+
print(f"Congratulations! You won the game with a score of {user_score} to {computer_score}.")
48+
elif user_score < computer_score:
49+
print(f"Sorry, you lost the game. Final score is You: {user_score} | Computer: {computer_score}.")
50+
else:
51+
print(f"The game is a draw with both scoring {user_score}.")
52+
53+
if __name__ == "__main__":
54+
play_game()

0 commit comments

Comments
 (0)