-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
68 lines (54 loc) · 2.33 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
""" Models a single card of a bavarian deck of cards. Supports
"""
class Card:
SUITS = {
"e": "Eichel",
"g": "Gras",
"h": "Herz",
"s": "Schellen"
}
NUMBERS = ["A", "X", "K", "O", "U", "9", "8", "7"]
def __init__(self, suit, number):
self.suit = suit
self.number = number
self.binding_suit = suit # used to determine which cards may be chosen in this round
# initially order as if hearts were trump
if number in ["O", "U"] or suit == "h":
self.binding_suit = "t"
def __repr__(self):
return self.suit + "+" + self.number
def __str__(self):
return self.suit + "-" + self.number
# using lexical ordering
def order(self):
return self.suit + "-" + str(Card.NUMBERS.index(self.number))
# using lexical ordering
def binding_order(self):
return self.binding_suit + "-" + str(Card.NUMBERS.index(self.number))
def adapt_to(self, game):
if game.is_trump(self):
self.binding_suit = "t"
else:
self.binding_suit = self.suit
def __lt__(self, other): # used for sorting
if (self.binding_suit == "t") ^ (other.binding_suit == "t"): # exactly one trump
return self.binding_suit == "t"
elif self.binding_suit == "t": # both trump
if (self.number == "O") ^ (other.number == "O"): # exactly one Ober
return self.number == "O"
elif (self.number == "U") ^ (other.number == "U"): # exactly one Unter
return self.number == "U"
return self.order() < other.order() # order by the original suit names
else: # no trump involved
return self.binding_order() < other.binding_order()
def __gt__(self, other): # used to determine whether a card beats another
if (self.binding_suit == "t") or (other.binding_suit == "t") or (self.binding_suit == other.binding_suit): # trump involved or same (binding) suit
return self < other # use sorting comparison
else: # otherwise a card cannot beat another
return False
def __eq__(self, other):
return self.__repr__() == other.__repr__()
def __le__(self, other):
return self < other or self == other
def __ge__(self, other):
return self > other or self == other