471,349 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Python BlackJack

22
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!
Jul 19 '07 #1
11 6570
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. >>> def evalBlackJackCard(card):
  2. ...     try:
  3. ...         value = int(card[0])  # the first character of the string
  4. ...     except ValueError:  # in this case it's a letter
  5. ...         value = 10
  6. ...     return value
  7. ... 
  8. >>> cardValue = "8C"
  9. >>> print evalBlackJackCard(cardValue)
  10. 8
  11. >>> cardValue = "KC"
  12. >>> print evalBlackJackCard(cardValue)
  13. 10
  14. >>> 
Jul 19 '07 #2
LolaT
22
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:
Expand|Select|Wrap|Line Numbers
  1. >>> def evalBlackJackCard(card):
  2. ...     try:
  3. ...         value = int(card[0])  # the first character of the string
  4. ...     except ValueError:  # in this case it's a letter
  5. ...         value = 10
  6. ...     return value
  7. ... 
  8. >>> cardValue = "8C"
  9. >>> print evalBlackJackCard(cardValue)
  10. 8
  11. >>> cardValue = "KC"
  12. >>> print evalBlackJackCard(cardValue)
  13. 10
  14. >>> 

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!

Expand|Select|Wrap|Line Numbers
  1. >>> import cards
  2. >>> import random
  3. >>> d=cards.deck()
  4. >>> D=d*2 #because we use 2 decks
  5. >>> userHand=[]
  6. >>> userHand.append(random.choice(D))
  7. >>> userHand.append(random.choice(D))
  8. >>> userHand=[userHand[0].shortname(),userHand[1].shortname()] #to display just the rank and the suit
  9. >>> print userHand
  10. ['JC', '4C']
  11. >>> def evalCard(hand):
  12.     for cards in hand:
  13.         try:
  14.             value=int(cards[0])
  15.         except ValueError:
  16.             value=10
  17.         return value
  18.  
  19.  
  20. >>> evalCard(userHand)
  21. 10
Jul 19 '07 #3
bvdet
2,851 Expert Mod 2GB
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):

Expand|Select|Wrap|Line Numbers
  1. def evalCard(hand):
  2.     value = 0
  3.     for cards in hand:
  4.         try:
  5.             value += int(cards[0])
  6.         except ValueError:
  7.             value += 10
  8.     return value    
  9. print evalCard(userHand)
Jul 19 '07 #4
LolaT
22
This should work (untested):

Expand|Select|Wrap|Line Numbers
  1. def evalCard(hand):
  2.     value = 0
  3.     for cards in hand:
  4.         try:
  5.             value += int(cards[0])
  6.         except ValueError:
  7.             value += 10
  8.     return value    
  9. print evalCard(userHand)


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
Jul 19 '07 #5
bvdet
2,851 Expert Mod 2GB
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.
Jul 19 '07 #6
bartonc
6,596 Expert 4TB
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():
Expand|Select|Wrap|Line Numbers
  1. >>> cardValue = "AH"
  2. >>> cardValue.startswith("A")
  3. True
  4. >>> 
I thought about the 1 or 11 thing at the onset. A little tricky, that one.
Jul 19 '07 #7
BambiL
2
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
Jul 19 '07 #8
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. >>> values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",]
  2. >>> suits = ["H", "C", "D", "S"]
  3. >>> deck = [(s1 + s2) for s1 in values for s2 in suits]
  4. >>> deck
  5. ['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']
  6. >>> card = deck.pop(0)
  7. >>> card
  8. 'AH'
  9. >>> deck
  10. ['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']
  11. >>> 
Jul 20 '07 #9
LolaT
22
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
Jul 20 '07 #10
bartonc
6,596 Expert 4TB
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 .
Jul 20 '07 #11
LolaT
22
Thank you to everyone for their helpful suggestions.
I am yet again having a problem with one of the functions.
Expand|Select|Wrap|Line Numbers
  1. def play(D):
  2.     points=0
  3.  
  4.     option=raw_input('Would you like to (H)it or (S)tay? ')
  5.     if 's' or 'S' in option:
  6.         print "Your hand has a value of " + str(handValue(userHand))
  7.         print dealerHand
  8.         while handValue(dealerHand)<16:
  9.             dealerHand.append(random.choice(D))
  10.             dealerHand=[dealerHand[0],dealerHand[1],dealerHand[2]]
  11.         print handValue(dealerHand)
  12.         if handValue(dealerHand)> handValue(userHand)and handValue(dealerHand)< 21:
  13.             print "The dealer's hand has a value of " + str(handValue(dealerHand))
  14.             print "You lose a point."
  15.             points=points-1
  16.             print points
  17.         elif handValue(dealerHand)<handValue(userHand)and handValue(userHand)<21:
  18.             print "You gain a point"
  19.             points=points+1
  20.             print points
  21.         elif handValue(dealerHand)>21:
  22.             print "Dealer busts"
  23.             print "You gain a point"
  24.             points=points+1
  25.             print points
  26.         elif handValue(userHand)==handValue(dealerHand):
  27.             print 'PUSH'
  28.  
  29.  
  30.     elif 'h' or 'H' in option:
  31.         userHand.append(random.choice(D))
  32.         if handValue(userHand)>21:
  33.             print 'Bust. Your hand has a value of ' + str(handValue(userHand))
  34.             points=points-1
  35.             print 'You lose a point'
  36.             print points
  37.         elif handValue==21:
  38.             print "Your hand has a value of 21."
  39.             print dealerHand
  40.             while handValue(dealerHand)<16:
  41.                 dealerHand.append(random.choice(D))
  42.                 print handValue(dealerHand)
  43.                 if handValue(dealerHand)> handValue(userHand)and handValue(dealerHand)< 21:
  44.                     print "You lose a point."
  45.                     points=points-1
  46.                     print points
  47.                 elif handValue(dealerHand)<handValue(userHand)and handValue(userHand)<21:
  48.                     print "You gain a point"
  49.                     points=points+1
  50.                     print points
  51.                 elif dealerValue>21:
  52.                     print "Dealer busts"
  53.                     print "You gain a point"
  54.                     points=points+1
  55.                     print points
  56.  
  57.     play=raw_input('Would you like to (P)lay again or (Q)uit? ')
  58.     while not 'q' or 'Q' in play:
  59.           play(D)
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.
Jul 20 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Charlie Cosse | last post: by
112 posts views Thread by mystilleef | last post: by
83 posts views Thread by Licheng Fang | last post: by
13 posts views Thread by Wiseman | last post: by
27 posts views Thread by scott | last post: by
10 posts views Thread by vedrandekovic | last post: by
16 posts views Thread by Raxit | last post: by
61 posts views Thread by giveitawhril2008 | last post: by
30 posts views Thread by Ivan Reborin | last post: by
6 posts views Thread by Daniel | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.