import random class RPSGame: def __init__(self, best_of = 3): self.deck = self.build_deck() self.user_score = 0 self.ai_score = 0 self.rounds_to_win = (best_of // 2) + 1 def build_deck(self): cards = [self.rock, self.paper, self.scissors] deck = [] random.shuffle(cards) for i in range(2): n = random.randint(5, 20) deck = cards[i](deck, n) missing_cards = 52 - len(deck) deck = cards[2](deck, missing_cards) random.shuffle(deck) return deck def rock(self, lst, n): lst.extend(["Rock"] * n) return lst def paper(self, lst, n): lst.extend(["Paper"] * n) return lst def scissors(self, lst, n): lst.extend(["Scissors"] * n) return lst def give_cards(self): return [self.deck.pop(0) for _ in range(3)], [self.deck.pop(0) for _ in range(3)] def battle(self, user_card, ai_card): if user_card == ai_card: return 0 if (user_card == "Rock" and ai_card == "Scissors") or \ (user_card == "Paper" and ai_card == "Rock") or \ (user_card == "Scissors" and ai_card == "Paper"): return 1 return -1 def play_cards(self, user_hand, ai_hand): while user_hand and ai_hand: print(f"\nYour cards: {user_hand}") while True: try: user_choice = int(input(f"Select a card (1-{len(user_hand)}): ")) - 1 if 0 <= user_choice < len(user_hand): break except ValueError: pass print("Invalid choice. Try again.") user_card = user_hand.pop(user_choice) ai_card = ai_hand.pop(random.randint(0, len(ai_hand) - 1)) print(f"You played: {user_card}, AI played: {ai_card}") result = self.battle(user_card, ai_card) if result == 1: print("You win this round!") return 1 elif result == -1: print("AI wins this round!") return -1 else: print("It's a tie! Play again with remaining cards.") print("No cards left, this round is a draw.") return 0 def play_game(self): while self.user_score < self.rounds_to_win and self.ai_score < self.rounds_to_win and len(self.deck) >= 6: user_hand, ai_hand = self.give_cards() round_result = self.play_cards(user_hand, ai_hand) if round_result == 1: self.user_score += 1 elif round_result == -1: self.ai_score += 1 print(f"Score - You: {self.user_score}, AI: {self.ai_score}") if self.user_score > self.ai_score: print("\nšŸŽ‰ You won the match!") else: print("\nšŸ¤– AI won the match!") if __name__ == "__main__": game = RPSGame(best_of = 3) game.play_game()