Game Logic: Determining Aces

Thread Solved

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Game Logic: Determining Aces

 
0
  #1
Jan 6th, 2007
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: ++
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Game Logic: Determining Aces

 
0
  #2
Jan 7th, 2007
One way to total up blackjack cards properly is presented in this thread:
http://www.daniweb.com/techtalkforum...98373-102.html
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Game Logic: Determining Aces

 
0
  #3
Jan 13th, 2007
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Game Logic: Determining Aces

 
0
  #4
Jan 17th, 2007
I like your solution, and I don't think there is any other way to handle the ace problem
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC