Here’s a more complex version of the program that includes additional features:
- The program keeps track of the user’s win/loss record and displays it at the end of each game.
- The program allows the user to choose between different game modes, including classic rock-paper-scissors, rock-paper-scissors-lizard-spock, and a custom mode where the user can define their own set of choices.
- The program allows the user to play against the computer or against another player.
- The program includes error handling to prevent the user from entering invalid input.
```
import random
class Game:
def init(self, mode):
self.mode = mode
self.choices = self.get_choices()
self.players = self.get_players()
self.scores = {player: 0 for player in self.players}
def get_choices(self):
if self.mode == "classic":
return ["rock", "paper", "scissors"]
elif self.mode == "lizard-spock":
return ["rock", "paper", "scissors", "lizard", "spock"]
elif self.mode == "custom":
choices = input("Enter your choices, separated by commas: ")
return [choice.strip() for choice in choices.split(",")]
def get_players(self):
while True:
num_players = input("Enter the number of players (1 or 2): ")
if num_players in ["1", "2"]:
break
else:
print("Invalid input. Please enter 1 or 2.")
if num_players == "1":
return ["player", "computer"]
else:
return ["player 1", "player 2"]
def get_player_choice(self, player):
while True:
choice = input(f"{player}, choose {self.mode}: ")
if choice in self.choices:
return choice
else:
print("Invalid input. Please choose one of the available options.")
def get_computer_choice(self):
return random.choice(self.choices)
def determine_winner(self, choices):
if choices[0] == choices[1]:
print("It's a tie!")
elif (choices[0] == "rock" and choices[1] == "scissors") or (choices[0] == "paper" and choices[1] == "rock") or (choices[0] == "scissors" and choices[1] == "paper"):
print(f"{self.players[0]} wins!")
self.scores[self.players[0]] += 1
else:
print(f"{self.players[1]} wins!")
self.scores[self.players[1]] += 1
def play_game(self):
for i in range(len(self.players)):
choices = []
if self.players[i] == "computer":
choices.append(self.get_computer_choice())
else:
choices.append(self.get_player_choice(self.players[i]))
print(f"{self.players[i]} chose {choices[0]}.")
if len(self.players) == 2:
choices.append(self.get_player_choice(self.players[(i+1)%2]))
print(f"{self.players[(i+1)%2]} chose {choices[1]}.")
else:
choices.append(self.get_computer_choice())
print(f"The computer chose {choices[1]}.")
self.determine_winner(choices)
print(f"Score: {self.scores[self.players[0]]} - {self.scores[self.players[1]]}")
print()
def display_results(self):
print("Final score:")
for player in self.players:
print(f"{player}: {self.scores[player]}")
if self.scores[self.players[0]] > self.scores[self.players[1]]:
print(f"{self.players[0]} wins!")
elif self.scores[self.players[0]] < self.scores[self.players[1]]:
print(f"{self.players[1]} wins!")
else:
print("It's a tie!")
def main():
print(“Welcome to the Rock-Paper-Scissors Game!”)
while True:
mode = input(“Choose a game mode (classic, lizard-spock, custom): “)
if mode in [“classic”, “lizard-spock”, “custom”]:
break
else:
print(“Invalid input. Please choose one of the available options.”)
game = Game(mode)
while True:
game.play_game()
game.display_results()
while True:
play_again = input(“Would you like to play again? (y/n): “)
if play_again.lower() == “y”:
game.scores = {player: 0 for player in game.players}
break
elif play_again.lower() == “n”:
print(“Thanks for playing!”)
return
else:
print(“Invalid input. Please enter y or n.”)
if name == “main”:
main()
```