-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path18.1.py
More file actions
75 lines (58 loc) · 1.8 KB
/
18.1.py
File metadata and controls
75 lines (58 loc) · 1.8 KB
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
67
68
69
70
71
72
73
74
75
#coding:utf-8
import random
class Card(object):
"""
Represents a standard playing card
"""
def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank
suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
rank_names = [None, 'Ace', '2', '3', '4', '5', '6', '7', '8',
'9', '10', 'Jack', 'Queen', 'King']
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(object):
"""Represents a 52 deck of cards"""
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)
def shuffle(self):
random.shuffle(self.cards)
def sort(self):
return self.cards.sort()
def deal_hands(self, hands, cards):
res = []
for hand in range(hands):
for card in range(cards):
res.append(self.pop_card())
return res
class Hand(Deck):
"""
Represents a hand of playing cards
"""
def __init__(self, label=''):
self.cards = []
self.label = label
def move_cards(self, hand, num):
for i in range(num):
hand.add_card(self.pop_card())
deck = Deck()
print deck.deal_hands(5, 5)