-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck_of_cards.py
More file actions
executable file
·78 lines (56 loc) · 2.02 KB
/
deck_of_cards.py
File metadata and controls
executable file
·78 lines (56 loc) · 2.02 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
76
77
#!/usr/bin/python
import random
SUITS = ['Clubs','Diamonds','Hearts', 'Spades']
NAMED_VALUES = [None, None, 'Two', 'Three' , 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
class Error(Exception):
pass
class CardInitializationError(Error):
def __init__(self, msg):
self.msg = msg
class CardComparisonError(Error):
def __init__(self, msg):
self.msg = msg
class DeckOfCardsEmptyError(Error):
def __init__(self, msg):
self.msg = msg
def generateDeck():
for suit in SUITS:
for num in xrange(2,15):
yield Card(suit,num)
class Card(object):
def __init__(self,suit,value):
if suit not in SUITS:
raise CardInitializationError('Expected suit in {0}, observed value: {1}'.format(str(SUITS), str(suit)))
if value < 2 or value > 14:
raise CardInitializationError('Expected value to integer between 2 and 14, observed value: {0}'.format(str(value)))
self.suit = suit
self.value = value
def __str__(self):
if self.value < 11:
return str(self.value) + self.suit[0]
else:
return str(NAMED_VALUES[self.value][0]) + self.suit[0]
def __cmp__(self, other):
if not isinstance(other, Card):
raise CardComparisonError('Attempted to compare Card to {0}'.format(str(type(other))))
return -cmp((self.value, self.suit), (other.value, other.suit))
class DeckOfCards(object):
def __init__(self):
self.cards_left= [card for card in generateDeck()]
def num_of_cards(self):
return len(self.cards_left)
def draw_card(self):
if self.num_of_cards():
new_card = random.choice(self.cards_left)
self.cards_left.remove(new_card)
return new_card
else:
raise DeckOfCardsEmptyError('Attempted to draw a card from an empty deck')
def draw_cards(self, num):
if self.num_of_cards() >= num:
new_cards = random.sample(self.cards_left, num)
for new_card in new_cards:
self.cards_left.remove(new_card)
return new_cards
else:
raise DeckOfCardsEmptyError('Attempted to draw {0} card(s) from a deck of {1} card(s)'.format(str(num), str(self.num_of_cards())))