-
Notifications
You must be signed in to change notification settings - Fork 6
/
trick.py
52 lines (42 loc) · 1.49 KB
/
trick.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
from relative_data import RelativeData
suit_names = {"S": "Spades", "H": "Hearts", "C": "Clubs", "D": "Diamonds", None: "Empty"}
class Trick(RelativeData):
def __init__(self, reference_id=0):
super().__init__(reference_id)
self.cards = self.data
self.won_by_spade = False
self.suit = None
def add_card(self, card, player_id):
"""Add a newly played card to the trick."""
global_id = self.globalize_id(player_id)
assert len(self.cards) < self.data_size and global_id not in self.cards
if len(self.cards) == 0:
self.suit = card.suit
self.cards[global_id] = card
if card.suit == "S":
self.won_by_spade = True
def get_suit(self):
"""Get the suit of this trick."""
return self.suit
def get_winner(self):
"""Return the player id of the player currently winning trick."""
winning_card = None
winning_id = None
for global_id in self.cards:
card = self.cards[global_id]
if card > winning_card:
winning_card = card
winning_id = global_id
assert len(self.cards) == 0 or winning_card is not None
if winning_card is None:
return None
return self.localize_id(winning_id)
def __str__(self):
t = {i: "??" for i in range(4)}
for i in range(4):
if i in self.cards:
local_id = self.localize_id(i)
t[local_id] = self.cards[i]
# card_str = ", ".join([str(i) + ": " + str(t[i]) for i in range(4)])
card_str = ", ".join([str(t[i]) for i in range(4)])
return suit_names[self.suit] + " Trick: " + card_str + " (Winner: " + str(self.get_winner()) + ")"