By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

Blackjack Code Help!!

P: 6
Hi.

I have made a blackjack code and need a some help on it. It seems to work overall but there are a few bits here and there that need sorting out, and I'm kinda stuck on it so was wondering if I could get some help? (Would be HUGELY appreciated )

The things I need help are on:

- Stopping the program going back to the code (you will know what I mean when you play it)
- How to add the re-run code in it and where
- How I can count the ace as 1 and 11 (as in my code it only counts as 11)
- How I can add the different suites like diamonds, clubs, hearts and spades, because my program only displays the numbers not the suites with it
- How I can show the cards played after each round
- How I can shuffle the cards
- How I can track what cards have been dealt so that each card is only dealt once

My code is here is attached.

Thanks in advance
Attached Files
File Type: zip tutti_blackjack_code (2).zip (1.4 KB, 93 views)
Jan 8 '09 #1
Share this Question
Share on Google+
11 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
We have a recent blackjack thread that addresses some of your concerns.

blackjack game

Three classes are defined: Card (defines suits and rank, including the Ace), Deck (creates a deck of cards, self.cards contains the Card objects), and BJ. BJ() is the game itself and has methods deal_hand(), hit_hand(), score_hand_BJ() (this is where the Ace is counted as 1 or 11 automatically), play() (the game loop), and play_over() (the game loop is summarized and results are displayed).

Your code has other problems. For example, selecting "H" for hit does not actually give the player a hit because you are using "H" instead of "h" in the if statement. The computer's hand should be automatically hit if below the threshold, but is only hit if the user elects to receive one. There is no check to see if the player busts. You can determine the current score using sum() instead of a for loop since your list only contains integers.

Maybe you can get other ideas to improve your code from the thread mentioned above.
Jan 8 '09 #2

P: 6
@bvdet
Hi.

Firslty I would like to thank you for spending your time to check out my code, appreciate it buddy :). Secondly, the error with the H/Hit there was, I have slightly fixed and changed and you will see the difference in the new updated code I will upload.

On another note, I have looked at the thread, and even used to the code and played it, and it is very good. But I just do not know how to adapt some of the code from there to mine. One of the main errors with my code is that it keeps going back to the code itself when playing a game (did you come across that?). Do you know how I can fix that? And would you know how I could fix any of the other issues/problems I posted in my first post? I am not that experienced in Python, I know more in VB, so yeah I do not know alot in Python. My friend helped me create this code as I struggled, and he has gone on a vacation so he cannot help me out, which sucks. If you could help me out, I would be very grateful and would appreciate it alot.

Thanks once again.

- ThaRealneSS
Attached Files
File Type: zip realness_updated_blackjack.zip (1.5 KB, 87 views)
Jan 8 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I would setup your while loop something like this:
Expand|Select|Wrap|Line Numbers
  1. def play():
  2.     # initialize a results variable
  3.     while True:
  4.         c = raw_input("[D]eal [H]it me [S]tick [Q]uit: ").lower()
  5.         .................
  6.         if c == 'd':
  7.             ..............
  8.         elif c == 'h':
  9.             ..........
  10.         elif c == 's':
  11.             ..........
  12.             # here is where you update results
  13.         elif c == 'q':
  14.             print "Thanks for playing blackjack with the computer!"
  15.             return results
  16.  
  17. print play()
Here's how I would hit a hand:
Expand|Select|Wrap|Line Numbers
  1. def hit_hand(self, hand, deck):
  2.     hand.append(deck.pop(random.choice(range(len(deck)))))
  3.  
  4. hit_hand(player_cards, deck)
  5. hit_hand(computer_cards, deck)
This also removes the card from the deck.
Jan 8 '09 #4

