card4.py
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 blackjack value of this card."
if self.rank == 14:
return 1
elif self.rank > 10:
return 10
else:
return self.rank
def _isFaceCard(self):
"Returns True iff this card is a face card (K, Q, or J)."
if self.rank > 10 and self.rank < 14:
return True
return False
def rummyValue(self):
"Returns the value of this card in Rummy."
if self._isFaceCard():
return 10
elif self.rank == 14:
return 15
elif self.rank == 10:
return 10
else:
return 5
def __cmp__(self, other):
""" Compares Card objects by their ranks """
# Could compare by black jack value or rummy value
#print self, other
if self.rank < other.getRank():
# print "less"
return -1
elif self.rank > other.getRank():
# print "greater"
return 1
else:
return 0
def main():
c1 = Card(14, "spades")
print c1
print "Black Jack Value", c1.blackJackValue()
c2 = Card(13, "hearts")
print c2
print "Black Jack Value", c2.blackJackValue()
#print c2._isFaceCard()
c3 = Card(3, "diamonds")
print c3
print "Rummy value", c3.rummyValue()
if __name__ == '__main__':
main()
Generated by GNU enscript 1.6.4.