Python:
import random
# Initialize scores
player_score = 0
opponent_score = 0
# Choices for the game
choices = ["rock", "paper", "scissors"]
def check_win(player_choice, opponent_choice):
"""
Determines if the player wins against the opponent.
Returns True if the player wins, False otherwise.
"""
if player_choice == opponent_choice:
return None # It's a tie
elif (player_choice == "rock" and opponent_choice == "scissors") or \
(player_choice == "scissors" and opponent_choice == "paper") or \
(player_choice == "paper" and opponent_choice == "rock"):
return True # Player wins
else:
return False # Opponent wins
def play(action, player_choice=None):
global player_score, opponent_score
match action:
case "attack":
if player_choice not in choices:
return "Invalid choice. Choose rock, paper, or scissors."
opponent_choice = random.choice(choices)
result = check_win(player_choice, opponent_choice)
if result is None:
return "It's a tie!"
elif result:
player_score += 1
return f"You win! Opponent chose {opponent_choice}. Your score: {player_score}, Opponent's score: {opponent_score}."
else:
opponent_score += 1
return f"You lose! Opponent chose {opponent_choice}. Your score: {player_score}, Opponent's score: {opponent_score}."
case "get score":
return f"Your score: {player_score}, Opponent's score: {opponent_score}."
case "reset score":
player_score = 0
opponent_score = 0
return "Scores have been reset."
case _:
return "Invalid action. Choose 'attack', 'get score', or 'reset score'."
# Example usage:
# print(play("attack", "rock"))
# print(play("get score"))
# print(play("reset score"))