# Import necessary libraries
from random import randint, choice

# Card class for Uno
class Card:
    def __init__(self, color, rank):
        self.color = color
        self.rank = rank
    
    def get_color(self):
        return self.color
    
    def get_rank(self):
        return self.rank

    def __str__(self):
        return f"{self.color} {self.rank}"

# Deck class for Uno
class Deck:
    def __init__(self):
        self.colors = ['red', 'yellow', 'green', 'blue']
        self.ranks = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Skip', 'Reverse', 'Draw Two']
        self.cards = [Card(color, rank) for color in self.colors for rank in self.ranks] * 2  # Two of each card

    def shuffle(self):
        for i in range(len(self.cards)-1, 0, -1):
            j = randint(0, i)
            self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
    
    def draw_card(self):
        return self.cards.pop() if self.cards else None

# Initialize Uno game variables
deck = Deck()
deck.shuffle()
player_hand = [deck.draw_card() for _ in range(7)]
dealer_hand = [deck.draw_card() for _ in range(7)]
discard_pile = [deck.draw_card()]

# Display initial hands
print("Initial Hands:")
print("Player Hand:", [str(card) for card in player_hand])
print("Dealer Hand:", [str(card) for card in dealer_hand])
print("Starting Card on Discard Pile:", discard_pile[-1])

# Function to check playable cards
def is_playable(card, top_card):
    return card.get_color() == top_card.get_color() or card.get_rank() == top_card.get_rank() or 'Draw' in card.get_rank()

# Function for player turn
def player_turn():
    global player_hand
    print("\nYour turn!")
    top_card = discard_pile[-1]
    playable_cards = [card for card in player_hand if is_playable(card, top_card)]
    
    if playable_cards:
        selected_card = playable_cards[0]  # For simplicity, auto-select the first playable card
        player_hand.remove(selected_card)
        discard_pile.append(selected_card)
        print(f"You played: {selected_card}")
    else:
        new_card = deck.draw_card()
        player_hand.append(new_card)
        print(f"No playable cards. You drew: {new_card}")

# Function for dealer turn
def dealer_turn():
    global dealer_hand
    print("\nDealer's turn!")
    top_card = discard_pile[-1]
    playable_cards = [card for card in dealer_hand if is_playable(card, top_card)]
    
    if playable_cards:
        selected_card = playable_cards[0]
        dealer_hand.remove(selected_card)
        discard_pile.append(selected_card)
        print(f"Dealer played: {selected_card}")
    else:
        new_card = deck.draw_card()
        dealer_hand.append(new_card)
        print("Dealer drew a card.")

# Main Uno game loop
def play_uno():
    print("\n--- Game Start ---")
    while player_hand and dealer_hand:
        player_turn()
        if not player_hand:
            print("\nCongratulations! You won!")
            break
        dealer_turn()
        if not dealer_hand:
            print("\nDealer wins! Better luck next time!")
            break
        print(f"\nCurrent top card: {discard_pile[-1]}")
        print("Your hand:", [str(card) for card in player_hand])
    
play_uno()

Initial Hands:
Player Hand: ['blue 9', 'yellow 3', 'green Reverse', 'red 5', 'yellow Draw Two', 'red 0', 'red Draw Two']
Dealer Hand: ['blue 1', 'red 2', 'blue 2', 'yellow 6', 'yellow Skip', 'green 5', 'red 2']
Starting Card on Discard Pile: blue 5

--- Game Start ---

Your turn!
You played: blue 9

Dealer's turn!
Dealer played: blue 1

Current top card: blue 1
Your hand: ['yellow 3', 'green Reverse', 'red 5', 'yellow Draw Two', 'red 0', 'red Draw Two']

Your turn!
You played: yellow Draw Two

Dealer's turn!
Dealer played: yellow 6

Current top card: yellow 6
Your hand: ['yellow 3', 'green Reverse', 'red 5', 'red 0', 'red Draw Two']

Your turn!
You played: yellow 3

Dealer's turn!
Dealer played: yellow Skip

Current top card: yellow Skip
Your hand: ['green Reverse', 'red 5', 'red 0', 'red Draw Two']

Your turn!
You played: red Draw Two

Dealer's turn!
Dealer played: red 2

Current top card: red 2
Your hand: ['green Reverse', 'red 5', 'red 0']

Your turn!
You played: red 5

Dealer's turn!
Dealer played: green 5

Current top card: green 5
Your hand: ['green Reverse', 'red 0']

Your turn!
You played: green Reverse

Dealer's turn!
Dealer drew a card.

Current top card: green Reverse
Your hand: ['red 0']

Your turn!
No playable cards. You drew: red 9

Dealer's turn!
Dealer played: green 6

Current top card: green 6
Your hand: ['red 0', 'red 9']

Your turn!
No playable cards. You drew: blue Draw Two

Dealer's turn!
Dealer drew a card.

Current top card: green 6
Your hand: ['red 0', 'red 9', 'blue Draw Two']

Your turn!
You played: blue Draw Two

Dealer's turn!
Dealer played: blue 2

Current top card: blue 2
Your hand: ['red 0', 'red 9']

Your turn!
No playable cards. You drew: green 1

Dealer's turn!
Dealer played: red 2

Current top card: red 2
Your hand: ['red 0', 'red 9', 'green 1']

Your turn!
You played: red 0

Dealer's turn!
Dealer played: red 0

Dealer wins! Better luck next time!