forked from dymbe/artificial-uno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunotypes.py
376 lines (303 loc) · 9.89 KB
/
unotypes.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from enum import Enum
from dataclasses import dataclass
from typing import Literal
import itertools
import random
from unoexceptions import IllegalMoveException
from copy import deepcopy
class Color(str, Enum):
RED = "RED"
GREEN = "GREEN"
BLUE = "BLUE"
YELLOW = "YELLOW"
class Sign(str, Enum):
ZERO = "ZERO"
ONE = "ONE"
TWO = "TWO"
THREE = "THREE"
FOUR = "FOUR"
FIVE = "FIVE"
SIX = "SIX"
SEVEN = "SEVEN"
EIGHT = "EIGHT"
NINE = "NINE"
SKIP = "SKIP"
REVERSE = "REVERSE"
DRAW_TWO = "DRAW_TWO"
DRAW_FOUR = "DRAW_FOUR"
CHANGE_COLOR = "CHANGE_COLOR"
@property
def is_wild(self):
return self == Sign.DRAW_FOUR or self == Sign.CHANGE_COLOR
@property
def is_action(self):
return (self == Sign.SKIP or
self == Sign.REVERSE or
self == Sign.DRAW_TWO or
self.is_wild)
@property
def is_number(self):
return not self.is_wild and not self.is_action
@property
def score(self) -> int:
match self:
case "ZERO":
return 0
case "ONE":
return 1
case "TWO":
return 2
case "THREE":
return 3
case "FOUR":
return 4
case "FIVE":
return 5
case "SIX":
return 6
case "SEVEN":
return 7
case "EIGHT":
return 8
case "NINE":
return 9
case "SKIP":
return 20
case "REVERSE":
return 20
case "DRAW_TWO":
return 20
case "DRAW_FOUR":
return 50
case "CHANGE_COLOR":
return 50
@dataclass(frozen=True)
class Card:
sign: Sign
color: Color | None
@property
def is_wild(self) -> bool:
return self.sign.is_wild
@property
def is_action(self):
return self.sign.is_action
@property
def is_number(self):
return self.sign.is_number
def stacks_on(self, card):
return (card is None or
self.is_wild or
self.color == card.color or
self.sign == card.sign)
def __eq__(self, other):
if other is None:
return False
elif self.is_wild:
return self.sign == other.sign
else:
return self.sign == other.sign and self.color == other.color
def __post_init__(self):
if not self.is_wild and self.color is None:
raise TypeError("Color can only be None for DRAW_FOUR and CHANGE_COLOR cards")
@dataclass
class Hand:
cards: list[Card]
def __init__(self, initial_cards: list[Card]):
self.cards = []
self.add(initial_cards)
def add(self, added_cards: list[Card]):
for card in added_cards:
if card.is_wild and card.color is not None:
self.cards.append(Card(card.sign, None))
else:
self.cards.append(card)
def remove(self, card: Card):
if card in self:
if card.is_wild:
self.cards.remove(Card(card.sign, None))
else:
self.cards.remove(card)
return card
else:
raise IllegalMoveException("Card not in hand")
def __iter__(self):
return self.cards.__iter__()
def __len__(self):
return len(self.cards)
@dataclass
class DrawPile:
cards: list[Card]
def __init__(self):
self.cards = []
non_wild_signs = [Sign(sign) for sign in Sign if not Sign(sign).is_wild]
for c, s in itertools.product(Color, non_wild_signs):
if s == Sign.ZERO:
self.cards.append(Card(sign=s, color=c))
else:
self.cards.extend([Card(sign=s, color=c) for _ in range(2)])
# Add wildcards
self.cards.extend([Card(sign=Sign.CHANGE_COLOR, color=None) for _ in range(4)])
self.cards.extend([Card(sign=Sign.DRAW_FOUR, color=None) for _ in range(4)])
random.shuffle(self.cards)
def top(self):
return self.cards[-1]
def shuffle(self):
random.shuffle(self.cards)
def pop(self) -> Card:
card = self.cards[-1]
self.cards = self.cards[:-1]
return card
def draw_cards(self, amount) -> list[Card]:
drawn_cards = self.cards[-amount:]
self.cards = self.cards[:-amount]
return drawn_cards
def distribute_hands(self, hand_amount) -> list[Hand]:
return [Hand(self.draw_cards(7)) for _ in range(hand_amount)]
def __len__(self):
return len(self.cards)
@dataclass
class DiscardPile:
cards: list[Card]
def __init__(self, initial_card: Card):
self.cards = [initial_card]
def top(self):
return self.cards[-1]
def stack(self, card):
if card.stacks_on(self.top()):
self.cards.append(card)
else:
raise IllegalMoveException(f"{card} can not be placed on top of {self.top()}")
def __len__(self):
return len(self.cards)
@dataclass(frozen=True)
class PlayCard:
card: Card
@dataclass(frozen=True)
class DrawCard:
pass
@dataclass(frozen=True)
class SkipTurn:
pass
@dataclass(frozen=True)
class AcceptDrawFour:
pass
@dataclass(frozen=True)
class ChallengeDrawFour:
pass
Action = PlayCard | DrawCard | SkipTurn | AcceptDrawFour | ChallengeDrawFour
@dataclass(frozen=True)
class Observation:
game_won: bool
is_initial_state: bool
agent_idx: int
top_card: Card
hand: Hand
cards_left: list[int]
draw_pile_size: int
discard_pile_size: int
current_agent_idx: int
direction: Literal[-1, 1]
previously_drawn_card: Card | None
can_challenge_draw_four: bool
draw_four_stacked_on: Card | None
challenger_idx: int | None
revealed_hand_idx: int | None
revealed_hand: Hand | None
scores: list[int]
def action_space(self) -> set[Action]:
if self.agent_idx != self.current_agent_idx:
return set()
if self.can_challenge_draw_four:
return {AcceptDrawFour(), ChallengeDrawFour()}
action_space = set()
playable_cards = []
if self.previously_drawn_card:
action_space.add(SkipTurn())
if self.previously_drawn_card.stacks_on(self.top_card):
playable_cards.append(self.previously_drawn_card)
else:
action_space.add(DrawCard())
for card in self.hand:
if card.stacks_on(self.top_card):
playable_cards.append(card)
for card in playable_cards:
if card.is_wild:
action_space = action_space.union([PlayCard(Card(card.sign, Color(color))) for color in Color])
else:
action_space.add(PlayCard(card))
return action_space
# Raises exception
def assert_valid(self, action: Action):
assert self.agent_idx == self.current_agent_idx
# Player has to either accept or challenge a played DRAW_FOUR-card
if self.can_challenge_draw_four:
assert action in [AcceptDrawFour(), ChallengeDrawFour()]
else:
assert action not in [AcceptDrawFour(), ChallengeDrawFour()]
match action:
case PlayCard(card):
assert card.color is not None
assert card in self.hand
assert card.stacks_on(self.top_card)
assert self.previously_drawn_card is None or card == self.previously_drawn_card
case DrawCard():
assert self.previously_drawn_card is None
case SkipTurn():
assert self.previously_drawn_card is not None
def is_valid_action(self, action: Action) -> bool:
try:
self.assert_valid(action)
return True
except AssertionError:
return False
@dataclass(frozen=True)
class GameState:
game_won: bool
is_initial_state: bool
draw_pile: DrawPile
hands: list[Hand]
discard_pile: DiscardPile
current_agent_idx: int
direction: Literal[-1, 1]
previously_drawn_card: Card | None
can_challenge_draw_four: bool
draw_four_stacked_on: Card | None
challenger_idx: int | None
revealed_hand_idx: int | None
revealed_hand: Hand | None
scores: list[int]
def observe(self, agent_idx) -> Observation:
if agent_idx == self.current_agent_idx:
previously_drawn_card = self.previously_drawn_card
else:
previously_drawn_card = None
if self.challenger_idx == agent_idx:
revealed_hand = self.revealed_hand
else:
revealed_hand = None
return deepcopy(Observation(
game_won=self.game_won,
is_initial_state=self.is_initial_state,
agent_idx=agent_idx,
hand=self.hands[agent_idx],
cards_left=[len(hand) for hand in self.hands],
draw_pile_size=len(self.draw_pile),
discard_pile_size=len(self.discard_pile),
top_card=self.discard_pile.top(),
current_agent_idx=self.current_agent_idx,
direction=self.direction,
previously_drawn_card=previously_drawn_card,
can_challenge_draw_four=self.can_challenge_draw_four,
draw_four_stacked_on=self.draw_four_stacked_on,
challenger_idx=self.challenger_idx,
revealed_hand_idx=self.revealed_hand_idx,
revealed_hand=revealed_hand,
scores=self.scores))
def assert_valid(self, agent_idx, action):
self.observe(agent_idx).assert_valid(action)
def is_valid_action(self, agent_idx, action) -> bool:
try:
self.assert_valid(agent_idx, action)
return True
except AssertionError:
return False