-
Notifications
You must be signed in to change notification settings - Fork 1
/
take5.py
362 lines (316 loc) · 12.3 KB
/
take5.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from abc import ABC
from random import shuffle, choice, randrange
from math import exp
class CardTooLowException(Exception):
pass
class Card(int):
@property
def points(self):
if self == 55:
return 7
elif self in [11, 22, 33, 44, 66, 77, 88, 99]:
return 5
elif self in [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
return 3
elif self in [5, 15, 25, 35, 45, 65, 75, 85, 95]:
return 2
else:
return 1
class Row:
CARD_LIMIT = 5
def __init__(self) -> None:
self.cards = []
def __repr__(self) -> str:
return " ".join([f"{c:3}" for c in self.cards])
@property
def length(self):
return len(self.cards)
@property
def empty(self) -> bool:
return self.length == 0
@property
def full(self) -> bool:
return self.length >= self.CARD_LIMIT
@property
def head(self) -> Card:
return self.cards[-1] if not self.empty else None
@property
def points(self) -> int:
return sum([card.points for card in self.cards])
def clear(self, new_card : Card = None) -> int:
pts = self.points
if new_card:
self.cards = [new_card]
else:
self.cards = []
return pts
def add_card(self, card : Card) -> int:
"""
Returns the number of points for the player if the row was full, otherwise zero
"""
if not self.empty and card <= self.head:
raise CardTooLowException()
if self.full:
pts = self.clear()
else:
pts = 0
self.cards.append(card)
return pts
class Board:
NR_ROWS = 4
def __init__(self) -> None:
self.rows = [Row() for _ in range(self.NR_ROWS)]
def __repr__(self) -> str:
return "\n".join([str(r) for r in self.rows])
def __iter__(self) -> Row:
for row in self.rows:
yield row
def clear(self):
for row in self.rows:
row.clear()
def play_card(self, card : Card) -> int:
"""
Returns:
-1 if card is lower than any rows -> player can choose which row to take
p otherwise, p being the number of points for the player
"""
# if any row is empty, card is played there
for row in self.rows:
if row.empty:
return row.add_card(card)
heads = [r.head for r in self.rows]
# check if played card lower than any heads
if not any([i < card for i in heads]):
return -1
# find highest head lower than played card
head = max(i for i in heads if i < card)
row_index = heads.index(head)
return self.rows[row_index].add_card(card)
def clear_row(self, row_index : int, new_card : Card) -> int:
return self.rows[row_index].clear(new_card)
class Player(ABC):
def __init__(self, id) -> None:
self.id = id
self.points = 0
def receive_hand(self, hand : list[Card]):
self.hand = hand
def add_points(self, points : int):
self.points += points
def clear_points(self):
self.points = 0
def choose_card(self, board : Board) -> Card:
"""Returns a card"""
raise NotImplementedError()
def choose_row(self, board : Board) -> int:
"""Returns a row index"""
raise NotImplementedError()
class ManualPlayer(Player):
def choose_card(self, board : Board) -> Card:
print(f"Player '{self.id}' has to choose a card to play")
print(f"Hand: {sorted(self.hand)}")
card = None
while card not in self.hand:
card_input = input("Card to play: ")
try:
card = Card(card_input)
except:
continue
self.hand.remove(card)
return card
def choose_row(self, board : Board) -> int:
"""Returns a row index"""
print(f"Player '{self.id}' has to choose a row to receive")
print(f"Points per row: {[row.points for row in board]}")
row = None
while row not in [1, 2, 3, 4]:
row_input = input("Row index to pick (1, 2, 3, 4): ")
try:
row = int(row_input)
except:
continue
return row - 1
class MinimumRowPointPlayer(Player):
def choose_row(self, board : Board) -> int:
"""Choose the row with lowest number of points"""
row_points = [row.points for row in board]
return row_points.index(min(row_points))
class RandomPlayer(MinimumRowPointPlayer):
def choose_card(self, board : Board) -> Card:
"""Pick a random card"""
card = self.hand.pop(randrange(len(self.hand)))
return card
class AscendingPlayer(MinimumRowPointPlayer):
def choose_card(self, board : Board) -> Card:
"""Pick the lowest card available"""
self.hand.sort(reverse=True)
card = self.hand.pop()
return card
class DescendingPlayer(MinimumRowPointPlayer):
def choose_card(self, board : Board) -> Card:
"""Pick the highest card available"""
self.hand.sort()
card = self.hand.pop()
return card
class SmallestGapPlayer(MinimumRowPointPlayer):
def choose_card(self, board : Board) -> Card:
"""Pick the card with the smallest gap to a not-full row head"""
heads = [row.head for row in board if not row.full]
best_card_so_far = self.hand[0]
smallest_gap_so_far = 1000
for card in self.hand:
# try to avoid picking card lower than any heads
if not any([head < card for head in heads]):
continue
# find highest head lower than played card
head = max([i for i in heads if i < card])
gap = card - head
if gap < smallest_gap_so_far:
best_card_so_far = card
smallest_gap_so_far = gap
self.hand.remove(best_card_so_far)
return best_card_so_far
class ShortestRowPlayer(MinimumRowPointPlayer):
def choose_card(self, board : Board) -> Card:
"""Pick the lowest card for the shortest row, with gap as tiebreaker"""
heads = [row.head for row in board]
lengths = [row.length for row in board]
best_card_so_far = self.hand[0]
shortest_length_so_far = 6
best_card_gap_so_far = 105
for card in self.hand:
# try to avoid picking card lower than any heads
if not any([head < card for head in heads]):
continue
# find highest head lower than played card
head = max([i for i in heads if i < card])
index = heads.index(head)
length = lengths[index]
gap = card - head
if length < shortest_length_so_far or (length == shortest_length_so_far and gap < best_card_gap_so_far):
best_card_so_far = card
shortest_length_so_far = length
best_card_gap_so_far = gap
self.hand.remove(best_card_so_far)
return best_card_so_far
class CostFunPlayer(MinimumRowPointPlayer):
def __init__(self, id, num_players : int, alpha : float = 0.3) -> None:
super().__init__(id)
self.num_players = num_players
self.alpha = alpha
def choose_card(self, board: Board) -> Card:
heads = [row.head for row in board]
lengths = [row.length for row in board]
points = [row.points for row in board]
def cost_fun(card : Card) -> float:
if not any([head < card for head in heads]):
# have to take a row, assuming min points row
#TODO: differentiate within these cards
cost = min(points)
return cost
head = max([i for i in heads if i < card])
index = heads.index(head)
length = lengths[index]
gap = card - head
pts = points[index]
limit = board.rows[index].CARD_LIMIT
cards_before_limit = limit - length
if cards_before_limit == 0:
est_chance_of_taking = 1.0
elif self.num_players <= cards_before_limit or gap == 1:
est_chance_of_taking = 0.0
else:
space_chance = 1.0 - cards_before_limit / self.num_players
gap_chance = 1 - exp(-self.alpha * (gap - 1))
est_chance_of_taking = space_chance * gap_chance
cost = pts * est_chance_of_taking
return cost
best_value_so_far = 0.0
best_card_so_far = None
for card in self.hand:
value = 1 / (cost_fun(card) + 0.001)
if value > best_value_so_far:
best_value_so_far = value
best_card_so_far = card
self.hand.remove(best_card_so_far)
return best_card_so_far
class Take5:
NR_TURNS = 10
NR_CARDS = 104
def __init__(self, players : list[Player], quiet : bool = False) -> None:
self.players = players
self.num_players = len(players)
if self.num_players > 10:
raise ValueError("Too many players")
self.board = Board()
if any([isinstance(player, ManualPlayer) for player in players]) and quiet:
raise ValueError("Can't play in quiet mode if manual players are playing")
self.quiet = quiet
def deal(self):
self.board.clear()
deck = [Card(i) for i in range(1, self.NR_CARDS + 1)]
shuffle(deck)
for player in self.players:
deck, hand = deck[:-self.NR_TURNS], deck[-self.NR_TURNS:]
player.receive_hand(hand)
for row in self.board:
row.add_card(deck.pop())
def print(self, s):
if not self.quiet:
print(s)
def round(self):
self.print("\n\nNEW ROUND")
self.deal()
for i in range(self.NR_TURNS):
self.print("\n-----------------")
self.print(f"Turn {i + 1}")
self.print("-----------------")
self.print(self.board)
played_cards = dict()
for player in self.players:
played_cards[player] = player.choose_card(self.board)
card_players = {card : player for player, card in played_cards.items()}
played_cards = list(played_cards.values())
played_cards.sort()
for card in played_cards:
player = card_players[card]
self.print(f"Player {player.id} plays {card}")
points = self.board.play_card(card)
if points == -1:
self.print(f"Player {player.id} has to choose a row to take")
row_index = player.choose_row(self.board)
self.print(f"Player {player.id} takes row {row_index + 1}")
points = self.board.clear_row(row_index, card)
if points != 0:
self.print(f"Player {player.id} receives {points} points")
player.add_points(points)
self.print(self.board)
self.print("-----------------")
self.print("Round results:")
for player in self.players:
self.print(f"Player {player.id}: {player.points}")
return {player.id: player.points for player in self.players}
if __name__ == "__main__":
NR_ROUNDS = 10000
# players = [RandomPlayer(i+1) for i in range(4)]
players = [
# ManualPlayer('Jitske'),
# ManualPlayer('Martijn'),
# RandomPlayer('randbot 1'),
# RandomPlayer('randbot 2'),
# RandomPlayer('randbot 3'),
CostFunPlayer('martijnbot 1', num_players=4, alpha=0.3),
ShortestRowPlayer('martijnbot 2'),
DescendingPlayer('martijnbot 3'),
SmallestGapPlayer('martijnbot 4'),
]
take5 = Take5(players, quiet=True)
stats = dict()
for round in range(NR_ROUNDS):
round_stats = take5.round()
stats = {p.id: stats.get(p.id, 0) + round_stats.get(p.id, 0) for p in players}
for player in players:
player.clear_points()
print(f"Point totals after {NR_ROUNDS} rounds:")
for player in players:
pts = stats.get(player.id, 0)
print(f"Player {player.id}: {pts} ({pts / NR_ROUNDS} per round)")