Contents
- card3_nocmp.py
- card4.py
- card5.py
card3_nocmp.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):
if self.rank == 14:
return 1
elif self.rank > 10:
return 10
else:
return self.rank
def rummyValue(self):
if 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():
c3 = Card(3, "hearts")
print "c1 =", c3
print "c1 is 3 of hearts"
c2 = Card(13, "hearts")
print "c2 =", c2
print "c2 is King of hearts"
c1 = Card(3, "spades")
print "c3 =", c1
print "c3 is 3 of spades"
print "\nTest _cmp_ method..."
#print c1, "__cmp__", c2, "-->", c1.__cmp__(c2)
#print c3, "__cmp__", c1, "-->", cmp(c3,c1)
#If you don't define a __cmp__ method, then the default behavior is to
# compare memory addresses.
print c1,">",c2,"?", c1 > c2
print c1,">",c3,"?", c1 > c3
print c2, ">", c3, "?", c2 > c3
if __name__ == '__main__':
main()
card4.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 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()
print
c2 = Card(13, "hearts")
print c2
print "Black Jack Value:", c2.blackJackValue()
print "Is c2 a face card?", c2._isFaceCard()
print
c3 = Card(3, "diamonds")
print c3
print "Rummy value", c3.rummyValue()
print "Is c3 a face card?", c3._isFaceCard()
if __name__ == '__main__':
main()
card5.py 3/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 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 suit (clubs, diamonds, hearts,
spades), then their rank """
if self.suit < other.getSuit():
return -1
if self.suit > other.getSuit():
return 1
# same suit; differentiate by rank
if self.rank < other.getRank():
return -1
if self.rank > other.getRank():
return 1
return 0
def main():
c1 = Card(14, "spades")
c2 = Card(13, "hearts")
c3 = Card(3, "diamonds")
cardList = []
cardList.append(c1)
cardList.append(c2)
cardList.append(c3)
print "The original list of Cards is:"
for card in cardList:
print card
cardList.sort()
print "\nThe sorted list of Cards is:"
for card in cardList:
print card
if __name__ == '__main__':
main()
Generated by GNU enscript 1.6.4.