Spring Semester 2007

Ten: Object-Oriented Design: The Blackjack Game (Part II).
With what we know so far we can start working on the Blackjack game. We first take a look at the cards module:
# Cards Module
# Basic classes for a game with playing cards
# Michael Dawson 4/18/03

class Card(object):
    """ A playing card. """
    RANKS = ["A", "2", "3", "4", "5", "6", "7",
             "8", "9", "10", "J", "Q", "K"]
    SUITS = ["c", "d", "h", "s"]

    def __init__(self, rank, suit, face_up = True):
        self.rank = rank
        self.suit = suit
        self.is_face_up = face_up

    def __str__(self):
        if self.is_face_up:
            rep = self.rank + self.suit
            rep = "XX"
        return rep

    def flip(self):
        self.is_face_up = not self.is_face_up
class Hand(object):
    """ A hand of playing cards. """
    def __init__(self):
        self.cards = []

    def __str__(self):
        if self.cards:
           rep = ""
           for card in self.cards:
               rep += str(card) + "\t"
            rep = ""
        return rep

    def clear(self):
        self.cards = []

    def add(self, card):

    def give(self, card, other_hand):

class Deck(Hand):
    """ A deck of playing cards. """
    def populate(self):
        for suit in Card.SUITS:
            for rank in Card.RANKS: 
                self.add(Card(rank, suit))

    def shuffle(self):
        import random

    def deal(self, hands, per_hand = 1):
        for rounds in range(per_hand):
            for hand in hands:
                if self.cards:
                    top_card = self.cards[0]
                    self.give(top_card, hand)
                    print "Can't continue deal. Out of cards!"

if __name__ == "__main__":
    print "This is a module with classes for playing cards."
    raw_input("\n\nPress the enter key to exit.")

Before we start designing the classes let's put together a design sketch.

Updated by Adrian German for A202/A598