Contents

  1. card2.py
  2. card.py

card2.py 1/2

[
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/2

[
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()

Generated by GNU enscript 1.6.4.