In [14]:
class Card:
def __init__(self, rank, suit):
self.suit = suit
self.rank = rank
In [20]:
card = Card("king", "clubs")
card.rank
Out[20]:
In [22]:
card2 = Card("King", "clubs")
card2
Out[22]:
In [ ]:
In [23]:
class Card:
def __init__(self, suit=0, rank=0):
self.suit = suit
self.rank = rank
In [25]:
three_of_clubs = Card(0, 3)
card1 = Card(1, 11)
card1
Out[25]:
In [26]:
class Card:
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit=0, rank=0):
self.suit = suit
self.rank = rank
def __str__(self):
return (self.ranks[self.rank] + " of " + self.suits[self.suit])
In [27]:
card = Card()
In [37]:
(card.rank, card.suit)
Out[37]:
In [42]:
class Card:
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit=0, rank=1):
self.suit = suit
self.rank = rank
def __str__(self):
return (self.ranks[self.rank] + " of " + self.suits[self.suit])
def cmp(self, other):
# Check the suits
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
# Suits are the same... check ranks
if self.rank > other.rank: return 1
if self.rank < other.rank: return -1
# Ranks are the same... it's a tie
return 0
In [50]:
card = Card()
card2 = Card(3, 5)
card3 = Card(3, 6)
print(card, card2, card3)
card2 < card3
In [52]:
class Card:
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit=0, rank=1):
self.suit = suit
self.rank = rank
def __str__(self):
return (self.ranks[self.rank] + " of " + self.suits[self.suit])
def cmp(self, other):
# Check the suits
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
# Suits are the same... check ranks
if self.rank > other.rank: return 1
if self.rank < other.rank: return -1
# Ranks are the same... it's a tie
return 0
def __eq__(self, other):
return self.cmp(other) == 0
def __le__(self, other):
return self.cmp(other) <= 0
def __ge__(self, other):
return self.cmp(other) >= 0
def __gt__(self, other):
return self.cmp(other) > 0
def __lt__(self, other):
return self.cmp(other) < 0
def __ne__(self, other):
return self.cmp(other) != 0
In [56]:
card = Card()
card2 = Card(3, 5)
card3 = Card(3, 6)
print(card, card2, card3)
card2 >= card3
Out[56]:
In [57]:
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
self.cards.append(Card(suit, rank))
In [60]:
deck = Deck()
deck.cards
Out[60]:
In [68]:
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
self.cards.append(Card(suit, rank))
def __str__(self):
s = ""
for i in range(len(self.cards)):
s = s + " " * i + str(self.cards[i]) + "\n"
return s
In [69]:
deck = Deck()
print(deck)
In [11]:
class Deck:
...
def remove(self, card):
if card in self.cards:
self.cards.remove(card)
return True
else:
return False
In [12]:
class Deck:
...
def pop(self):
return self.cards.pop()
In [13]:
class Deck:
...
def is_empty(self):
return self.cards == []
In [70]:
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
self.cards.append(Card(suit, rank))
def __str__(self):
s = ""
for i in range(len(self.cards)):
s = s + " " * i + str(self.cards[i]) + "\n"
return s
def remove(self, card):
if card in self.cards:
self.cards.remove(card)
return True
else:
return False
def pop(self):
return self.cards.pop()
def is_empty(self):
return self.cards == []
In [77]:
deck = Deck()
card = Card(1,2)
print(card)
deck.remove(card)
Out[77]:
In [80]:
print(deck.pop())
In [81]:
print(deck)
In [ ]: