Python BlackJack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LolaT
    New Member
    • Jul 2007
    • 22

    Python BlackJack

    I am trying to develop a function that calculates the sum of a hand in BlackJack, however, I am having trouble assigning values to the Face cards. I am using the cards module for python 2.5. Also, the randomly selected cards are returned in the format '8C' for example, so I am given the rank and the suit, which also makes it difficult to evaluate the hand. I'm a python newbie, so if anyone has any helpful hints on how I can make this function work, please let me know!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by LolaT
    I am trying to develop a function that calculates the sum of a hand in BlackJack, however, I am having trouble assigning values to the Face cards. I am using the cards module for python 2.5. Also, the randomly selected cards are returned in the format '8C' for example, so I am given the rank and the suit, which also makes it difficult to evaluate the hand. I'm a python newbie, so if anyone has any helpful hints on how I can make this function work, please let me know!
    For the first part, we'll need some kind of example code. Read the REPLY GUIDELINES on the right hand side of the page while your are editing your reply to learn how to use CODE tags.

    For the second part, here's a suggestion:[CODE=python]
    >>> def evalBlackJackCa rd(card):
    ... try:
    ... value = int(card[0]) # the first character of the string
    ... except ValueError: # in this case it's a letter
    ... value = 10
    ... return value
    ...
    >>> cardValue = "8C"
    >>> print evalBlackJackCa rd(cardValue)
    8
    >>> cardValue = "KC"
    >>> print evalBlackJackCa rd(cardValue)
    10
    >>> [/CODE]

    Comment

    • LolaT
      New Member
      • Jul 2007
      • 22

      #3
      Originally posted by bartonc
      For the first part, we'll need some kind of example code. Read the REPLY GUIDELINES on the right hand side of the page while your are editing your reply to learn how to use CODE tags.

      For the second part, here's a suggestion:[CODE=python]
      >>> def evalBlackJackCa rd(card):
      ... try:
      ... value = int(card[0]) # the first character of the string
      ... except ValueError: # in this case it's a letter
      ... value = 10
      ... return value
      ...
      >>> cardValue = "8C"
      >>> print evalBlackJackCa rd(cardValue)
      8
      >>> cardValue = "KC"
      >>> print evalBlackJackCa rd(cardValue)
      10
      >>> [/CODE]

      Thank you for your suggestion, I have figured out how to give the Face cards a value of 10, however now my problem lies in the fact that I can't get an integer value for all of the cards in a hand. For example, if i have a hand consisting of ['KC', '9H'], I can only get the integer value of the first card in the hand. Here is my code, and if there are any further suggestions I would greatly appreciate them!

      [code=python]>>> import cards
      >>> import random
      >>> d=cards.deck()
      >>> D=d*2 #because we use 2 decks
      >>> userHand=[]
      >>> userHand.append (random.choice( D))
      >>> userHand.append (random.choice( D))
      >>> userHand=[userHand[0].shortname(),us erHand[1].shortname()] #to display just the rank and the suit
      >>> print userHand
      ['JC', '4C']
      >>> def evalCard(hand):
      for cards in hand:
      try:
      value=int(cards[0])
      except ValueError:
      value=10
      return value


      >>> evalCard(userHa nd)
      10[/code]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by LolaT
        Thank you for your suggestion, I have figured out how to give the Face cards a value of 10, however now my problem lies in the fact that I can't get an integer value for all of the cards in a hand. For example, if i have a hand consisting of ['KC', '9H'], I can only get the integer value of the first card in the hand. Here is my code, and if there are any further suggestions I would greatly appreciate them!
        This should work (untested):

        [code=python]
        def evalCard(hand):
        value = 0
        for cards in hand:
        try:
        value += int(cards[0])
        except ValueError:
        value += 10
        return value
        print evalCard(userHa nd)[/code]

        Comment

        • LolaT
          New Member
          • Jul 2007
          • 22

          #5
          Originally posted by bvdet
          This should work (untested):

          [code=python]
          def evalCard(hand):
          value = 0
          for cards in hand:
          try:
          value += int(cards[0])
          except ValueError:
          value += 10
          return value
          print evalCard(userHa nd)[/code]


          thanks it does work
          but i believe it also converts the ace into a 10
          the ace needs to be a 1 or an 11 though

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by LolaT
            thanks it does work
            but i believe it also converts the ace into a 10
            the ace needs to be a 1 or an 11 though
            All you need is one more conditional statement.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by LolaT
              thanks it does work
              but i believe it also converts the ace into a 10
              the ace needs to be a 1 or an 11 though
              Strings have lots of neat methods. One of my favorite is startswith():[CODE=python]
              >>> cardValue = "AH"
              >>> cardValue.start swith("A")
              True
              >>> [/CODE]I thought about the 1 or 11 thing at the onset. A little tricky, that one.

              Comment

              • BambiL
                New Member
                • Jul 2007
                • 2

                #8
                I'm trying to make a blackjack game with python too, and i'm trying to create a function which uses the unused cards of a shuffled deck to play.
                for example, the output should look something like:

                "The dealer has a three of hearts showing.

                Your hand: 8H QH
                Would you like to (H)it or (S)tand? s
                Your hand has a value of 18

                The dealer has: 3H 6S
                The dealer drew the nine of hearts
                The dealer's hand has a value of 18
                Push"


                i have no idea where to even begin :S

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by BambiL
                  I'm trying to make a blackjack game with python too, and i'm trying to create a function which uses the unused cards of a shuffled deck to play.
                  for example, the output should look something like:

                  "The dealer has a three of hearts showing.

                  Your hand: 8H QH
                  Would you like to (H)it or (S)tand? s
                  Your hand has a value of 18

                  The dealer has: 3H 6S
                  The dealer drew the nine of hearts
                  The dealer's hand has a value of 18
                  Push"


                  i have no idea where to even begin :S
                  Perhaps the Original Poster of this thread would like to share the source of the card class being used in this discussion. In the mean time, here is a basic start:[CODE=python]
                  >>> values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",]
                  >>> suits = ["H", "C", "D", "S"]
                  >>> deck = [(s1 + s2) for s1 in values for s2 in suits]
                  >>> deck
                  ['AH', 'AC', 'AD', 'AS', '2H', '2C', '2D', '2S', '3H', '3C', '3D', '3S', '4H', '4C', '4D', '4S', '5H', '5C', '5D', '5S', '6H', '6C', '6D', '6S', '7H', '7C', '7D', '7S', '8H', '8C', '8D', '8S', '9H', '9C', '9D', '9S', '10H', '10C', '10D', '10S', 'JH', 'JC', 'JD', 'JS', 'QH', 'QC', 'QD', 'QS', 'KH', 'KC', 'KD', 'KS']
                  >>> card = deck.pop(0)
                  >>> card
                  'AH'
                  >>> deck
                  ['AC', 'AD', 'AS', '2H', '2C', '2D', '2S', '3H', '3C', '3D', '3S', '4H', '4C', '4D', '4S', '5H', '5C', '5D', '5S', '6H', '6C', '6D', '6S', '7H', '7C', '7D', '7S', '8H', '8C', '8D', '8S', '9H', '9C', '9D', '9S', '10H', '10C', '10D', '10S', 'JH', 'JC', 'JD', 'JS', 'QH', 'QC', 'QD', 'QS', 'KH', 'KC', 'KD', 'KS']
                  >>> [/CODE]

                  Comment

                  • LolaT
                    New Member
                    • Jul 2007
                    • 22

                    #10
                    A friend taking a computing course gave me the link to download a cards module, which was given through the course website. If you are interested in the module, the following is the link to access it.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by LolaT
                      A friend taking a computing course gave me the link to download a cards module, which was given through the course website. If you are interested in the module, the following is the link to access it.

                      http://www.cs.sfu.ca/CC/120/ggbaker/.../assign3-cards
                      Thank you .

                      Comment

                      • LolaT
                        New Member
                        • Jul 2007
                        • 22

                        #12
                        Thank you to everyone for their helpful suggestions.
                        I am yet again having a problem with one of the functions.
                        [code=python]def play(D):
                        points=0

                        option=raw_inpu t('Would you like to (H)it or (S)tay? ')
                        if 's' or 'S' in option:
                        print "Your hand has a value of " + str(handValue(u serHand))
                        print dealerHand
                        while handValue(deale rHand)<16:
                        dealerHand.appe nd(random.choic e(D))
                        dealerHand=[dealerHand[0],dealerHand[1],dealerHand[2]]
                        print handValue(deale rHand)
                        if handValue(deale rHand)> handValue(userH and)and handValue(deale rHand)< 21:
                        print "The dealer's hand has a value of " + str(handValue(d ealerHand))
                        print "You lose a point."
                        points=points-1
                        print points
                        elif handValue(deale rHand)<handValu e(userHand)and handValue(userH and)<21:
                        print "You gain a point"
                        points=points+1
                        print points
                        elif handValue(deale rHand)>21:
                        print "Dealer busts"
                        print "You gain a point"
                        points=points+1
                        print points
                        elif handValue(userH and)==handValue (dealerHand):
                        print 'PUSH'


                        elif 'h' or 'H' in option:
                        userHand.append (random.choice( D))
                        if handValue(userH and)>21:
                        print 'Bust. Your hand has a value of ' + str(handValue(u serHand))
                        points=points-1
                        print 'You lose a point'
                        print points
                        elif handValue==21:
                        print "Your hand has a value of 21."
                        print dealerHand
                        while handValue(deale rHand)<16:
                        dealerHand.appe nd(random.choic e(D))
                        print handValue(deale rHand)
                        if handValue(deale rHand)> handValue(userH and)and handValue(deale rHand)< 21:
                        print "You lose a point."
                        points=points-1
                        print points
                        elif handValue(deale rHand)<handValu e(userHand)and handValue(userH and)<21:
                        print "You gain a point"
                        points=points+1
                        print points
                        elif dealerValue>21:
                        print "Dealer busts"
                        print "You gain a point"
                        points=points+1
                        print points

                        play=raw_input( 'Would you like to (P)lay again or (Q)uit? ')
                        while not 'q' or 'Q' in play:
                        play(D)[/code]

                        Clearly I'm a newbie and am having quite a lot of trouble.
                        I'm trying to figure out how to configure the function so that if the player decides to hit, the function that I have to evaluate the hand evaluates the next card added to the hand. I know this sounds confusing, but I would really appreciate it if someone could steer me in the right direction.

                        Comment

                        Working...