forked from bennuttall/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uno_pgz.py
503 lines (423 loc) · 15.3 KB
/
uno_pgz.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from random import shuffle, choice, randint
from itertools import product, repeat, chain
from threading import Thread
from time import sleep
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
self.sprite = Actor('{}_{}'.format(color, card_type))
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 __format__(self, f):
if f == 'full':
return '{} {}'.format(self.color, self.card_type)
else:
return str(self)
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=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
self._check_first_card()
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)
def _check_first_card(self):
if self.current_card.color == 'black':
color = choice(COLORS)
self.current_card.temp_color = color
print("Selected random color for black card: {}".format(color))
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 GameData:
def __init__(self):
self.selected_card = None
self.selected_color = None
self.color_selection_required = False
self.log = ''
@property
def selected_card(self):
selected_card = self._selected_card
self.selected_card = None
return selected_card
@selected_card.setter
def selected_card(self, value):
self._selected_card = value
@property
def selected_color(self):
selected_color = self._selected_color
self.selected_color = None
return selected_color
@selected_color.setter
def selected_color(self, value):
self._selected_color = value
game_data = GameData()
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))
def __next__(self):
game = self.game
player = game.current_player
player_id = player.player_id
current_card = game.current_card
if player == self.player:
played = False
while not played:
card_index = None
while card_index is None:
card_index = game_data.selected_card
new_color = None
if card_index is not False:
card = player.hand[card_index]
if not game.current_card.playable(card):
game_data.log = 'You cannot play that card'
continue
else:
game_data.log = 'You played card {:full}'.format(card)
if card.color == 'black' and len(player.hand) > 1:
game_data.color_selection_required = True
while new_color is None:
new_color = game_data.selected_color
game_data.log = 'You selected {}'.format(new_color)
else:
card_index = None
game_data.log = 'You picked up'
game.play(player_id, card_index, new_color)
played = True
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
game_data.log = "Player {} played {:full}".format(player, card)
game.play(player=player_id, card=i, new_color=new_color)
break
else:
game_data.log = "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)
))
num_players = 3
game = AIUnoGame(num_players)
WIDTH = 1200
HEIGHT = 800
deck_img = Actor('back')
color_imgs = {color: Actor(color) for color in COLORS}
def game_loop():
while game.game.is_active:
sleep(1)
next(game)
game_loop_thread = Thread(target=game_loop)
game_loop_thread.start()
def draw_deck():
deck_img.pos = (130, 70)
deck_img.draw()
current_card = game.game.current_card
current_card.sprite.pos = (210, 70)
current_card.sprite.draw()
if game_data.color_selection_required:
for i, card in enumerate(color_imgs.values()):
card.pos = (290+i*80, 70)
card.draw()
elif current_card.color == 'black' and current_card.temp_color is not None:
color_img = color_imgs[current_card.temp_color]
color_img.pos = (290, 70)
color_img.draw()
def draw_players_hands():
for p, player in enumerate(game.game.players):
color = 'red' if player == game.game.current_player else 'black'
text = 'P{} {}'.format(p, 'wins' if game.game.winner == player else '')
screen.draw.text(text, (0, 300+p*130), fontsize=100, color=color)
for c, card in enumerate(player.hand):
if player == game.player:
sprite = card.sprite
else:
sprite = Actor('back')
sprite.pos = (130+c*80, 330+p*130)
sprite.draw()
def show_log():
screen.draw.text(game_data.log, midbottom=(WIDTH/2, HEIGHT-50), color='black')
def update():
screen.clear()
screen.fill((255, 255, 255))
draw_deck()
draw_players_hands()
show_log()
def on_mouse_down(pos):
if game.player == game.game.current_player:
for card in game.player.hand:
if card.sprite.collidepoint(pos):
game_data.selected_card = game.player.hand.index(card)
print('Selected card {} index {}'.format(card, game.player.hand.index(card)))
if deck_img.collidepoint(pos):
game_data.selected_card = False
print('Selected pick up')
for color, card in color_imgs.items():
if card.collidepoint(pos):
game_data.selected_color = color
game_data.color_selection_required = False