-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_thanks_old.py
362 lines (274 loc) · 11.7 KB
/
no_thanks_old.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
import random
from dataclasses import dataclass
ACTION_TAKE = 0
ACTION_PASS = 1
def diff(first, second):
second = set(second)
return [item for item in first if item not in second]
class NoThanksState():
def __init__(self, game):
self.game = game
# setup state
self.n_cards = self.game.max_card - self.game.min_card + 1
self.deck = [True for i in range(self.n_cards)]
self.n_cards_left = self.n_cards - self.game.n_omit_cards
self.current_player = 0
self.chips = [self.game.start_coins for i in range(self.game.n_players)]
self.player_cards = [[] for i in range(self.game.n_players)]
# draw a card from the deck
cards_in_deck = [i for i in range(self.game.min_card, self.game.max_card + 1) if self.deck[i - self.game.min_card]]
self.card_in_play = random.choice(cards_in_deck)
self.n_cards_left -= 1
self.coins_in_play = 0
def legal_actions(self):
if self.coins[self.current_player] == 0:
return [ACTION_PASS]
else:
return [ACTION_TAKE, ACTION_PASS]
def apply_action(self, action):
if action == ACTION_TAKE:
self.cards[self.current_player].append(self.card_in_play)
self.chips[self.current_player] += self.coins_in_play
self.coins_in_play = 0
if self.n_cards_left > 0:
cards_in_deck = [i for i in range(self.game.min_card, self.game.max_card + 1) if self.deck[i - self.game.min_card]]
self.card_in_play = random.choice(cards_in_deck)
self.deck[self.card_in_play - self.game.min_card] = False
self.n_cards_left -= 1
else:
self.card_in_play = None
else: # action == ACTION_PASS
self.chips[self.current_player] -= 1
self.coins_in_play += 1
self.current_player += 1
if self.current_player == self.game.n_players:
self.current_player = 0
def is_terminal(self):
if self.n_cards_left == 0 and self.card_in_play == None:
return True
else:
return False
class NoThanksGame():
def __init__(self, n_players = 3, min_card = 3, max_card = 35, start_coins = 11, n_omit_cards = 9):
self.n_players = n_players
self.min_card = min_card
self.max_card = max_card
self.start_coins = start_coins
self.n_omit_cards = n_omit_cards
def starting_state(self):
coins = [self.config.start_coins for i in range(self.n_players)]
cards = [[] for i in range(self.n_players)]
card_in_play = random.choice(self.full_deck)
coins_in_play = 0
n_cards_in_deck = self.n_cards - 1 - self.n_omit_cards
current_player = 0
return coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player)
def next_state(self, state, action):
pass
def is_legal(self, state, action):
pass
def legal_actions(self, state):
pass
def pack_state(self, state):
coins, cards, details = state
packed_state = tuple(coins), tuple(map(tuple, cards)), details
return packed_state
def unpack_state(self, packed_state):
coins, cards, details = packed_state
coins = list(coins)
cards = list(map(list, cards))
return coins, cards, details
def is_ended(self, state):
pass
def compute_scores(self, state):
pass
def winner(self, state):
pass
def basic_display_state(self, state):
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
print("Coins: {0}".format(coins))
print("Cards: {0}".format(cards))
print("Card in play: {0}".format(card_in_play))
print("Coins: {0}".format(coins_in_play))
print("Player: {0}".format(current_player))
def display_scores(self, state):
scores = self.compute_scores(state)
print("")
print("--- Scores ---")
for i in range(self.n_players):
print("Player {0}: {1}".format(i, scores[i]))
print("")
def display_state(self, state):
state = self.unpack_state(state)
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if self.n_players == 3:
print(""
@dataclass
class NoThanksConfig:
min_card: int = 3
max_card: int = 35
start_coins: int = 11
n_omit_cards: int = 9
class NoThanksBoard():
def __init__(self, n_players = 3, config = NoThanksConfig()):
self.n_players = n_players
self.start_coins = config.start_coins
self.min_card = config.min_card
self.max_card = config.max_card
self.full_deck = list(range(self.min_card, self.max_card+1))
self.n_omit_cards = config.n_omit_cards
self.n_cards = self.max_card - self.min_card + 1
# state: ((player coins),(player cards),(card in play, coins in play, n_cards_remaining, current player))
def starting_state(self):
coins = [self.start_coins for i in range(self.n_players)]
cards = [[] for i in range(self.n_players)]
card_in_play = random.choice(self.full_deck)
coins_in_play = 0
n_cards_in_deck = self.n_cards - 1 - self.n_omit_cards
current_player = 0
return coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player)
def next_state(self, state, action):
state = self.unpack_state(state)
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if action == ACTION_TAKE:
cards[current_player].append(card_in_play)
coins[current_player] += coins_in_play
all_player_cards = [card for player_cards in cards for card in player_cards]
cards_in_deck = diff(self.full_deck, all_player_cards)
if cards_in_deck and n_cards_in_deck > 0:
random.shuffle(list(cards_in_deck))
card_in_play = random.choice(cards_in_deck)
n_cards_in_deck -= 1
else:
card_in_play = None
coins_in_play = 0
else:
coins[current_player] -= 1
coins_in_play += 1
current_player += 1
if current_player == self.n_players:
current_player = 0
next_state = coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player)
return self.pack_state(next_state)
def is_legal(self, state, action):
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if coins[current_player] == 0 and action == ACTION_PASS:
return False
else:
if card_in_play is None:
return False
else:
return True
def legal_actions(self, state):
actions = []
actions.append(ACTION_TAKE)
if self.is_legal(state, ACTION_PASS):
actions.append(ACTION_PASS)
return actions
def pack_state(self, state):
coins, cards, details = state
packed_state = tuple(coins), tuple(map(tuple, cards)), details
return packed_state
def unpack_state(self, packed_state):
coins, cards, details = packed_state
coins = list(coins)
cards = list(map(list, cards))
return coins, cards, details
def is_ended(self, state):
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if n_cards_in_deck == 0 and card_in_play == None:
return True
else:
return False
def compute_scores(self, state):
state = self.unpack_state(state)
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
scores = []
for p_idx in range(self.n_players):
cards[p_idx].sort()
score = 0
if cards[p_idx]:
score += cards[p_idx][0]
last_card = cards[p_idx][0]
for card_idx in range(1, len(cards[p_idx])):
new_card = cards[p_idx][card_idx]
if not new_card == last_card + 1:
score += new_card
last_card = new_card
score -= coins[p_idx]
scores.append(score)
return scores
def winner(self, state):
state = self.unpack_state(state)
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if not self.is_ended(state):
return None
scores = self.compute_scores(state)
min_score = 1000
lowest_scorers = []
# get lowest scorers (could be more than one)
for i, score in enumerate(scores):
if score < min_score:
lowest_scorers = [i]
min_score = score
if score <= min_score:
lowest_scorers.append(i)
# if players are tied on lowest score, get the one with the fewest cards
if len(lowest_scorers) > 1:
min_n_cards = 1000
for i in lowest_scorers:
n_cards = len(cards[i])
if n_cards < min_n_cards:
lowest_card_players = [i]
min_n_cards = n_cards
elif n_cards <= min_n_cards:
lowest_card_players.append(i)
if len(lowest_card_players) > 1:
winner = lowest_card_players[0]
else: # if still tied, pick a random winner (not the official rules)
winner = random.choice(lowest_card_players)
else:
winners = lowest_scorers[0]
return winner
def basic_display_state(self, state):
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
print("Coins: {0}".format(coins))
print("Cards: {0}".format(cards))
print("Card in play: {0}".format(card_in_play))
print("Coins: {0}".format(coins_in_play))
print("Player: {0}".format(current_player))
def display_scores(self, state):
scores = self.compute_scores(state)
print("")
print("--- Scores ---")
for i in range(self.n_players):
print("Player {0}: {1}".format(i, scores[i]))
print("")
def display_state(self, state):
state = self.unpack_state(state)
coins, cards, (card_in_play, coins_in_play, n_cards_in_deck, current_player) = state
if self.n_players == 3:
print("")
print("-------------------------------------------------------")
print("")
print("--- Player 1 ---\t\t\t---Player 2 ---")
print(" Cards: {0}".format(sorted(cards[1])))
print("\t\t\t\t\t Cards: {0}".format(sorted(cards[2])))
print(" Coins: {0}".format(coins[1]))
print("\t\t\t\t\t Coins: {0}".format(coins[2]))
print("")
print("\t\t In play: [{0}]".format(card_in_play))
print("\t\t Coins: {0}".format(coins_in_play))
print("")
print("")
print("--- Player 0 (You) ---")
print(" Cards: {0}".format(sorted(cards[0])))
print(" Coins: {0}".format(coins[0]))
print("")
def pack_action(self, notation):
if notation == "y" or notation == "Y":
return ACTION_TAKE
else:
return ACTION_PASS
def current_player(self, state):
return state[2][3]