-
Notifications
You must be signed in to change notification settings - Fork 70
/
card.py
61 lines (50 loc) · 1.58 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
import re
class Card:
SUIT_TO_STRING = {
1: "s",
2: "h",
3: "d",
4: "c"
}
RANK_TO_STRING = {
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "T",
11: "J",
12: "Q",
13: "K",
14: "A"
}
RANK_JACK = 11
RANK_QUEEN = 12
RANK_KING = 13
RANK_ACE = 14
STRING_TO_SUIT = dict([(v, k) for k, v in SUIT_TO_STRING.iteritems()])
STRING_TO_RANK = dict([(v, k) for k, v in RANK_TO_STRING.iteritems()])
REPR_RE = re.compile(r'\((.*?)\)')
def __init__(self, rank, suit):
"""Create a card. Rank is 2-14, representing 2-A,
while suit is 1-4 representing spades, hearts, diamonds, clubs"""
self.rank = rank
self.suit = suit
def __repr__(self):
return "%s%s" % (self.RANK_TO_STRING[self.rank], self.SUIT_TO_STRING[self.suit])
def __eq__(self, other):
return (isinstance(other, self.__class__) and self.rank == other.rank and self.suit == other.suit)
def __hash__(self):
return hash((self.rank, self.suit))
@classmethod
def from_repr(cls, repr):
"""Return a card instance from repr.
This is really dirty--it just matches between the parens.
It's meant for debugging."""
between_parens = re.search(cls.REPR_RE, repr).group(1)
rank = cls.STRING_TO_RANK[between_parens[0].upper()]
suit = cls.STRING_TO_SUIT[between_parens[1].lower()]
return Card(rank, suit)