Skip to content

Commit fe4bf0a

Browse files
Merge pull request steam-bell-92#425 from sangitabera/refactoring_rps_game
added OOP concept in RPS project
2 parents 265bfd7 + 426e5a3 commit fe4bf0a

1 file changed

Lines changed: 65 additions & 38 deletions

File tree

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,76 @@
1-
import random
1+
class Rock_Paper_Scissor:
2+
def __init__(self):
3+
self.user_score = 0
4+
self.computer_score = 0
5+
self.rounds_played = 0
6+
self.play_game()
27

8+
def users_play(self):
9+
import random
10+
choices = ["rock", "paper", "scissor"]
11+
12+
user_choice = ""
13+
while user_choice not in choices:
14+
user_choice = input("Enter your choice (rock, paper, or scissor): ").lower()
15+
if user_choice not in choices:
16+
print("Invalid choice. Please choose rock, paper, or scissor.")
317

4-
print("🎮 Rock, Paper, Scissors Game! 🎮")
5-
print("🪨 Rock beats ✂️ Scissors")
6-
print("📄 Paper beats 🪨 Rock")
7-
print("✂️ Scissors beats 📄 Paper\n")
18+
computer_choice = random.choice(choices)
19+
print(f"Computer chose: {computer_choice}")
820

9-
Flag = True
21+
if user_choice == computer_choice:
22+
print("It's a Tie!")
23+
return "tie"
24+
elif (user_choice == "rock" and computer_choice == "scissor") or \
25+
(user_choice == "paper" and computer_choice == "rock") or \
26+
(user_choice == "scissor" and computer_choice == "paper"):
27+
print("You Win this round!")
28+
return "user"
29+
else:
30+
print("Computer Wins this round!")
31+
return "computer"
1032

11-
valid = {
12-
'r': 1,
13-
'p': 2,
14-
's': 3,
15-
}
1633

17-
key = {
18-
1: 'Rock 🪨',
19-
2: 'Paper 📄',
20-
3: 'Scissors ✂️',
21-
}
34+
def statistics(self):
35+
print("\n--- Game Statistics ---")
36+
print(f"Rounds Played: {self.rounds_played}")
37+
print(f"Your Score: {self.user_score}")
38+
print(f"Computer Score: {self.computer_score}")
2239

2340

24-
while Flag:
25-
value = str(input('🎯 Choose - Rock(r), Paper(p), Scissors(s): ')).lower()
26-
computer = random.randint(1, 3)
41+
def save_game(self):
42+
name = input("Enter your name to save the results (optional): ")
43+
if not name:
44+
name = "Anonymous"
45+
result_string = f"Player: {name}, Final Score: {self.user_score} - {self.computer_score} (User-Computer), Rounds: {self.rounds_played}\n"
46+
try:
47+
with open("game_results.txt", "a") as f:
48+
f.write(result_string)
49+
print("Game results saved successfully.")
50+
except IOError:
51+
print("Error: Could not save game results to file.")
2752

28-
if value not in valid:
29-
print('❌ Invalid choice! Please enter r, p, or s. Try again.\n')
30-
continue
3153

32-
print(f'\n👤 You chose: {key[valid[value]]}')
33-
print(f'🤖 Computer chose: {key[computer]}\n')
54+
def play_game(self):
55+
print("Welcome to Rock, Paper, Scissors!")
56+
while True:
57+
self.rounds_played += 1
58+
print(f"\n--- Round {self.rounds_played} ---")
59+
60+
round_winner = self.users_play() # Call the users_play method for one round
3461

35-
if (valid[value] == 1 and computer == 2) or (valid[value] == 2 and computer == 3) or (valid[value] == 3 and computer == 1):
36-
print('😢 You lost!! Better luck next time!\n')
37-
elif valid[value] == computer:
38-
print("🤝 It's a Tie!! Great minds think alike!\n")
39-
else:
40-
print('🎉 You won!! Congratulations!\n')
62+
if round_winner == "user":
63+
self.user_score += 1
64+
elif round_winner == "computer":
65+
self.computer_score += 1
4166

42-
response = str(input('Continue playing? Yes(y) or No(n): ')).lower()
43-
44-
if response == 'y':
45-
Flag = True
46-
print()
47-
else:
48-
Flag = False
49-
print('\n👋 Thanks for playing! See you next time!\n')
67+
self.statistics()
68+
play_again_input = input("do you want to play again ? (yes/no): ").lower()
69+
if play_again_input != "yes":
70+
print("\nThanks for playing! Final results:")
71+
self.statistics()
72+
self.save_game()
73+
break
74+
75+
game = Rock_Paper_Scissor()
76+
print(game)

0 commit comments

Comments
 (0)