-
Notifications
You must be signed in to change notification settings - Fork 30
/
uno.py
388 lines (330 loc) · 11.8 KB
/
uno.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
377
378
379
380
381
382
383
384
385
386
387
388
from random import shuffle, choice
from itertools import product, repeat, chain
COLORS = ['red', 'yellow', 'green', 'blue']
ALL_COLORS = COLORS + ['black']
NUMBERS = list(range(10)) + list(range(1, 10))
SPECIAL_CARD_TYPES = ['skip', 'reverse', '+2']
COLOR_CARD_TYPES = NUMBERS + SPECIAL_CARD_TYPES * 2
BLACK_CARD_TYPES = ['wildcard', '+4']
CARD_TYPES = NUMBERS + SPECIAL_CARD_TYPES + BLACK_CARD_TYPES
class UnoCard:
"""
Represents a single Uno Card, given a valid color and card type.
color: string
card_type: string/int
>>> card = UnoCard('red', 5)
"""
def __init__(self, color, card_type):
self._validate(color, card_type)
self.color = color
self.card_type = card_type
self.temp_color = None
def __repr__(self):
return '<UnoCard object: {} {}>'.format(self.color, self.card_type)
def __str__(self):
return '{}{}'.format(self.color_short, self.card_type_short)
def __eq__(self, other):
return self.color == other.color and self.card_type == other.card_type
def _validate(self, color, card_type):
"""
Check the card is valid, raise exception if not.
"""
if color not in ALL_COLORS:
raise ValueError('Invalid color')
if color == 'black' and card_type not in BLACK_CARD_TYPES:
raise ValueError('Invalid card type')
if color != 'black' and card_type not in COLOR_CARD_TYPES:
raise ValueError('Invalid card type')
@property
def color_short(self):
return self.color[0].upper()
@property
def card_type_short(self):
if self.card_type in ('skip', 'reverse', 'wildcard'):
return self.card_type[0].upper()
else:
return self.card_type
@property
def _color(self):
return self.temp_color if self.temp_color else self.color
@property
def temp_color(self):
return self._temp_color
@temp_color.setter
def temp_color(self, color):
if color is not None:
if color not in COLORS:
raise ValueError('Invalid color')
self._temp_color = color
def playable(self, other):
"""
Return True if the other card is playable on top of this card,
otherwise return False
"""
return (
self._color == other.color or
self.card_type == other.card_type or
other.color == 'black'
)
class UnoPlayer:
"""
Represents a player in an Uno game. A player is created with a list of 7
Uno cards.
cards: list of 7 UnoCards
player_id: int/str (default: None)
>>> cards = [UnoCard('red', n) for n in range(7)]
>>> player = UnoPlayer(cards)
"""
def __init__(self, cards, player_id=None):
if len(cards) != 7:
raise ValueError(
'Invalid player: must be initalised with 7 UnoCards'
)
if not all(isinstance(card, UnoCard) for card in cards):
raise ValueError(
'Invalid player: cards must all be UnoCard objects'
)
self.hand = cards
self.player_id = player_id
def __repr__(self):
if self.player_id is not None:
return '<UnoPlayer object: player {}>'.format(self.player_id)
else:
return '<UnoPlayer object>'
def __str__(self):
if self.player_id is not None:
return str(self.player_id)
else:
return repr(self)
def can_play(self, current_card):
"""
Return True if the player has any playable cards (on top of the current
card provided), otherwise return False
"""
return any(current_card.playable(card) for card in self.hand)
class UnoGame:
"""
Represents an Uno game.
players: int
random: bool (default: True)
>>> game = UnoGame(5)
"""
def __init__(self, players, random=True):
if not isinstance(players, int):
raise ValueError('Invalid game: players must be integer')
if not 2 <= players <= 15:
raise ValueError('Invalid game: must be between 2 and 15 players')
self.deck = self._create_deck(random)
self.players = [
UnoPlayer(self._deal_hand(), n) for n in range(players)
]
self._player_cycle = ReversibleCycle(self.players)
self._current_player = next(self._player_cycle)
self._winner = None
def __next__(self):
"""
Iteration sets the current player to the next player in the cycle.
"""
self._current_player = next(self._player_cycle)
def _create_deck(self, random):
"""
Return a list of the complete set of Uno Cards. If random is True, the
deck will be shuffled, otherwise will be unshuffled.
"""
color_cards = product(COLORS, COLOR_CARD_TYPES)
black_cards = product(repeat('black', 4), BLACK_CARD_TYPES)
all_cards = chain(color_cards, black_cards)
deck = [UnoCard(color, card_type) for color, card_type in all_cards]
if random:
shuffle(deck)
return deck
else:
return list(reversed(deck))
def _deal_hand(self):
"""
Return a list of 7 cards from the top of the deck, and remove these
from the deck.
"""
return [self.deck.pop() for i in range(7)]
@property
def current_card(self):
return self.deck[-1]
@property
def is_active(self):
return all(len(player.hand) > 0 for player in self.players)
@property
def current_player(self):
return self._current_player
@property
def winner(self):
return self._winner
def play(self, player, card=None, new_color=None):
"""
Process the player playing a card.
player: int representing player index number
card: int representing index number of card in player's hand
It must be player's turn, and if card is given, it must be playable.
If card is not given (None), the player picks up a card from the deck.
If game is over, raise an exception.
"""
if not isinstance(player, int):
raise ValueError('Invalid player: should be the index number')
if not 0 <= player < len(self.players):
raise ValueError('Invalid player: index out of range')
_player = self.players[player]
if self.current_player != _player:
raise ValueError('Invalid player: not their turn')
if card is None:
self._pick_up(_player, 1)
next(self)
return
_card = _player.hand[card]
if not self.current_card.playable(_card):
raise ValueError(
'Invalid card: {} not playable on {}'.format(
_card, self.current_card
)
)
if _card.color == 'black':
if new_color not in COLORS:
raise ValueError(
'Invalid new_color: must be red, yellow, green or blue'
)
if not self.is_active:
raise ValueError('Game is over')
played_card = _player.hand.pop(card)
self.deck.append(played_card)
card_color = played_card.color
card_type = played_card.card_type
if card_color == 'black':
self.current_card.temp_color = new_color
if card_type == '+4':
next(self)
self._pick_up(self.current_player, 4)
elif card_type == 'reverse':
self._player_cycle.reverse()
elif card_type == 'skip':
next(self)
elif card_type == '+2':
next(self)
self._pick_up(self.current_player, 2)
if self.is_active:
next(self)
else:
self._winner = _player
self._print_winner()
def _print_winner(self):
"""
Print the winner name if available, otherwise look up the index number.
"""
if self.winner.player_id:
winner_name = self.winner.player_id
else:
winner_name = self.players.index(self.winner)
print("Player {} wins!".format(winner_name))
def _pick_up(self, player, n):
"""
Take n cards from the bottom of the deck and add it to the player's
hand.
player: UnoPlayer
n: int
"""
penalty_cards = [self.deck.pop(0) for i in range(n)]
player.hand.extend(penalty_cards)
class ReversibleCycle:
"""
Represents an interface to an iterable which can be infinitely cycled (like
itertools.cycle), and can be reversed.
Starts at the first item (index 0), unless reversed before first iteration,
in which case starts at the last item.
iterable: any finite iterable
>>> rc = ReversibleCycle(range(3))
>>> next(rc)
0
>>> next(rc)
1
>>> rc.reverse()
>>> next(rc)
0
>>> next(rc)
2
"""
def __init__(self, iterable):
self._items = list(iterable)
self._pos = None
self._reverse = False
def __next__(self):
if self.pos is None:
self.pos = -1 if self._reverse else 0
else:
self.pos = self.pos + self._delta
return self._items[self.pos]
@property
def _delta(self):
return -1 if self._reverse else 1
@property
def pos(self):
return self._pos
@pos.setter
def pos(self, value):
self._pos = value % len(self._items)
def reverse(self):
"""
Reverse the order of the iterable.
"""
self._reverse = not self._reverse
class AIUnoGame:
def __init__(self, players):
self.game = UnoGame(players)
self.player = choice(self.game.players)
self.player_index = self.game.players.index(self.player)
print('The game begins. You are Player {}.'.format(self.player_index))
self.print_hand()
while self.game.is_active:
print()
next(self)
def __next__(self):
game = self.game
player = game.current_player
player_id = player.player_id
current_card = game.current_card
if player == self.player:
print('Current card: {}, color: {}'.format(
game.current_card, game.current_card._color
))
self.print_hand()
if player.can_play(current_card):
played = False
while not played:
card_index = int(input('Which card do you want to play? '))
card = player.hand[card_index]
if not game.current_card.playable(card):
print('Cannot play that card')
else:
if card.color == 'black':
new_color = input('Which color do you want? ')
else:
new_color = None
game.play(player_id, card_index, new_color)
played = True
else:
print('You cannot play. You must pick up a card.')
game.play(player_id, card=None)
self.print_hand()
elif player.can_play(game.current_card):
for i, card in enumerate(player.hand):
if game.current_card.playable(card):
if card.color == 'black':
new_color = choice(COLORS)
else:
new_color = None
print("Player {} played {}".format(player, card))
game.play(player=player_id, card=i, new_color=new_color)
break
else:
print("Player {} picked up".format(player))
game.play(player=player_id, card=None)
def print_hand(self):
print('Your hand: {}'.format(
' '.join(str(card) for card in self.player.hand)
))