-
Notifications
You must be signed in to change notification settings - Fork 33
/
uno_tests.py
338 lines (272 loc) · 8.08 KB
/
uno_tests.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
import pytest
from uno import *
# Test creating invalid cards
with pytest.raises(TypeError):
card = UnoCard()
with pytest.raises(TypeError):
card = UnoCard('black')
with pytest.raises(ValueError):
card = UnoCard('purple', 'ace')
with pytest.raises(ValueError):
card = UnoCard('red', 'ace')
with pytest.raises(ValueError):
card = UnoCard('purple', 1)
with pytest.raises(ValueError):
card = UnoCard('red', 'wildcard')
with pytest.raises(ValueError):
card = UnoCard('black', 1)
# Test creating valid cards
card1 = UnoCard('red', 0)
assert repr(card1) == '<UnoCard object: red 0>'
card2 = UnoCard('red', 1)
assert repr(card2) == '<UnoCard object: red 1>'
assert card1 != card2
assert card1.color == card2.color
assert card1.card_type != card2.card_type
card3 = UnoCard('red', 0)
assert card1 == card3
# Test placing valid cards on other cards
card1 = UnoCard('red', 1)
card2 = UnoCard('red', 1)
assert card1.playable(card2)
card2 = UnoCard('red', 2)
assert card1.playable(card2)
card2 = UnoCard('red', 'skip')
assert card1.playable(card2)
card2 = UnoCard('green', 1)
assert card1.playable(card2)
card2 = UnoCard('green', 1)
assert card1.playable(card2)
card2 = UnoCard('red', 1)
assert card1.playable(card2)
card2 = UnoCard('black', 'wildcard')
assert card1.playable(card2)
card2 = UnoCard('black', '+4')
assert card1.playable(card2)
# Test placing invalid cards on other cards
card1 = UnoCard('red', 1)
card2 = UnoCard('green', 2)
assert not card1.playable(card2)
card2 = UnoCard('blue', 9)
assert not card1.playable(card2)
card2 = UnoCard('yellow', 'skip')
assert not card1.playable(card2)
# Test placing valid cards on black cards
card1 = UnoCard('black', 'wildcard')
card1.temp_color = 'red'
card2 = UnoCard('red', 1)
assert card1.playable(card2)
card2 = UnoCard('red', 'skip')
assert card1.playable(card2)
card2 = UnoCard('black', 'wildcard')
assert card1.playable(card2)
# Test placing invalid cards on black cards
card1 = UnoCard('black', 'wildcard')
card1.temp_color = 'red'
card2 = UnoCard('green', 1)
assert not card1.playable(card2)
card2 = UnoCard('green', 'skip')
assert not card1.playable(card2)
# Test creating invalid Uno Game
with pytest.raises(TypeError):
game = UnoGame()
with pytest.raises(ValueError):
game = UnoGame(1)
with pytest.raises(ValueError):
game = UnoGame('foo')
card = UnoCard('red', 1)
with pytest.raises(ValueError):
game = UnoGame(card)
with pytest.raises(ValueError):
game = UnoGame(16)
# Test creating invalid Uno Player
cards = []
with pytest.raises(ValueError):
player = UnoPlayer(cards)
cards = range(7)
with pytest.raises(ValueError):
player = UnoPlayer(cards)
cards = [UnoCard('red', 0)]
with pytest.raises(ValueError):
player = UnoPlayer(cards)
cards = [
('red', 0),
('red', 1),
('yellow', 0),
('yellow', 2),
('blue', 0),
('blue', 1),
]
uno_cards = [UnoCard(color, card_type) for color, card_type in cards]
uno_cards.append(1)
with pytest.raises(ValueError):
player = UnoPlayer(cards)
cards = [
('red', 0),
('red', 1),
('yellow', 0),
('yellow', 2),
('blue', 0),
('blue', 1),
('black', 'wildcard'),
('black', '+4'),
]
uno_cards = [UnoCard(color, card_type) for color, card_type in cards]
with pytest.raises(ValueError):
player = UnoPlayer(cards)
# Test creating valid Uno Player
cards = [
('red', 0),
('red', 1),
('yellow', 0),
('yellow', 2),
('blue', 0),
('blue', 1),
('black', 'wildcard'),
]
uno_cards = [UnoCard(color, card_type) for color, card_type in cards]
player = UnoPlayer(uno_cards)
# Test ReversibleCycle
rc = ReversibleCycle(range(3))
a = next(rc)
assert a == 0
a = next(rc)
assert a == 1
a = next(rc)
assert a == 2
a = next(rc)
assert a == 0
a = next(rc)
assert a == 1
a = next(rc)
assert a == 2
rc.reverse()
a = next(rc)
assert a == 1
a = next(rc)
assert a == 0
rc.reverse()
a = next(rc)
assert a == 1
rc = ReversibleCycle(range(3))
rc.reverse()
a = next(rc)
assert a == 2
a = next(rc)
assert a == 1
rc = ReversibleCycle(range(3))
rc.reverse()
rc.reverse()
a = next(rc)
assert a == 0
a = next(rc)
assert a == 1
# Test creating valid Uno Game
for n in range(2, 16):
game = UnoGame(n)
assert len(game.players) == n
for player in game.players:
assert isinstance(player, UnoPlayer)
assert len(player.hand) == 7
assert len(game.deck) == 108 - 7*n
assert len(game.deck) > 1
# Test start of gameplay
game = UnoGame(2)
assert isinstance(game.current_card, UnoCard)
assert game.is_active
assert game.current_player == game.players[0]
assert game.winner is None
# Test gameplay with un-shuffled deck
game = UnoGame(5, random=False)
player_0 = game.players[0]
player_1 = game.players[1]
player_2 = game.players[2]
player_3 = game.players[3]
player_4 = game.players[4]
assert game.current_player == player_0
assert game.current_card == UnoCard('yellow', 1)
assert game.winner is None
assert player_0.can_play(game.current_card)
with pytest.raises(ValueError):
game.play(player="bob", card=0)
with pytest.raises(ValueError):
# not player 1's go
game.play(player=1, card=0)
with pytest.raises(ValueError):
# cannot play red 0
game.play(player=0, card=0)
assert player_0.hand[1] == UnoCard('red', 1)
# can play red 1
game.play(player=0, card=1)
assert len(player_0.hand) == 6
assert game.current_card == UnoCard('red', 1)
assert game.is_active
game.play(player=1, card=0) # red 7
game.play(player=2, card=0) # red 5
game.play(player=3, card=0) # red +2
# player 4 must pick up 2 cards and skip a go
assert len(player_4.hand) == 9
assert game.current_player == player_0
with pytest.raises(ValueError):
game.play(player=4, card=1)
game.play(player=0, card=0) # red 0
game.play(player=1, card=0) # red 8
game.play(player=2, card=0) # red 6
game.play(player=3, card=0) # red skip
assert game.current_player == player_0
game.play(player=0, card=0) # red 2
game.play(player=1, card=0) # red 9
game.play(player=2, card=0) # red 7
game.play(player=3, card=0) # red reverse
assert game.current_player == player_2
game.play(player=2, card=0) # red 8
game.play(player=1, card=0) # red 1
game.play(player=0, card=0) # red 3
game.play(player=4, card=0) # yellow 3
game.play(player=3, card=1) # yellow 1
assert not game.current_player.can_play(game.current_card)
assert game.current_player == player_2
player_2_hand_size_before = len(player_2.hand)
game.play(player=2, card=None) # can't go, pick up
player_2_hand_size_after = len(player_2.hand)
assert player_2_hand_size_after == player_2_hand_size_before + 1
game.play(player=1, card=None) # can't go, pick up
game.play(player=0, card=None) # can't go, pick up
player_3_hand_size_before = len(player_3.hand)
game.play(player=4, card=7, new_color='yellow') # black wildcard
player_3_hand_size_after = len(player_3.hand)
assert game.current_player == player_3
assert game.current_card._color == 'yellow'
assert player_3_hand_size_after == player_3_hand_size_before
game.play(player=3, card=1) # yellow 1
with pytest.raises(ValueError):
game.play(player=2, card=3) # no new_color set
player_1_hand_size_before = len(player_1.hand)
game.play(player=2, card=3, new_color='red') # black +4
player_1_hand_size_after = len(player_1.hand)
assert game.current_player == player_0
assert game.current_card._color == 'red'
assert player_1_hand_size_after == player_1_hand_size_before + 4
game.play(player=0, card=0) # red 4
game.play(player=4, card=0) # yellow 4
game.play(player=3, card=1) # yellow 2
game.play(player=2, card=None) # can't go, pick up
game.play(player=1, card=0) # red 2
game.play(player=0, card=0) # red 5
game.play(player=4, card=None) # doesn't go, picks up
assert game.winner is None
game.play(player=3, card=0) # red +2, final card
assert len(player_3.hand) == 0
assert not game.is_active
assert game.winner == player_3
with pytest.raises(ValueError):
game.play(player=2, card=0)
with pytest.raises(ValueError):
game.play(player=1, card=0)
"""
print("current player:", game.current_player)
print("current card:", game.current_card, "color:", game.current_card._color)
print()
for i, player in enumerate(game.players):
print("player", i, player.hand, end="\n\n")
"""