#                          BlackJack                                #
#                                                                   #
#                     Created by Tjampman                           #
#                                                                   #
#####################################################################
#                                                                   #
#                   BlackJack ver. 0.2 Alpha                        #
#   You can read version history in 'BlackJack Ducumentation.txt'   #
#                                                                   #
#####################################################################
# importing modules
import random
from time import sleep

# Defiened funcions
def hit_or_stand():
    print "Do you want to\n1 hit or\n2 stand?"

def shuffle():
    """deck = shuffle -> deck will be created,\nand
    consist of 52 cards of 4 colours."""
    return [[ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king],
            [ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king],
            [ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king],
            [ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king]]


def deal(hand, deck):
    """deal(hand, deck) -> one card will be cut from
    the deck\nand copied to the hand."""
    # We need a colour and a value using
    # random.randrange to get a random card from
    # the deck
    colour = random.randrange(0, len(deck))
    if len(deck[colour]) == 0:
        del deck[colour]
        colour = random.randrange(0, len(deck))
            
    value = random.randrange(0, len(deck[colour]))
    # Adding the card to hand
    hand.append(deck[colour][value])
    # And deleting the card from the deck
    del(deck[colour][value])

def sum_(hand, t=0, i=0):
    """sum_(hand) -> returns the value that the
    player holds"""
    for x in hand:
	t += x  # This counts the value of the hand, counting ace as 11
	if x == ace:
	    i = i + 1   #Counts aces in hand
    if t == 21 and len(hand) == 2:
	    return "BlackJack"
    elif i == 0:    # If no ace's: do this
	if len(hand) == 5 and t <= 21:
            return 21   # If you hold five cards, it counts as 21 but > BJ
	else:
	    return t
    elif i > 0:     # If you hold ace(s) the sum must be calculated differently
        # 'Hard sum' is when you have no ace or ace(s) counts as 1
	hard = t - i*10
	# 'Soft sum' is the sum of hand carrying an ace with the value of 11
	soft = t - (i-1)*10 # Hence 'i' is one less then with hard sum
	if len(hand) == 5 and hard <= 21:
	    return 21	
	elif t > 21 and soft > 21:
	    return hard
	else:
	    return hard, soft
    else:
	return "Out of Bounds"

def count(deck, i=0):
    """count(deck) -> returns the amount of cards in the deck."""
    for x in deck:
        i += len(x)
    return i
        
ace = 11
jack = queen = king = 10

deck_cards = shuffle()
game = "1"

while game != "2":
    if game == "1":
        hand = []
        
        if count(deck_cards) < 5:
            deck_cards = shuffle()
            print "Shufling"
            print "."
            sleep(1)
            print "."
            sleep(1)
            print "."
        deal(hand, deck_cards)
        deal(hand, deck_cards)
        if type(sum_(hand))== tuple:
            print "You hold:", hand, "sum: %s/%s" % (sum_(hand)[0], sum_(hand)[1])
        else:
            print "you hold:", hand, "sum: %s" % (sum_(hand))

        if sum_(hand) != "BlackJack":
            hit_or_stand()
            play = raw_input()

            while play != "2":
                if play == "1":
                    deal(hand, deck_cards)
                    if sum_(hand) > 21 and type(sum_(hand)) == int:
                        print "You busted with", hand, "sum: %s" % (sum_(hand))
                        break
                                
                    if type(sum_(hand))== tuple:
                        print "You hold:", hand, "sum: %s/%s" % (sum_(hand)[0], sum_(hand)[1])
                    else:
                        print "You hold:", hand, "sum: %s" % (sum_(hand))
                    hit_or_stand()
                    play = raw_input()
                    
                else:
                    hit_or_stand()
                    play = raw_input()
                    
        print len(deck_cards), "colours left"
        print count(deck_cards), "cards left"
        # blocked out for testing
        #game = raw_input("Play again?\n1 Yes\n2 No\n")
    else:
        game = raw_input("Play again?\n1 Yes\n2 No\n")
print "end"

