[Edu-sig] Cards n stuff...

Kirby Urner pdx4d@teleport.com
Wed, 14 Mar 2001 10:32:27 -0800


I thought I was done with this Cards thread (fun fun), but
was just reading the Python docs and discovered the 'shuffle'
method in the random module.  It reorders a sequence in place.
That reduces my shuffle method to:

  def shuffle(self):
      shuffle(self.cards)

-- with an 'from random import shuffle' up top.

Also, whereas the loop-in-a-loop (I call 'em loop-dee-loops)
method for initializing every combination of suite and rank
is fine, there's a way to do this with 2.0+ list comprehension 
which deserves advertising, so I've changed Deck's __init__
to:

  def __init__(self):
      self.cards = [Card(s,r) for s in suites for r in ranks.keys()]
      self.shuffle()

One might argue that we could take out 'def shuffle' as a method
altogether, and just get a new deck every time we want a shuffled 
deck.  But there's always a chance we'll want to deal some cards 
and then shuffle the remaining cards in the deck (?), which is 
what the separate 'shuffle' method will do for us.

Latest version:  
http://www.inetarena.com/~pdx4d/ocn/python/cards.py

Kirby

PS:  I realize my otherplayer's decision-making is pretty lame.
otherplayer gets to see what two cards I'm dealt yes?  So that
should factor into the decision whether to ask for a 3rd card
(it doesn't).  Like I said, my understanding of Blackjack is 
rather weak, and this cards.py module is more a 'scratch pad' 
of ideas -- it's not a mature implementation of the game, by 
any stretch of the imagination.  

Thanks again to Jeff for getting my wheels spinning.  I think
using objects for Deck, Card and Hand (player) is a good 
illustration of OO structuring (even though no subclasses 
get used), plus having parts of the code in a more procedural 
mode, while making use of these objects, shows how Python allows 
a creative blending/hybrid of an OO-style with procedural and 
functional styles -- part of what makes it both fun and powerful.