Skip to content

Commit 41c14c0

Browse files
Merge pull request #559 from yuvraj-k-singh/bug/rock-paper-game
fix: resolve unwanted object output and improve Rock Paper Scissors game structure
2 parents bb16f4a + 1a66368 commit 41c14c0

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
class Rock_Paper_Scissor:
1+
import random
2+
3+
class Rock_Paper_Scissors:
24
def __init__(self):
5+
"""Initializes the game state without blocking execution."""
36
self.user_score = 0
47
self.computer_score = 0
58
self.rounds_played = 0
6-
self.play_game()
9+
# Perfectly mirrors the JS choices array ['rock', 'paper', 'scissors']
10+
self.choices = ["rock", "paper", "scissors"]
711

812
def users_play(self):
9-
import random
10-
choices = ["rock", "paper", "scissor"]
11-
13+
"""Handles a single round of interaction."""
1214
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.")
15+
while user_choice not in self.choices:
16+
user_choice = input("Enter your choice (rock, paper, or scissors): ").lower()
17+
if user_choice not in self.choices:
18+
print("Invalid choice. Please choose rock, paper, or scissors.")
1719

18-
computer_choice = random.choice(choices)
20+
computer_choice = random.choice(self.choices)
1921
print(f"Computer chose: {computer_choice}")
2022

2123
if user_choice == computer_choice:
22-
print("It's a Tie!")
24+
print("It's a Tie! 🤝")
2325
return "tie"
24-
elif (user_choice == "rock" and computer_choice == "scissor") or \
26+
elif (user_choice == "rock" and computer_choice == "scissors") or \
2527
(user_choice == "paper" and computer_choice == "rock") or \
26-
(user_choice == "scissor" and computer_choice == "paper"):
27-
print("You Win this round!")
28+
(user_choice == "scissors" and computer_choice == "paper"):
29+
print("You Win this round! 🎉")
2830
return "user"
2931
else:
30-
print("Computer Wins this round!")
32+
print("Computer Wins this round! 🤖")
3133
return "computer"
3234

33-
3435
def statistics(self):
36+
"""Displays performance statistics matching the web dashboard metrics."""
3537
print("\n--- Game Statistics ---")
3638
print(f"Rounds Played: {self.rounds_played}")
3739
print(f"Your Score: {self.user_score}")
3840
print(f"Computer Score: {self.computer_score}")
3941

40-
4142
def save_game(self):
43+
"""Appends the final game results to a local tracking log."""
4244
name = input("Enter your name to save the results (optional): ")
4345
if not name:
4446
name = "Anonymous"
@@ -50,27 +52,29 @@ def save_game(self):
5052
except IOError:
5153
print("Error: Could not save game results to file.")
5254

53-
5455
def play_game(self):
56+
"""Launches the primary interactive gameplay loop."""
5557
print("Welcome to Rock, Paper, Scissors!")
5658
while True:
5759
self.rounds_played += 1
5860
print(f"\n--- Round {self.rounds_played} ---")
5961

60-
round_winner = self.users_play() # Call the users_play method for one round
62+
round_winner = self.users_play()
6163

6264
if round_winner == "user":
6365
self.user_score += 1
6466
elif round_winner == "computer":
6567
self.computer_score += 1
6668

6769
self.statistics()
68-
play_again_input = input("do you want to play again ? (yes/no): ").lower()
70+
play_again_input = input("Do you want to play again? (yes/no): ").lower()
6971
if play_again_input != "yes":
7072
print("\nThanks for playing! Final results:")
7173
self.statistics()
7274
self.save_game()
7375
break
74-
75-
game = Rock_Paper_Scissor()
76-
print(game)
76+
77+
# Standard execution block ensuring clean instantiation
78+
if __name__ == "__main__":
79+
game = Rock_Paper_Scissors()
80+
game.play_game()

0 commit comments

Comments
 (0)