473,802 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Blackjack Code Help!!

6 New Member
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, 139 views)
Jan 8 '09 #1
11 2114
bvdet
2,851 Recognized Expert Moderator Specialist
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
ThaRealneSS
6 New Member
@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, 139 views)
Jan 8 '09 #3
bvdet
2,851 Recognized Expert Moderator Specialist
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
ThaRealneSS
6 New Member
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, 100 views)
Jan 8 '09 #5
bvdet
2,851 Recognized Expert Moderator Specialist
@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
2,851 Recognized Expert Moderator Specialist
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
ThaRealneSS
6 New Member
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
2,851 Recognized Expert Moderator Specialist
@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
ThaRealneSS
6 New Member
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

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

Similar topics

0
1246
by: Stephen | last post by:
I was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes (txtAdd,txtCity&txtPostcode). The array must hold the values of these three textboxes. I would like the array to also display these address values in an asp:Lable within a asp:TableCell inside a asp:Table. I want all this to happen on the...
9
2417
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a single string separated by a user defined character. There is no error trapping (by design), USE AT YOUR OWN RISK.
4
2830
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual Basic code that will examine numeric value for a particular field in a query, and assign a new numeric vaule to that field. There are over 21 possible values and I am told that IIF statement will only handle 9 of the possibilities and that I need...
3
25061
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown. eg. this only transferes the first record in the area. ..Fields("Uge").Value = ws.Range("A98").Value Sub SelectMaster()
5
1302
by: deercreek | last post by:
I could use a little help from a good code writer out there. I found some code and modified it a bit for my needs but, I need a little help to finish it up. What I am trying to due is to get a number to fill out a text box on my form. I want it to look at the form and get the CampStartDate and my CampEndDate also to look at a table of holidays. Then I want the text box to be filled with the number of days they will be staying that do not...
30
9142
by: imran akhtar | last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong with it. that will be great. thereis an attched file, to see the code more cleaer. from random import choice as randomcards def total(hand): # how many aces in the hand aces = hand.count(11) # to complicate things a little the...
9
7361
by: Chris Ahmsi | last post by:
I have been tasked to create a 'simple' form in Access providing managers to input necessary changes. I have 2 command buttons on the form and a check box. Command button 1 updates my table for multiple entries, and command button 2 e-mails the table in HTML format to my team, appends the data entered to a back-up table, and deletes the entries on the current table. When the check box on my form is checked (indicating a permanent change) I...
20
1949
by: 16800960 | last post by:
The Code is as Follows bool findTitlePrice(string allTitles, double allPrices, int totalRec, string title, double & price) { for(int i=0; i < totalRec; i++) { // your code here: // for each element of array allTitles, check if it matches // the given title, i.e. if title and allTitles are the same;
1
2973
by: Joshua T | last post by:
I am not very good with MySQL server side scripts. So any help would be awesome! My database info is as follows: <?php define('DB_HOST', 'pingback.db.5645640.hostedresource.com'); define('DB_USER', 'pingback'); define('DB_PASSWORD', 'Wind**p1'); define('DB_DATABASE', 'pingback'); ?> This is how my sql is setup:
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.