I am in the end part of a Python Gui game build; it is going quite well but I am finding that I underestimated one area of the logic, incorrectly coding it: The game is blackjack.

One area that, although it seemed simple on the surface (and in fact it probably is not as difficult as I am making it) is to monitor Aces that may be in one hand of the game. I will not go into the details of the game of blackjack but I will explain the basics concerning Aces.

  • They have a value of "1" by default
  • Aces can be played as either as "1" or "11" depending on which suits the player better without going over "21"-- that is, they can and do switch value from each card dealt depending on the other cards

I am writing code that first determines if a card is specifically an Ace. I then need to have the program decide whether or not to make the Ace a "1" or an "11". This is simple when dealing with one Ace but if you have more than one Ace it gets trickier. You must, of course, determine how many Aces you have and what their value should be depending on every other card. At this point, I check for an Ace after each new card is dealt then I try to determine its proper value, whatever creates the best hand without going over 21.

I have written a lot of code (I will not post any at this point because it is a mess and it only works in part, that is if you have only two cards dealt, it can sometimes know what to do if one card is a "5" and one card is an Ace: the total at that point would and should be "16" (11 + 5). Add a new Ace,(dealt card 3) and it may or may not react correctly.

I realize the algo for this must be somewhat simple, but I am going in circles at this point. I seemed to have it working well earlier (I am only coding and testing for the first 3 dealt cards at this point seeing that I can simply clone and update this code for the next, dealt cards); I took just this Ace section out of the program code, created a simplified file without the GUI\animation and ran it all night. This worked until I placed it back into the real program then... instant bugs concerning Ace recognition and value manipulation.

Has anyone ever dealt with this sort of thing. I know I will figure out it alone but I could use some help.

Thank you in advance,
sharky_machine

I wrote a blackjack also, but was informed by one of my students that I put in a poker-like betting system instead of the blackjack betting system. :o

Anyways, here's what I did with the ace problem:

class Card(object):

   RANKS = ['A','2','3','4','5','6','7','8','9','T','J','Q','K']
   VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, \
                    '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
   
   # stuff
   
   def get_value(self):
        return Card.VALUES[self.rank]

   value = property(get_value)

class Hand(object):
    
    def __init__(self):
        self.cards = []
        # etc.

   def get_value(self):

       value = sum([x.value for x in self.cards])
       if value > 21:
           ace_count  = [x.rank for x in self.cards].count('A')
           while ace_count > 0 and value > 21:
                 value -= 10
                 ace_count -= 1
       return value

It's my closest approximation to the way that I would compute it with real-life cards.

Hope it helps,
Jeff

I like your solution, and I don't think there is any other way to handle the ace problem

but was informed by one of my students that I put in a poker-like betting system instead of the blackjack betting system

They always know more than we do. Viva la future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.