import random

def draw_cards(number):
    cards = []
    for _i in range(0, number):
        card = random.randint(2,14)
        if card >= 12:
            card = 10
        if card == 11:
            card = "A"
        cards.append(card)
    return(cards)

def count_score(cards):
    score = sum(filter(lambda card: isinstance(card, int), cards))
    for _ in filter(lambda card: isinstance(card, str), cards):
        if score + 11 > 21:
            score += 1
        else:
            score += 11
    return(score)

def display_players_cards(cards, score):
    print(f'Your cards: {cards}, current score: {score}')

def display_cpu_cards(cards):
    print(f'Computer\'s first card: {cards[0]}')

def dealer_game(cards):
    cpu_score = count_score(cards)
    if cpu_score < 17:
        cards.extend(draw_cards(1))
        dealer_game(cards)

def compute_winner(player_cards, cpu_cards):
    player_score = count_score(player_cards)
    cpu_score = count_score(cpu_cards)

    display_players_cards(player_cards, player_score)
    display_players_cards(cpu_cards, cpu_score)

    if cpu_score > 21:
        print("Dealer went over 21 - You win!")
    elif cpu_score == player_score:
        print("It's a draw!")
    elif cpu_score > player_score:
        print("You lose!")
    else:
        print("You win")

def launch_new_game():
    wanna_play = input('Do you want to play again? Type "y" or "n":')

    if wanna_play == 'y':
        play_blackjack([], [])

def play_blackjack(player_cards, cpu_cards):
    if player_cards == []:
        player_cards = draw_cards(2)
        cpu_cards = draw_cards(2)
    else:
        player_cards.extend(draw_cards(1))

    player_score = count_score(player_cards)

    display_players_cards(player_cards,player_score)
    display_cpu_cards(cpu_cards)

    if player_score > 21:
        print("You went over 21 - You lose!")
        launch_new_game()

    action = input('Type "y" to get another card, type "n" to pass:')
    if action == 'y':
        play_blackjack(player_cards, cpu_cards)
    else:
        dealer_game(cpu_cards)
        compute_winner(player_cards, cpu_cards)
        launch_new_game()

wanna_play = input('Do you want to play a game of Blackjack? Type "y" or "n":')
if wanna_play == 'y':
    play_blackjack([], [])
