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!
Python BlackJack
Collapse
X
-
Originally posted by LolaTI 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 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] -
Originally posted by bartoncFor 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
-
Originally posted by LolaTThank 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]
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
-
Originally posted by bvdetThis 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 thoughComment
-
Originally posted by LolaTthanks 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
>>> 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
-
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 :SComment
-
Originally posted by BambiLI'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
>>> 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
-
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
-
Originally posted by LolaTA 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-cardsComment
-
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
Comment