forked from pgwhalen/euchre_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zachsim_player.py
252 lines (182 loc) · 6.49 KB
/
zachsim_player.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import utils
from player import Player
PLAYER_DEBUG = 0
class ZachSimPlayer(Player):
def __init__(self, name):
super().__init__(name)
def action(self, trick):
return super().action(trick)
def action(self, trick):
card_to_play = None
if PLAYER_DEBUG:
print("DEBUG: === action ===")
# playing the first card of the trick
if not trick:
# lead: play highest trump
card_to_play = self.highest_trump()
# lead: if no trump, highest non-trump
if not card_to_play:
card_to_play = self.highest_non_trump()
# not playing the first card of the trick
else:
# follow suit: highest card, iff you can beat the leader
card_to_play = self.highest_of_suit_beats_lead(trick[0])
# follow suit: throw low
if not card_to_play:
card_to_play = self.lowest_of_suit(trick[0])
# can't follow suit: throw trump
if not card_to_play:
card_to_play = self.lowest_of_suit(None)
# can't follow suit or no trump
if not card_to_play:
card_to_play = self.lowest_overall()
if not card_to_play:
# TODO: blow up!
pass
return card_to_play
def call(self, top_card):
return super().call(top_card)
def discard(self):
return super().discard()
def end_call(self, caller_position, trump):
return super().end_call(caller_position, trump)
def end_trick(self):
return super().end_trick()
def has_suit(self,suit):
return super().has_suit(suit)
def highest_trump(self):
card_to_play = None
trumps = []
if PLAYER_DEBUG:
print("DEBUG: --- highest_trump ---")
# get list of trump cards in hand
for card in self.game.hand_for(self):
if card[1] == self.game._trump or card == self.game._leftbower:
trumps.append(card)
if PLAYER_DEBUG:
print("DEBUG: trumps: ", trumps)
# sort trumps highest to lowest
if (len(trumps) > 0):
card_order = [ 'J', 'A', 'K', 'Q', 'T', '9' ]
#suit_order = [ self.game._trump, utils.same_color(self.game._trump) ]
sorted_trumps = sorted(trumps, key=lambda c: (card_order.index(c[0])))
#TODO: sort to get the bowers in here
if PLAYER_DEBUG:
print("DEBUG: sorted_trumps: ", sorted_trumps)
card_to_play = sorted_trumps[0]
if PLAYER_DEBUG:
print("DEBUG: card_to_play: ", card_to_play)
return card_to_play
def highest_non_trump(self):
card_to_play = None
nontrumps = []
if PLAYER_DEBUG:
print("DEBUG: --- highest_nontrump ---")
# get list of non-trump cards in hand
for card in self.game.hand_for(self):
if card[1] != self.game._trump and card != self.game._leftbower:
nontrumps.append(card)
if PLAYER_DEBUG:
print("DEBUG: nontrumps: ", nontrumps)
# sort non-trumps lowest to highest
if (len(nontrumps) > 0):
card_order = [ 'J', 'A', 'K', 'Q', 'T', '9' ]
sorted_nontrumps = sorted(nontrumps, key=lambda c: (card_order.index(c[0])))
if PLAYER_DEBUG:
print("DEBUG: sorted_nontrumps: ", sorted_nontrumps)
card_to_play = sorted_nontrumps[0]
if PLAYER_DEBUG:
print("DEBUG: card_to_play: ", card_to_play)
return card_to_play
def highest_of_suit_beats_lead(self,lead):
card_to_play = None
extra_card = None
possibles = []
if PLAYER_DEBUG:
print("DEBUG: --- highest_of_suit_beats_lead ---")
# if the left bower was led, then we need to follow suit with trump
# otherwise just follow the suit that was led
if lead == self.game._leftbower:
suit_to_follow = self.game._trump
else:
suit_to_follow = lead[1]
if suit_to_follow == self.game._trump:
extra_card = self.game._leftbower
# get list of cards in hand of the same suit (including the left bower if suit is trump)
for card in self.game.hand_for(self):
if card[1] == suit_to_follow or card == extra_card:
possibles.append(card)
if PLAYER_DEBUG:
print("DEBUG: possibles: ", possibles)
# sort highest to lowest and remove anything that won't beat the lead
if (len(possibles) > 0):
card_order = [ 'Jx', 'Jy', 'Ax', 'Kx', 'Qx', 'Tx', '9x' ]
for n in range(len(card_order)):
if lead == self.game._leftbower:
card_order[n] = card_order[n].replace('x', utils.same_color(lead[1]))
card_order[n] = card_order[n].replace('y', lead[1])
else:
card_order[n] = card_order[n].replace('x', lead[1])
card_order[n] = card_order[n].replace('y', utils.same_color(lead[1]))
sorted_possibles = sorted(possibles, key=lambda c: (card_order.index(c)))
if PLAYER_DEBUG:
print("DEBUG: sorted_possibles: ", sorted_possibles)
# anything with a smaller index than the lead card will work
lead_index = card_order.index(lead)
for card in sorted_possibles:
if (card_order.index(card) < lead_index):
card_to_play = card
break
if PLAYER_DEBUG:
print("DEBUG: card_to_play: ", card_to_play)
return card_to_play
def lowest_of_suit(self,lead):
card_to_play = None
possibles = []
if PLAYER_DEBUG:
print("DEBUG: --- lowest_of_suit ---")
# if lead not given, use trump
if lead == None or lead == self.game._leftbower:
suit = self.game._trump
else:
suit = lead[1]
if PLAYER_DEBUG:
print("DEBUG: lead: ", lead)
print("DEBUG: suit: ", suit)
# get list of cards in hand of the same suit (including left bower if suit is trump)
for card in self.game.hand_for(self):
if card[1] == suit or (suit == self.game._trump and card == self.game._leftbower):
possibles.append(card);
if PLAYER_DEBUG:
print("DEBUG: possibles: ", possibles)
# sort lowest to highest
if (len(possibles) > 0):
card_order = [ 'Jx', 'Jy', 'Ax', 'Kx', 'Qx', 'Tx', '9x' ]
for n in range(len(card_order)):
card_order[n] = card_order[n].replace('x', suit)
card_order[n] = card_order[n].replace('y', utils.same_color(suit))
sorted_possibles = sorted(possibles, key=lambda c: (card_order.index(c)), reverse=True)
if PLAYER_DEBUG:
print("DEBUG: sorted_possibles: ", sorted_possibles)
card_to_play = sorted_possibles[0]
if PLAYER_DEBUG:
print("DEBUG: card_to_play: ", card_to_play)
return card_to_play
def lowest_overall(self):
card_to_play = None
possibles = []
if PLAYER_DEBUG:
print("DEBUG: --- lowest_overall ---")
possibles = self.game.hand_for(self).copy()
if PLAYER_DEBUG:
print("DEBUG: possibles: ", possibles)
# sort lowest to highest
if (len(possibles) > 0):
card_order = [ 'J', 'A', 'K', 'Q', 'T', '9' ]
sorted_possibles = sorted(possibles, key=lambda c: (card_order.index(c[0])), reverse=True)
if PLAYER_DEBUG:
print("DEBUG: sorted_possibles: ", sorted_possibles)
card_to_play = sorted_possibles[0]
if PLAYER_DEBUG:
print("DEBUG: card_to_play: ", card_to_play)
return card_to_play