import random def rock(lst, n): for _ in range(n): lst.append("Rock") return lst def paper(lst, n): for _ in range(n): lst.append("Paper") return lst def scissors(lst, n): for _ in range(n): lst.append("Scissors") return lst def build_deck(): cards = [rock, paper, 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 give_cards(deck): """Give 3 cards to both player and AI, removing them from the deck.""" return [deck.pop(0) for _ in range(3)], [deck.pop(0) for _ in range(3)] def battle(user_card, ai_card): """Returns 1 if user wins, -1 if AI wins, 0 for a tie.""" 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(user_hand, ai_hand): """Handles playing cards until a winner is determined or cards run out.""" while user_hand and ai_hand: # Keep playing until hands are empty 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_choice = random.randint(0, len(ai_hand) - 1) ai_card = ai_hand.pop(ai_choice) print(f"You played: {user_card}, AI played: {ai_card}") result = battle(user_card, ai_card) if result == 1: print("You win this round!") return 1 # User wins elif result == -1: print("AI wins this round!") return -1 # AI wins else: print("It's a tie! Play again with remaining cards.") print("No cards left, this round is a draw.") return 0 # Round is a draw def play_game(best_of = 3): """Main game loop.""" my_deck = build_deck() user_score, ai_score = 0, 0 rounds_to_win = (best_of // 2) + 1 while user_score < rounds_to_win and ai_score < rounds_to_win and len(my_deck) >= 6: user_hand, ai_hand = give_cards(my_deck) round_result = play_cards(user_hand, ai_hand) if round_result == 1: user_score += 1 elif round_result == -1: ai_score += 1 print(f"Score - You: {user_score}, AI: {ai_score}") if user_score > ai_score: print("\nšŸŽ‰ You won the match!") else: print("\nšŸ¤– AI won the match!") # Run the game play_game(best_of = 5)