Trotaner
Trotaner

Reputation: 21

Python Blackjack Count

Not so much a problem or question, just wanted to know how other people would approach this. I'm working in python to make a blackjack game through python's class structure and I've made the deck an array with the cards as strings. This helps with the fact that 4 cards are worth 10 in blackjack and an Ace can be worth 1 or 11. But, calculating a hand's value is hard. The deck is in the init. How could this be better? I considered a dictionary but that doesn't handle duplicates. Any thoughts are appreciated. Sorry if this is a bad post, I'm new here.

    self.deck = [['2']*4, ['3']*4, ['4']*4, ['5']*4, ['6']*4, ['7']*4, \
                ['8']*4, ['9']*4, ['10']*4, ['J']*4, ['Q']*4, ['K']*4, \
                ['A']*4]



    def bust(self, person):

      count = 0
      for i in self.cards[person]:
        if i == 'A':
            count += 1
        elif i == '2':
            count += 2
        elif i == '3':
            count += 3
        elif i == '4':
            count += 4
        elif i == '5':
            count += 5
        elif i == '6':
            count += 6

Upvotes: 2

Views: 2990

Answers (2)

ted
ted

Reputation: 14744

SOURCE

So here is what you can do :

Make your deck a string

import random 

cards = 'A'*4 + '2'*4 + ... + 'K'*4
self.deck = ''.join(random.sample(cards,len(cards)))
values = {'2': 2,
          '3': 3,
          '4': 4,
          '5': 5,
          '6': 6,
          '7': 7,
          '8': 8,
          '9': 9,
          'T': 10,
          'J': 10,
          'Q': 10,
          'K': 10
         }

Then you define a hand as a string and use a count method :

def counth(hand):
    """Evaluates the "score" of a given hand. """
    count = 0
    for i in hand:
        if i in values:
            count += values[i]
        else:
            pass
    for x in hand:
        if x == 'A':
        ## makes exception for aces
            if count + 11 > 21:
                count += 1
            elif hand.count('A') == 1:
                count += 11
            else:
                count += 1
        else:
            pass
    return count

Upvotes: 0

9000
9000

Reputation: 40894

Do yourself a favor, get an explicit map of card values:

CARD_VALUE = {
  '2': 2,
  '3': 3,
  # etc
  'A': 1,
  'J': 12,
  'Q': 13,
  'K': 14,
}

# Calculate the value of a hand;
# a hand is a list of cards.
hand_value = sum(CARD_VALUE[card] for card in hand)

For different games, you can have different value mappings, e.g. with Ace worth 1 or 11. You can put these mappings into a dictionary named by game's name.

Also, I'd not keep my hand representation as a simple list of cards. Instead I'd pack the repeating values using counts:

# Naive storage, even unsorted:
hand = ['2', '2', '3', '2', 'Q', 'Q']

# Grouped storage using a {card: count} dictionary:
hand = {'2': 3, '3': 1, 'Q': 2}
# Allows for neat operations
got_a_queen = 'Q' in hand
how_many_twos = hand['2']  # only good for present cards. 
how_many_fives = hand.get('5', 0)  # 0 returned since '5' not found.
hand_value = sum(CARD_VALUE(card) * hand[card] for card in hand)

Hope this helps.

Upvotes: 1

Related Questions