P: 6
I added the changes (and I know where I added some are wrong, but hey I tried lol), and there are some errors :(. Attached it.
Attached Files
File Type: zip realness_BJ_1.zip (1.4 KB, 81 views)
Jan 8 '09 #5

bvdet
Expert Mod 2.5K+
P: 2,851
@ThaRealneSS
The comment "# initialize a results variable" meant that YOU need to write some code to initialize an appropriate variable to save the results of the game, maybe a list, nested list or dictionary.
Jan 8 '09 #6

bvdet
Expert Mod 2.5K+
P: 2,851
A Card object is just what it says - a card from 52 possibilities.
Expand|Select|Wrap|Line Numbers
  1. >>> Card(2,3)
  2. 3 of Clubs
  3. >>> Card(0,12)
  4. Queen of Hearts
A Deck object is a collection of the 52 possible cards.
Expand|Select|Wrap|Line Numbers
  1. >>> Deck()
  2. [Ace of Hearts, 2 of Hearts, 3 of Hearts, 4 of Hearts, 5 of Hearts, 6 of Hearts, 7 of Hearts, 8 of Hearts, 9 of Hearts, 10 of Hearts, Jack of Hearts, Queen of Hearts, King of Hearts, Ace of Diamonds, 2 of Diamonds, 3 of Diamonds, 4 of Diamonds, 5 of Diamonds, 6 of Diamonds, 7 of Diamonds, 8 of Diamonds, 9 of Diamonds, 10 of Diamonds, Jack of Diamonds, Queen of Diamonds, King of Diamonds, Ace of Clubs, 2 of Clubs, 3 of Clubs, 4 of Clubs, 5 of Clubs, 6 of Clubs, 7 of Clubs, 8 of Clubs, 9 of Clubs, 10 of Clubs, Jack of Clubs, Queen of Clubs, King of Clubs, Ace of Spades, 2 of Spades, 3 of Spades, 4 of Spades, 5 of Spades, 6 of Spades, 7 of Spades, 8 of Spades, 9 of Spades, 10 of Spades, Jack of Spades, Queen of Spades, King of Spades]
  3. >>> d = Deck()
  4. >>> d[12]
  5. King of Hearts
  6. >>> len(d)
  7. 52
  8. >>> print d
  9. Ace of Hearts
  10. 2 of Hearts
  11. 3 of Hearts
  12. 4 of Hearts
  13. 5 of Hearts
  14. 6 of Hearts
  15. 7 of Hearts
  16. 8 of Hearts
  17. 9 of Hearts
  18. 10 of Hearts
  19. Jack of Hearts
  20. Queen of Hearts
  21. King of Hearts
  22. Ace of Diamonds
  23. 2 of Diamonds
  24. 3 of Diamonds
  25. 4 of Diamonds
  26. 5 of Diamonds
  27. 6 of Diamonds
  28. 7 of Diamonds
  29. 8 of Diamonds
  30. 9 of Diamonds
  31. 10 of Diamonds
  32. Jack of Diamonds
  33. Queen of Diamonds
  34. King of Diamonds
  35. Ace of Clubs
  36. 2 of Clubs
  37. 3 of Clubs
  38. 4 of Clubs
  39. 5 of Clubs
  40. 6 of Clubs
  41. 7 of Clubs
  42. 8 of Clubs
  43. 9 of Clubs
  44. 10 of Clubs
  45. Jack of Clubs
  46. Queen of Clubs
  47. King of Clubs
  48. Ace of Spades
  49. 2 of Spades
  50. 3 of Spades
  51. 4 of Spades
  52. 5 of Spades
  53. 6 of Spades
  54. 7 of Spades
  55. 8 of Spades
  56. 9 of Spades
  57. 10 of Spades
  58. Jack of Spades
  59. Queen of Spades
  60. King of Spades
  61. >>> 
All the action takes place in BJ method play(). play() creates a deck of cards and calls other BJ methods to play a game. A Card object is dealt to a hand like this:
Expand|Select|Wrap|Line Numbers
  1. hand.append(deck.cards.pop(random.choice(range(len(deck)))))
Variables hand1 and hand2 represent the hands for Player 1 and Player 2. Player 2 is the dealer (computer). The game results are stored in BJ attribute results, which is a nested list in this format: [[winner, playerhand, dealerhand], [winner, playerhand, dealerhand], [winner, playerhand, dealerhand], ..................]

BJ method play_over() determine how many hands were won by whom, and formats the hands for display.
Jan 8 '09 #7

P: 6
I appreciate all the help you are giving me, but whenever I put some of it in my code I just get errors and it doesn't work. I may sound like a "n00b" but I guess that's what I am in Python. And I can't fix the error which always makes it go back to the code whenevr a run the program and play it (did you get that too?!?). Thanks once again.

- TheRealneSS
Jan 9 '09 #8

bvdet
Expert Mod 2.5K+
P: 2,851
@ThaRealneSS
Maybe I don't understand what you mean by "go back to the code". I had no problem exiting the script. When I enter a "Q", it exits.
Jan 9 '09 #9

P: 6
Like when I run the code and play a game, for example if I press the "Q" key to exit, then I will see the CMD shell window pop up for like half a second, and then it will take me back to my code (Python Editor). For me to be able to continute using my code I have to minimise the Python Editor and go back onto te Python Shell. Get what I mean now?

- ThaRealneSS
Jan 9 '09 #10

bvdet
Expert Mod 2.5K+
P: 2,851
@ThaRealneSS
Not really. Are you running the script from your Python editor or from the command prompt?
Jan 9 '09 #11

P: 6
@bvdet
From the Python Editor.
Jan 9 '09 #12

Post your reply

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