"""File: play_cards1.py Purpose: Implement some functions that can be used in a program that plays blackjack Run: python play_cards1.py Input: Number of hands Output: Each player's hand. (This shows the dealer's hole card.) Note: Cards are represented as tuples consisting of two strings strings: Ranks are 'A', '2', . . . , '10', 'J', 'Q', 'K' Suits are 'C', 'D', 'H', 'S' """ from random import randint from sys import exit #---------------------------------------------------------------------- def print_card(card): """Print a single card""" print '(', card[0], ',', card[1], ')', #---------------------------------------------------------------------- def create_deck(): """Create a deck of cards ranks is a list of the ranks suits is a list of the suits""" ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] suits = ['C', 'D', 'H', 'S'] deck = [] for suit in suits: for rank in ranks: card = (rank, suit) deck.append(card) return deck #---------------------------------------------------------------------- def shuffle_deck(deck): """Shuffle the deck by swapping pairs of cards""" for i in range(51): # i ranges from 0 to 50 j = randint(i+1,51) # j ranges from i+1 to 51 deck[i], deck[j] = deck[j], deck[i] #---------------------------------------------------------------------- def print_deck(deck): """Print a deck of cards""" for card in deck: print_card(card) print #---------------------------------------------------------------------- def print_hand(hand): """Print the cards in a hand""" for card in hand: print_card(card) print #---------------------------------------------------------------------- def deal_card(deck): """Deal one card from the deck""" if len(deck) == 0: print "Trying to deal from empty deck!" print "Bye" exit() card = deck[0] del deck[0] return card #---------------------------------------------------------------------- def eval_blackjack_hand(hand, rank_vals): """Return a list whose elements are the possible values of the blackjack hand, hand. rank_vals is a dictionary with values of the various ranks, An ace is stored as 1""" val = 0 ace_count = 0 for card in hand: rank = card[0] card_val = rank_vals[rank] if card_val > 1: val = val + card_val else: val = val + 1 ace_count = ace_count+1 val_list = [val] for i in range(ace_count): val_list.append(val + (i+1)*10) return val_list #---------------------------------------------------------------------- def deal_start_hands(players, deck): """Deal two cards to each player""" # Create an empty hand for each player hands = [] for player in range(players): hands.append([]) for which_card in range(2): for player in range(players): card = deal_card(deck) hands[player].append(card) return hands #---------------------------------------------------------------------- def show_hands(hands, show_hole_card): """Show each hand, hiding the hole card of the dealer if show_hole_card is False""" for player in range(len(hands)-1): print player, ": ", print_hand(hands[player]) print "Dealer: ", if not show_hole_card: hand = hands[len(hands)-1] print '( X , X )', p_hand = hand[1:len(hand)] print_hand(p_hand) else: print_hand(hands[len(hands)-1]) #---------------------------------------------------------------------- if __name__ == "__main__": rank_vals = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':10, 'Q':10, 'K':10} deck = create_deck() # print "Original deck:" # print_deck(deck) # print shuffle_deck(deck) # print "Shuffled deck:" # print_deck(deck) players = int(raw_input("How many players?\n ")) hands = deal_start_hands(players, deck) print "The hands are:" show_hands(hands, True)