[Tutor] Using a Blackjack Chart...

Steven D'Aprano steve at pearwood.info
Thu May 24 03:36:34 CEST 2012


Ken G. wrote:
> I would like to create a Python program in the manner of an using flash 
> card format.
> That is, a question is asked and you can respond according and you will 
> be notify if you are correct or incorrect.

Is this supposed to be a graphical flashcard program?

Or something you run at the terminal, using just text? That's much simpler.


> Using such format stated above, I would like to create a Blackjack 
> program.  I wish
> to utilize a 'cheat sheet chart' format that provide the proper response.
>
> The chart has 10 rows across, being identified as the dealer's up card, 
> from 2 to 10 plus Ace.
> 
> There are 23 columns indicating the player's cards being shown, such as 
> 8 to 12, 13-16, 17+, A2 to A8 and 2,2 to A,A.
> 
> Each row and column would indicate either Hit, DD (Double), S (Stand) 
> and SP (Split).

There's no need for the 200lb sledgehammer of a database to crack this peanut.

Your data structure looks like a table, with a mere 10*23 = 230 cells. Just 
use a dictionary, with keys (dealer-up-card, player-cards) and values the 
response:

table = {
     (2, 8): 'H',  # dealer's card is 2, player's cards add to 8
     (2, 9): 'H',
     ...
     (2, 21): 'S',
     (3, 8): 'H',
     ...
     }


A text-only flash-card test would look something like this:

import random

def flash():
     # Pick a random cell from the table.
     cell = random.choice(table.keys())
     print "The dealer shows", cell[0]
     print "Your hand is", cell[1]
     response = raw_input("What do you do? ")
     if response == table[cell]:
         print "Correct"
     else:
         print "The dealer laughs cruelly as he takes your money."



def main():
     print "Blackjack flash-card program"
     print "Type Control-C at any time to halt."
     try:
         while True:
             flash()
     except KeyboardInterrupt:
         pass


main()



-- 
Steven


More information about the Tutor mailing list