-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
91 lines (77 loc) · 2.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Card:
# Cards are 0-indexed so the ace's value is 0
ACE_VALUE = 0
# Value 0 1 2 3 4 5 6 7 8 9 10 11 12
# Card A 2 3 4 5 6 7 8 9 0 J Q K
COUNT_VAL = [-1, 1, 1, 2, 2, 2, 1, 0, 0, -2, -2, -2, -2] # Complicated
#COUNT_VAL = [-1, 1, 1, 1, 1, 1, 0, 0, 0, -1, -1, -1, -1] # Simple
"""
A card represents a suit, value pair. Card suits
are 0-based and are ordered as Clubs, Diamonds,
Spades, Hearts. Card values are also 0-based, and
a value of 0 corresponds to an ace, while a value
of 12 corresponds to a king. Suits and values must
be non-negative integers, but no enforcement on
them is made aside from that. Face cards, which
have a count of 10, are considered to be all cards
from ten onward.
"""
def __init__(self, suit, value):
if suit < 0:
raise ValueError("Suit must be a non-negative integer " \
"but was {0}".format(suit))
if value < 0:
raise ValueError("Value must be a non-negative integer " \
"but was {0}".format(value))
self.suit = suit
self.value = value
def getCount(self):
"""
The count of a card is simply its 1-based value
for ace through ten and is 10 for the jack and
higher.
"""
if self.value < 10:
return self.value + 1
else:
return 10
def getSoftCount(self):
"""
The soft count for a card is the same as its
count except for the ace, in which case the
soft count is 11.
"""
if self.value == 0:
return 11
else:
return self.getCount()
def __eq__(self, other):
return (self.suit, self.value) == (other.suit, other.value)
def __hash__(self):
return hash((self.suit, self.value))
def __str__(self):
return "{1} of {0}".format(self.suitString(),
self.valueString())
__repr__ = __str__
def suitString(self):
if self.suit == 0:
return "Clubs"
elif self.suit == 1:
return "Diamonds"
elif self.suit == 2:
return "Spades"
elif self.suit == 3:
return "Hearts"
else:
return "Suit {0}".format(self.suit)
def valueString(self):
if self.value == 0:
return "Ace"
elif self.value == 10:
return "Jack"
elif self.value == 11:
return "Queen"
elif self.value == 12:
return "King"
else:
return "{0}".format(self.value + 1)