-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.py
66 lines (50 loc) · 1.56 KB
/
card.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import random233
class Card:
suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
rank_names = [None, 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank
def __str__(self):
return '%s of %s' % (Card.rank_names[self.rank],Card.suit_names[self.suit])
def __lt__(self, other):
t1 = self.suit, self.rank
t2 = other.suit, other.rank
return t1 < t2
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
card = Card(suit, rank)
self.cards.append(card)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
def pop_card(self):
return self.cards.pop()
def add_card(self, card):
self.cards.append(card) #A method like this that uses another method without doing much work is sometimes called a veneer
def shuffle(self):
random233.shuffle(self.cards)
def move_cards(self, hand, num):
for i in range(num):
hand.add_card(self.pop_card())
class Hand(Deck):
def __init__(self, label=''):
self.cards = []
self.label = label
card1 = Card(2, 11)
card2 = Card(2, 13)
deck = Deck()
hand = Hand('new hand')
print("printing deck....")
print(deck , '\n')
card = deck.pop_card()
hand.add_card(card)
print(hand)
print(card1 < card2)
print(hand.label)
print(hand.cards)