| | |
Game Logic: Determining Aces
Thread Solved |
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.
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
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 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: ++
One way to total up blackjack cards properly is presented in this thread:
http://www.daniweb.com/techtalkforum...98373-102.html
http://www.daniweb.com/techtalkforum...98373-102.html
May 'the Google' be with you!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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:
It's my closest approximation to the way that I would compute it with real-life cards.
Hope it helps,
Jeff
Anyways, here's what I did with the ace problem:
Python Syntax (Toggle Plain Text)
class Card(object): RANKS = ['A','2','3','4','5','6','7','8','9','T','J','Q','K'] VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, \ '9':9, 'T':10, 'J':10, 'Q':10, 'K':10} # stuff def get_value(self): return Card.VALUES[self.rank] value = property(get_value) class Hand(object): def __init__(self): self.cards = [] # etc. def get_value(self): value = sum([x.value for x in self.cards]) if value > 21: ace_count = [x.rank for x in self.cards].count('A') while ace_count > 0 and value > 21: value -= 10 ace_count -= 1 return value
It's my closest approximation to the way that I would compute it with real-life cards.
Hope it helps,
Jeff
![]() |
Similar Threads
- Refreshing a Tkinter Canvas Multiple Times: (Python)
- Tkinter, GUI's, Logic... (Python)
- ASM Game Coding Problem/Question (Assembly)
- special keys as inputs (Game Development)
Other Threads in the Python Forum
- Previous Thread: import execl application using python
- Next Thread: PATH not known in Windows?
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






