944,110 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1536
  • Python RSS
Jan 6th, 2007
0

Game Logic: Determining Aces

Expand Post »
I am in the end part of a Python Gui game build; it is going quite well but I am finding that I underestimated one area of the logic, incorrectly coding it: The game is blackjack.

One area that, although it seemed simple on the surface (and in fact it probably is not as difficult as I am making it) is to monitor Aces that may be in one hand of the game. I will not go into the details of the game of blackjack but I will explain the basics concerning Aces.
  • They have a value of "1" by default
  • Aces can be played as either as "1" or "11" depending on which suits the player better without going over "21"-- that is, they can and do switch value from each card dealt depending on the other cards
I am writing code that first determines if a card is specifically an Ace. I then need to have the program decide whether or not to make the Ace a "1" or an "11". This is simple when dealing with one Ace but if you have more than one Ace it gets trickier. You must, of course, determine how many Aces you have and what their value should be depending on every other card. At this point, I check for an Ace after each new card is dealt then I try to determine its proper value, whatever creates the best hand without going over 21.

I have written a lot of code (I will not post any at this point because it is a mess and it only works in part, that is if you have only two cards dealt, it can sometimes know what to do if one card is a "5" and one card is an Ace: the total at that point would and should be "16" (11 + 5). Add a new Ace,(dealt card 3) and it may or may not react correctly.

I realize the algo for this must be somewhat simple, but I am going in circles at this point. I seemed to have it working well earlier (I am only coding and testing for the first 3 dealt cards at this point seeing that I can simply clone and update this code for the next, dealt cards); I took just this Ace section out of the program code, created a simplified file without the GUI\animation and ran it all night. This worked until I placed it back into the real program then... instant bugs concerning Ace recognition and value manipulation.

Has anyone ever dealt with this sort of thing. I know I will figure out it alone but I could use some help.

Thank you in advance,
sharky_machine
Last edited by mattyd; Jan 6th, 2007 at 4:19 am. Reason: ++
Similar Threads
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Jan 7th, 2007
0

Re: Game Logic: Determining Aces

One way to total up blackjack cards properly is presented in this thread:
http://www.daniweb.com/techtalkforum...98373-102.html
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 13th, 2007
0

Re: Game Logic: Determining Aces

I wrote a blackjack also, but was informed by one of my students that I put in a poker-like betting system instead of the blackjack betting system. :o

Anyways, here's what I did with the ace problem:

Python Syntax (Toggle Plain Text)
  1.  
  2. class Card(object):
  3.  
  4. RANKS = ['A','2','3','4','5','6','7','8','9','T','J','Q','K']
  5. VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, \
  6. '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
  7.  
  8. # stuff
  9.  
  10. def get_value(self):
  11. return Card.VALUES[self.rank]
  12.  
  13. value = property(get_value)
  14.  
  15. class Hand(object):
  16.  
  17. def __init__(self):
  18. self.cards = []
  19. # etc.
  20.  
  21. def get_value(self):
  22.  
  23. value = sum([x.value for x in self.cards])
  24. if value > 21:
  25. ace_count = [x.rank for x in self.cards].count('A')
  26. while ace_count > 0 and value > 21:
  27. value -= 10
  28. ace_count -= 1
  29. return value

It's my closest approximation to the way that I would compute it with real-life cards.

Hope it helps,
Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jan 17th, 2007
0

Re: Game Logic: Determining Aces

I like your solution, and I don't think there is any other way to handle the ace problem
Quote ...
but was informed by one of my students that I put in a poker-like betting system instead of the blackjack betting system
They always know more than we do. Viva la future.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: import execl application using python
Next Thread in Python Forum Timeline: PATH not known in Windows?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC