Hi,

I'm learning python and I just started to learn about classes. To get a handle on this, I decided to make a black jack game and create 3 classes: deck, hand, and card. I created a card class, but I am not sure how I would implement it in the hand class when I create those methods.

For example, did I do the add_card method correctly for the hand class? How does python know that card must be of class Card?

Thank you!

SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES =    {   'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, 
                '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10
            }


# define card class
class Card:
    def __init__(self, suit, rank):
        if (suit in SUITS) and (rank in RANKS):
            self.suit = suit
            self.rank = rank
        else:
            self.suit = None
            self.rank = None
            print "Invalid card: ", suit, rank

    def __str__(self):
        return self.suit + self.rank

    def get_suit(self):
        return self.suit

    def get_rank(self):
        return self.rank


# define hand class
class Hand:
    def __init__(self):
        self.hand = []

    def __str__(self):
        return self.hand

    def add_card(self, card):
        return self.hand.append(card)

    # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
    def get_value(self):


    def busted(self):
        pass

    def draw(self, canvas, p):
        pass


# define deck class
class Deck:
    def __init__(self):
        self.

    # add cards back to deck and shuffle
    def shuffle(self):
        pass

    def deal_card(self):
        pass

    def __str__(self):
        pass

How does python know that card must be of class Card?

Python does not know. You could add anything with this method. There is no type checking at run time. What you could do is add an assert statement

    def add_card(self, card):
        assert isinstance(card, Card)
        return self.hand.append(card)

Start with the deck class by itself. A hand would be a list of cards dealt from the deck, so you can have as many lists/hands as you want. Something to get you started

class Deck(object):
    def __init__(self):
        suits = ('C', 'S', 'H', 'D')
        ranks = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
        self.deck = [rank+suit for rank in ranks for suit in suits]
        print self.deck

        self.shuffle_deck()
        self.next_card = 0
        for ctr in range(5):
            self.deal_a_card()

    def deal_a_card(self):
        print "\n  deal a card", self.deck[self.next_card]
        self.next_card += 1

    def shuffle_deck(self):
        random.shuffle(self.deck)
        print "\nshuffled deck", self.deck

DK=Deck()

Edited 4 Years Ago by woooee

Nice woooee, bit nicer still if you include self.next_card = 0 in shufle_deck method and we should check when deck is dealt completely by catching index error from deck. other maybe more common method would use pop from list of cards, which makes running out of cards bit more obvious.

This article has been dead for over six months. Start a new discussion instead.