-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunogame.py
177 lines (139 loc) · 6.2 KB
/
unogame.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
from unotypes import *
from typing import Literal
from copy import deepcopy
class UnoGame:
def __init__(self, agent_amount: int, winning_score=500):
assert 2 <= agent_amount <= 10
self.agent_amount = agent_amount
self.winning_score = winning_score
self.game_won = False
self.scores = agent_amount * [0]
self.state_log = None
self.is_initial_state = None
self.draw_pile = None
self.hands = None
self.discard_pile = None
self.current_agent_idx = None
self.direction = None
self.previously_drawn_card = None
self.can_challenge_draw_four = None
self.draw_four_stacked_on = None
self.revealed_hand_idx = None
self.challenger_idx = None
self.revealed_hand = None
self.init_round()
def init_round(self):
self.is_initial_state = True
self.state_log: list[GameState] = []
self.draw_pile = DrawPile()
self.hands = self.draw_pile.distribute_hands(self.agent_amount)
# Not proper UNO, but implementation is simpler if top card is always a number card
while not self.draw_pile.top().is_number:
self.draw_pile.shuffle()
self.discard_pile = DiscardPile(initial_card=self.draw_pile.pop())
self.current_agent_idx = random.randint(0, len(self.hands) - 1)
self.direction: Literal[-1, 1] = 1
self.previously_drawn_card: Card | None = None
self.can_challenge_draw_four = False
self.draw_four_stacked_on: Card | None = None
self.challenger_idx: int | None = None
self.revealed_hand_idx: int | None = None
self.revealed_hand: Hand | None = None
self.log_state()
self.is_initial_state = False
def gamestate(self) -> GameState:
return GameState(
game_won=self.game_won,
is_initial_state=self.is_initial_state,
draw_pile=self.draw_pile,
hands=self.hands,
discard_pile=self.discard_pile,
current_agent_idx=self.current_agent_idx,
direction=self.direction,
previously_drawn_card=self.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=self.revealed_hand,
scores=self.scores
)
def step(self, agent_idx, action: Action):
self.gamestate().assert_valid(agent_idx, action)
hand = self.hands[agent_idx]
match action:
case PlayCard(card):
increment = 1
match card.sign:
case Sign.SKIP:
increment += 1
case Sign.REVERSE:
self.direction *= -1
case Sign.DRAW_TWO:
increment += 1
self.draw_cards(self.current_agent_idx + self.direction, 2)
case Sign.DRAW_FOUR:
self.can_challenge_draw_four = True
self.draw_four_stacked_on = self.discard_pile.top()
hand.remove(card)
self.discard_pile.stack(card)
if len(hand) == 0:
for hand in self.hands:
for card in hand:
self.scores[self.current_agent_idx] += card.sign.score
if self.scores[self.current_agent_idx] >= self.winning_score:
self.game_won = True
else:
self.init_round()
return
else:
self.next_turn(increment)
case DrawCard():
card_list = self.draw_cards(self.current_agent_idx, 1)
if card_list:
self.previously_drawn_card = card_list[0]
case SkipTurn():
self.next_turn()
case AcceptDrawFour():
self.draw_cards(self.current_agent_idx, 4)
self.next_turn()
self.can_challenge_draw_four = False
self.draw_four_stacked_on = None
case ChallengeDrawFour():
self.challenger_idx = self.current_agent_idx
self.revealed_hand_idx = (self.current_agent_idx - self.direction) % len(self.hands)
self.revealed_hand = deepcopy(self.hands[self.revealed_hand_idx])
if any(card.color == self.draw_four_stacked_on.color for card in self.revealed_hand):
self.draw_cards(self.revealed_hand_idx, 4)
else:
self.draw_cards(self.current_agent_idx, 6)
self.next_turn()
self.can_challenge_draw_four = False
self.draw_four_stacked_on = None
case _:
raise IllegalMoveException(f"Action <{action}> is invalid")
self.log_state()
self.challenger_idx = None
self.revealed_hand_idx = None
self.revealed_hand = None
def next_turn(self, increment=1):
self.current_agent_idx = (self.current_agent_idx + self.direction * increment) % len(self.hands)
self.previously_drawn_card = None
def recycle_discard_pile(self):
self.draw_pile.cards += self.discard_pile.cards[:-1]
self.draw_pile.shuffle()
self.discard_pile = DiscardPile(self.discard_pile.top())
def draw_cards(self, idx: int, amount: int):
idx = idx % len(self.hands)
if len(self.draw_pile) < amount:
self.recycle_discard_pile()
amount = min(len(self.draw_pile), amount) # Just in case the draw pile is still not big enough
cards = self.draw_pile.draw_cards(amount)
self.hands[idx].add(cards)
return cards
def log_state(self):
self.state_log.append(deepcopy(self.gamestate()))
def observations(self, agent_idx) -> list[Observation]:
return [state.observe(agent_idx) for state in self.state_log]
def last_observation(self, agent_idx) -> Observation:
return self.state_log[-1].observe(agent_idx)