forked from Jehensjuh/rlcard-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
52 lines (43 loc) · 1.67 KB
/
base.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
''' Game-related base classes
'''
class Card:
'''
Card stores the suit and rank of a single card
Note:
The suit variable in a standard card game should be one of [S, H, D, C, BJ, RJ] meaning [Spades, Hearts, Diamonds, Clubs, Black Joker, Red Joker]
Similarly the rank variable should be one of [A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K]
'''
suit = None
rank = None
valid_suit = ['S', 'H', 'D', 'C', 'BJ', 'RJ']
valid_rank = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
def __init__(self, suit, rank):
''' Initialize the suit and rank of a card
Args:
suit: string, suit of the card, should be one of valid_suit
rank: string, rank of the card, should be one of valid_rank
'''
self.suit = suit
self.rank = rank
def __eq__(self, other):
if isinstance(other, Card):
return self.rank == other.rank and self.suit == other.suit
else:
# don't attempt to compare against unrelated types
return NotImplemented
def __hash__(self):
suit_index = Card.valid_suit.index(self.suit)
rank_index = Card.valid_rank.index(self.rank)
return rank_index + 100 * suit_index
def __str__(self):
''' Get string representation of a card.
Returns:
string: the combination of rank and suit of a card. Eg: AS, 5H, JD, 3C, ...
'''
return self.rank + self.suit
def get_index(self):
''' Get index of a card.
Returns:
string: the combination of suit and rank of a card. Eg: 1S, 2H, AD, BJ, RJ...
'''
return self.suit+self.rank