import random
import sqlite3

# Blackjack

points = {'A': 1, 'J': 10, 'Q': 10, 'K':10}
points.update({n: n for n in range(2, 11)})

def hand_score(hand):
    """Total score for a hand."""
    total = sum([points[card] for card in hand])
    if total <= 11 and 'A' in hand:
        return total + 10
    return total

db = sqlite3.Connection('cards.db')
sql = db.execute
sql('DROP TABLE IF EXISTS cards')
sql('CREATE TABLE cards(card, place, hand);')

def play(card, place, hand):
    """Play a card so that the player can see it."""
    sql('INSERT INTO cards VALUES (?, ?, ?)', (card, place, hand))
    db.commit()

def score(who):
    """Compute the hand score for the player or dealer."""
    cards = sql('SELECT * from cards where place = ?;', [who])
    return hand_score([card for card, place, hand in cards.fetchall()])

def bust(who):
    """Check if the player or dealer went bust."""
    return score(who) > 21

player, dealer = "Player", "Dealer"

def play_hand(deck, hand):
    """Play a hand of Blackjack."""
    play(deck.pop(), player, hand)
    play(deck.pop(), dealer, hand)
    play(deck.pop(), player, hand)
    hidden = deck.pop()

    while 'y' in input("Hit? ").lower():
        play(deck.pop(), player, hand)
        if bust(player):
            print(player, "went bust!")
            return

    play(hidden, dealer, hand)

    while score(dealer) < 17:
        play(deck.pop(), dealer, hand)
        if bust(dealer):
            print(dealer, "went bust!")
            return

    print(player, score(player), "and", dealer, score(dealer))

deck = list(points.keys()) * 4
random.shuffle(deck)
hand = 0
while len(deck) > 10:
    print('\nDealing...')
    play_hand(deck, hand)
    sql('UPDATE cards SET place="Discard";')
    hand += 1;