Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

A proper Greeting

+2 votes
asked Mar 21, 2025 by Simon Lin (250 points)
import random

# Define the colors and special cards
colors = ['Red', 'Yellow', 'Green', 'Blue']
special_cards = ['Skip', 'Reverse', 'Draw Two', 'Wild', 'Wild Draw Four']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# Create the deck of cards
def create_deck():
    deck = []
    # Add number cards (0-9) for each color
    for color in colors:
        for num in numbers:
            deck.append(f'{num} of {color}')
        # Add special action cards
        for action in special_cards[:-2]:  # Skip, Reverse, Draw Two
            for _ in range(2):  # Two of each action card
                deck.append(f'{action} of {color}')
    
    # Add wild and wild draw four cards
    for _ in range(4):  # Four wild cards
        deck.append('Wild')
    for _ in range(4):  # Four wild draw four cards
        deck.append('Wild Draw Four')
    
    random.shuffle(deck)
    return deck

# Function to display the hand of a player
def display_hand(player_name, hand):
    print(f"\n{player_name}'s hand:")
    for i, card in enumerate(hand):
        print(f"{i+1}. {card}")

# Function to check if a card is playable
def is_playable(card, top_card):
    if 'Wild' in card:
        return True  # Wild cards are always playable
    if card.split()[-1] == top_card.split()[-1]:  # Matching color
        return True
    if card.split()[0] == top_card.split()[0]:  # Matching number
        return True
    return False

# Function to handle the turn of a player
def player_turn(player_name, hand, top_card, deck):
    display_hand(player_name, hand)
    print(f"\nTop card: {top_card}")
    
    # Ask player to play or draw
    while True:
        print(f"{player_name}'s turn:")
        choice = input(f"Choose a card to play or type 'draw' to draw a card: ").strip().lower()

        if choice == 'draw':
            if deck:
                drawn_card = deck.pop()
                hand.append(drawn_card)
                print(f"{player_name} drew a card: {drawn_card}")
                break
            else:
                print("Deck is empty! You can't draw.")
        elif choice.isdigit() and 1 <= int(choice) <= len(hand):
            card_to_play = hand[int(choice) - 1]
            if is_playable(card_to_play, top_card):
                hand.remove(card_to_play)
                print(f"{player_name} played: {card_to_play}")
                break
            else:
                print(f"Card {card_to_play} is not playable. Choose another card or draw.")
        else:
            print("Invalid input! Please choose a valid card or type 'draw'.")

    return hand, top_card

# Function to start the Uno game
def play_uno():
    deck = create_deck()
    player1_hand = [deck.pop() for _ in range(7)]
    player2_hand = [deck.pop() for _ in range(7)]

    top_card = deck.pop()  # The top card from the deck to start the game
    current_player = 1
    game_over = False

    print("Welcome to Uno!")
    print(f"Starting card: {top_card}\n")

    while not game_over:
        if current_player == 1:
            player1_hand, top_card = player_turn("Player 1", player1_hand, top_card, deck)
            if not player1_hand:
                print("Player 1 wins!")
                game_over = True
        else:
            player2_hand, top_card = player_turn("Player 2", player2_hand, top_card, deck)
            if not player2_hand:
                print("Player 2 wins!")
                game_over = True

        # Switch player turn
        current_player = 3 - current_player  # Toggle between 1 and 2

if __name__ == "__main__":
    play_uno()

1 Answer

+1 vote
answered Mar 25, 2025 by (170 points)
Very nice. Congrats!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...