Back

Rock, Paper, Scissors

The code is a simple implementation of the classic "Rock, Paper, Scissors" game where a player competes against the computer, and the first to win two rounds is declared the winner.

Code Block

This script allows the user to play a "Rock, Paper, Scissors" game against the computer. The game continues until either the player or the computer wins two rounds. The player is prompted to choose between "rock," "paper," or "scissors," while the computer randomly selects one of these options. The script compares the choices to determine the round's winner and updates the score accordingly. The game announces the final winner once either the player or the computer reaches two wins.

Check out the Code

Code Analysis

Logic Correction:

  • There is an issue with the final winner announcement. The congratulatory message is printed after every round instead of after the game ends. It should be placed outside the while loop to announce the overall winner only after the game concludes.

print(f"Final Score - Player: {player_wins}, Computer: {computer_wins}")
if player_wins > computer_wins:
   print("Congratulations! You won the game.")
else:
   print("Computer won the game!")

Input Validation:

  • The current implementation does not handle invalid user input (e.g., if the player enters something other than "rock," "paper," or "scissors"). Adding input validation can make the game more robust:

while player_choice not in choices:
   player_choice = input("Invalid choice. Please choose rock, paper, or scissors: ").lower()

Refactoring for Reusability:

  • The game logic can be refactored into functions, making the code cleaner and more modular. This approach allows for easier maintenance and potential future expansions.

def get_winner(player_choice, computer_choice):
   if (player_choice == "rock" and computer_choice == "scissors") or \
      (player_choice == "scissors" and computer_choice == "paper") or \
      (player_choice == "paper" and computer_choice == "rock"):
       return "Player"
   elif player_choice == computer_choice:
       return "Tie"
   else:
       return "Computer"

Enhancing User Experience:

  • To improve the user experience, consider adding features such as allowing the player to quit the game early, providing a brief explanation of the rules before the game starts, or adding a best-of-five option.

if input("Do you want to play another round? (yes/no): ").lower() != "yes":
   break

Modularizing the Game Loop:

  • Separating the game loop from the rest of the code into its function can make the code more readable and maintainable:

def play_game():
   player_wins = 0
   computer_wins = 0
   print("Let's play rock, paper, or scissors")

   while player_wins < 2 and computer_wins < 2:
       player_choice = input("\nChoose rock, paper, or scissors: ").lower()
       choices = ["rock", "paper", "scissors"]
       if player_choice not in choices:
           print("Invalid choice, try again.")
           continue

       computer_choice = random.choice(choices)
       print(f"Computer chose: {computer_choice}")
       winner = get_winner(player_choice, computer_choice)

       if winner == "Player":
           print("You won this round!")
           player_wins += 1
       elif winner == "Computer":
           print("Computer won this round!")
           computer_wins += 1
       else:
           print("It's a tie.")

       print(f"Current Score - Player: {player_wins}, Computer: {computer_wins}")

   return player_wins, computer_wins

player_wins, computer_wins = play_game()
print(f"Final Score - Player: {player_wins}, Computer: {computer_wins}")
if player_wins > computer_wins:
   print("Congratulations! You won the game.")
else:
   print("Computer won the game!")

Expanding the Game:

  • The game could be expanded by adding more choices (like "lizard" and "spock" from "Rock, Paper, Scissors, Lizard, Spock") or keeping a running tally of wins and losses across multiple sessions.

Final Notes:

The code successfully implements the basic mechanics of "Rock, Paper, Scissors." By adding input validation, better structuring with functions, and improving the game loop, you can create a more robust and user-friendly game that’s easier to extend and maintain.