Contents

  1. card2.py
  2. card.py
  3. deck.py

card2.py 1/3

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    def __init__(self, rank, suit):
        "Constructor for class Card takes int rank and string suit."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
	"Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit

    def blackJackValue(self):
    	"Returns the value of the card in Black Jack"
        if self.rank == 14:
            # alternatively, this could be 11
            return 1
        elif self.rank > 10:
            return 10
        else:
            return self.rank

    def rummyValue(self):
        "Returns the value of the card in Rummy"
        if self.rank <= 9:
            return 5
        elif self.rank < 14:
            return 10
        else:
            return 15

def main():
    c1 = Card(14, "spades")
    print c1
    print "Black Jack Value", c1.blackJackValue()
    print "Rummy Value", c1.rummyValue()
    c2 = Card(13, "hearts")
    print c2
    print "Black Jack Value", c2.blackJackValue()
    print "Rummy Value", c2.rummyValue()
    
if __name__ == '__main__':
    main()

card.py 2/3

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    def __init__(self, rank, suit):
        "Constructor for class Card takes int rank and string suit."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
	"Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit

def main():
    c1 = Card(14, "spades")
    print c1
    c2 = Card(13, "hearts")
    print c2
    
if __name__ == '__main__':
    main()

deck.py 3/3

[
top][prev][next]
from card import *
from random import shuffle

class Deck:
    """ A class to represent a deck of playing cards."""
    
    def __init__(self):
        self.cards = []
        # executes 4 times (once for each suit)
        for suit in ["clubs","hearts","diamonds","spades"]:
            # executes 13 times (once for each rank)
            for rank in range(2,15):
                # the body of the loop gets executed a total of 52
                # times
                self.cards.append(Card(rank, suit))

    def __str__(self):
        result = ""
        for c in self.cards:
            # Either of the following two lines do the same thing,
            # have the same result:
        
            #result += c.__str__() + "\n"
            result += str(c) + "\n"
        return result

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

    def getNumCardsLeft(self):
        """Returns the number of cards left in the deck."""
        return len(self.cards)

    def drawCard(self):
        """Returns the top card on the deck.  If there are no cards in
        the deck, returns None"""
        if self.getNumCardsLeft() == 0:
            return None
        return self.cards.pop(0)


def main():
    d = Deck()
    d.shuffle
    print d

main()

Generated by GNU enscript 1.6.4.