473,216 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 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 6725
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

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

Similar topics

0
by: Charlie Cosse | last post by:
Asymptopia BlackJack is written in Python and uses PyGame for multimedia. Version 1.2 contains both Windows and Linux install scripts. Asymptopia BlackJack is a full-featured casino-style...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
83
by: Licheng Fang | last post by:
Hi, I'm learning STL and I wrote some simple code to compare the efficiency of python and STL. //C++ #include <iostream> #include <string> #include <vector> #include <set> #include...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
27
by: scott | last post by:
Hi all, I have been looking at the various programming languages available. I have programed in Basic since I was a teenager and I also have a basic understanding of C, but I want something...
10
by: vedrandekovic | last post by:
Hello, I was install Python 2.5 and uninstall Python 2.4 now I cannot run my scripts, only from idle What should I do? Regards, Vedran
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
61
by: giveitawhril2008 | last post by:
I'm just learning about Python now and it sounds interesting. But I just read (on the Wiki page) that mainstream Python was written in C. That's what I was searching for: Python was written in what...
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
6
by: Daniel | last post by:
I hope this question is OK for this list. I've downloaded Rpyc and placed it in my site packages dir. On some machines it works fine, on others not so much. Here is one error I get when I try...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.