import cards import random def shuffledDeck(): """ returns a list containing 2 combined shuffled decks """ print "Shuffling." shuffled = [] unshuffled = [] #create the unshuffled deck for i in range(2): unshuffled += cards.deck() #randomly choose cards to move into the shuffled deck while unshuffled != []: card = random.randint(0, len(unshuffled)-1) shuffled.append(unshuffled[card]) del unshuffled[card] return shuffled def dealHands(deck): """returns numPlayers hands""" #the hands will be stored in a list hands = [[],[]] #the loops place the 1st and 3rd card in the first hand, 2nd and 4th in the other for i in range(2): for player in range(2): (hands[player]).append(deck[0]) del deck[0] return hands def playRound(deck): """Play one round between the dealer and player""" hands = dealHands(deck) playerHand = hands[0] dealerHand = hands[1] print "The dealer has a " + dealerHand[0].longname() + " showing.\n" playHand(playerHand,deck) if handValue(playerHand) > 21: print "You lose a point." return -1 else: print playDealerHand(dealerHand,deck) if handValue(playerHand) == handValue(dealerHand): print "Push" return 0 elif handValue(dealerHand) > 21 or handValue(playerHand) > handValue(dealerHand): print "You win a point." return 1 else: print "You lose a point." return -1 def playHand(hand,deck): """The player plays the hand passed to the function""" choice = "" while handValue(hand) <= 21 and choice != "S": print "Your hand: " + cards.hand_string(hand) choice = "" #repeat the prompt until we get appropriate input while choice != "H" and choice != "S": choice = raw_input("Would you like to (H)it or (S)tand? ").upper() if choice == "H": hand.append(deck[0]) print "You drew the " + deck[0].longname() + "\n" del deck[0] if handValue(hand) > 21: print "BUST", print "Your hand has a value of " + str(handValue(hand)) return def playDealerHand(hand,deck): """Play hand by the dealer's rules""" print "The dealer has: " + cards.hand_string(hand) while handValue(hand) <= 16: hand.append(deck[0]) print "The dealer drew the " + deck[0].longname() del deck[0] if handValue(hand) > 21: print "BUST", print "The dealer's hand has a value of " + str(handValue(hand)) def handValue(hand): """returns the best value of a hand""" aceFound = False value = 0 for card in hand: rank = card.rank() if rank == "A": aceFound = True value = value + 1 else: #most ranks are integers try: value = value + int(rank) #However, except for the ace, the rest are worth ten (T,J,Q,Q) except: value = value + 10 high = value if aceFound: high = high + 10 if high <= 21: return high else: return value print "BlackJack" print "-------------" playerScore = 0 deck = shuffledDeck() cont = "Y" while cont == "Y": playerScore += playRound(deck) print "\nYour score is: " + str(playerScore) + "\n" cont = "" #repeat prompt until there is good input while cont != "Y" and cont != "N": cont = raw_input("Do you want to play another hand, (Y)es or (N)o? ").upper() #shuffle if necesarry if len(deck) < 52: deck = shuffledDeck()