-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
717 lines (584 loc) · 27.5 KB
/
tests.py
File metadata and controls
717 lines (584 loc) · 27.5 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import unittest
from dubito.hand import Hand
from dubito.handlers import GameHandler, StatsHandler, generate_player_data
from dubito.core_game import create_deck, assign_cards, initialize, dubito
from bots.manual.rule_based import AlwaysTruthful, MrDoubt, MrNoDoubt, RandomBoi
# ---------------------------------------------------------------------------
# Hand
# ---------------------------------------------------------------------------
class TestHand(unittest.TestCase):
def test_pick_exact_amount(self):
h = Hand([1, 1, 2, 3])
picked = h.pick(1, 2)
self.assertEqual(picked, [1, 1])
self.assertEqual(sorted(h.hand), [2, 3])
def test_pick_removes_from_hand(self):
h = Hand([5, 5, 5])
h.pick(5, 2)
self.assertEqual(h.hand, [5])
def test_pick_all(self):
h = Hand([3, 3, 3, 7])
picked = h.pick_all(3)
self.assertEqual(picked, [3, 3, 3])
self.assertEqual(h.hand, [7])
def test_pick_random_reduces_hand(self):
h = Hand([1, 2, 3, 4, 5])
picked = h.pick_random(3)
self.assertEqual(len(picked), 3)
self.assertEqual(len(h.hand), 2)
def test_discard_four_of_a_kind(self):
h = Hand([1, 1, 1, 1, 2, 3])
discarded = h.discard(amount=4)
self.assertIn(1, discarded)
self.assertNotIn(1, h.hand)
def test_discard_does_not_remove_joker(self):
# Jokers are value 0; at most 2 exist so discard(4) never removes them
h = Hand([0, 0, 1, 1, 1, 1])
discarded = h.discard(amount=4)
self.assertIn(1, discarded)
self.assertIn(0, h.hand)
self.assertEqual(h.hand.count(0), 2)
def test_add_sorts_hand(self):
h = Hand([3, 5])
h.add([1, 4])
self.assertEqual(h.hand, [1, 3, 4, 5])
def test_count(self):
h = Hand([2, 2, 2, 5])
self.assertEqual(h.count(2), 3)
self.assertEqual(h.count(5), 1)
self.assertEqual(h.count(9), 0)
def test_has(self):
h = Hand([7, 8])
self.assertTrue(h.has(7))
self.assertFalse(h.has(1))
def test_len(self):
h = Hand([1, 2, 3])
self.assertEqual(len(h), 3)
def test_pick_most(self):
h = Hand([1, 2, 2, 2, 3])
picked = h.pick_most()
self.assertEqual(picked, [2, 2, 2])
self.assertEqual(sorted(h.hand), [1, 3])
# ---------------------------------------------------------------------------
# Deck creation
# ---------------------------------------------------------------------------
class TestCreateDeck(unittest.TestCase):
def test_standard_deck_size(self):
deck = create_deck(deck_size=14, n_jollies=0)
self.assertEqual(len(deck), 52) # 13 numbers × 4
def test_each_card_appears_four_times(self):
deck = create_deck(deck_size=14, n_jollies=0)
for n in range(1, 14):
self.assertEqual(deck.count(n), 4)
def test_jokers_added(self):
deck = create_deck(deck_size=14, n_jollies=2)
self.assertEqual(deck.count(0), 2)
self.assertEqual(len(deck), 54)
def test_no_jokers_by_default_is_zero(self):
deck = create_deck(deck_size=14, n_jollies=0)
self.assertNotIn(0, deck)
# ---------------------------------------------------------------------------
# GameHandler — is_honest / joker mechanic
# ---------------------------------------------------------------------------
def _make_game(n_players=4):
players = [MrNoDoubt(i) for i in range(n_players)]
return GameHandler(all_players=players, deck_size=14), players
class TestIsHonest(unittest.TestCase):
def test_honest_when_cards_match(self):
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([5, 5])
self.assertTrue(gh.is_honest())
def test_dishonest_when_cards_dont_match(self):
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([5, 3])
self.assertFalse(gh.is_honest())
def test_joker_alone_is_honest(self):
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([0])
self.assertTrue(gh.is_honest())
def test_joker_with_wrong_card_is_dishonest(self):
# is_honest() checks each card individually: a joker protects its own
# slot but a non-matching non-joker card still makes the play dishonest.
# The whole-play joker protection is handled separately via jokers_in_latest().
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([0, 3]) # joker OK, but 3 ≠ 5
self.assertFalse(gh.is_honest())
def test_joker_with_matching_card_is_honest(self):
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([0, 5]) # joker + correct card
self.assertTrue(gh.is_honest())
def test_jokers_in_latest_returns_jokers(self):
gh, _ = _make_game()
gh.set_board_cards([0, 5, 0])
self.assertEqual(gh.jokers_in_latest(), [0, 0])
def test_jokers_in_latest_empty_when_none(self):
gh, _ = _make_game()
gh.set_board_cards([5, 5])
self.assertEqual(gh.jokers_in_latest(), [])
# ---------------------------------------------------------------------------
# Joker mechanic end-to-end (via dubito)
# ---------------------------------------------------------------------------
class TestJokerMechanic(unittest.TestCase):
def test_game_completes_with_jokers(self):
# Game ends when 2 players remain → exactly 2 losers
for _ in range(10):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
shuffle_players=True,
n_jollies=2,
)
self.assertEqual(len(result['losers']), 2)
def test_joker_event_appears_in_logs(self):
# MrDoubt always doubts, RandomBoi bluffs randomly — joker events are frequent
found = False
for _ in range(300):
_, infos = dubito(
all_players=[MrDoubt(1), RandomBoi(2), MrDoubt(3), RandomBoi(4)],
shuffle_players=False,
n_jollies=2,
)
if 'Joker revealed' in infos['logs']:
found = True
break
self.assertTrue(found, "Joker mechanic never triggered in 300 games")
def test_game_completes_without_jokers(self):
for _ in range(10):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
shuffle_players=True,
n_jollies=0,
)
self.assertEqual(len(result['losers']), 2)
# ---------------------------------------------------------------------------
# Full game smoke test
# ---------------------------------------------------------------------------
class TestFullGame(unittest.TestCase):
def test_exactly_two_losers(self):
# Game ends when 2 players remain — both are losers.
for _ in range(20):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
)
self.assertEqual(len(result['losers']), 2)
def test_n_minus_two_winners(self):
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
for _ in range(10):
result, _ = dubito(all_players=list(players))
self.assertEqual(len(result['winners']), len(players) - 2)
def test_winner_not_in_losers(self):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
)
winner_ids = {p.id for p in result['winners']}
loser_ids = {p.id for p in result['losers']}
self.assertTrue(winner_ids.isdisjoint(loser_ids))
def test_all_players_accounted_for(self):
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
result, _ = dubito(all_players=players)
total = len(result['winners']) + len(result['losers'])
self.assertEqual(total, len(players))
def test_winners_ordered_by_finish(self):
# winners[0] emptied their hand first; the list must be a permutation of the players
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
result, _ = dubito(all_players=list(players))
winner_ids = [p.id for p in result['winners']]
# All winner ids are distinct
self.assertEqual(len(winner_ids), len(set(winner_ids)))
def test_turn_cap_terminates_game(self):
# With max_turns=1 the game must terminate immediately and account for all players.
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
result, infos = dubito(all_players=list(players), max_turns=1)
total = len(result['winners']) + len(result['losers'])
self.assertEqual(total, len(players))
self.assertIn('Turn limit', infos['logs'])
def test_player_sizes_vary(self):
for n in range(3, 8):
ps = [RandomBoi(i) for i in range(1, n + 1)]
result, _ = dubito(all_players=ps)
self.assertEqual(len(result['winners']) + len(result['losers']), n)
self.assertEqual(len(result['losers']), 2)
self.assertEqual(len(result['winners']), n - 2)
def test_no_crash_on_correct_doubt_after_win(self):
# Regression: correct_doubt=True reuses prev_player across loop iterations.
# If prev_player won in the previous won-phase, set_winners must not fire again.
for _ in range(200):
result, _ = dubito(
all_players=[MrDoubt(1), RandomBoi(2), MrDoubt(3), RandomBoi(4)],
)
total = len(result['winners']) + len(result['losers'])
self.assertEqual(total, 4)
# ---------------------------------------------------------------------------
# Board state
# ---------------------------------------------------------------------------
class TestBoardState(unittest.TestCase):
def test_is_first_hand_true_when_empty(self):
gh, _ = _make_game()
self.assertTrue(gh.is_first_hand())
def test_is_first_hand_false_after_play(self):
gh, _ = _make_game()
gh.set_board_cards([5])
self.assertFalse(gh.is_first_hand())
def test_board_accumulates_across_plays(self):
gh, _ = _make_game()
gh.set_board_cards([1, 2])
gh.set_board_cards([3])
self.assertEqual(sorted(gh.get_board()), [1, 2, 3])
self.assertEqual(gh.n_cards_board(), 3)
def test_latests_updated_to_most_recent_play(self):
gh, _ = _make_game()
gh.set_board_cards([1, 2])
gh.set_board_cards([7, 8])
self.assertEqual(gh.get_latest_played_cards(), [7, 8])
def test_reset_board_clears_everything(self):
gh, _ = _make_game()
gh.set_current_number(5)
gh.set_board_cards([5, 5])
gh.turn.streak = 3
gh.reset_board()
self.assertEqual(gh.get_board(), [])
self.assertEqual(gh.get_current_number(), 0)
self.assertEqual(gh.get_latest_played_cards(), [])
self.assertEqual(gh.turn.streak, 0)
def test_set_discarded_cards_removes_from_availables(self):
gh, _ = _make_game()
self.assertIn(5, gh.board.availables)
gh.set_discarded_cards([5])
self.assertNotIn(5, gh.board.availables)
def test_set_discarded_cards_leaves_others_intact(self):
gh, _ = _make_game()
gh.set_discarded_cards([5])
for n in range(1, 14):
if n != 5:
self.assertIn(n, gh.board.availables)
# ---------------------------------------------------------------------------
# Turn cycling
# ---------------------------------------------------------------------------
class TestTurnCycling(unittest.TestCase):
def test_next_turn_returns_correct_players(self):
gh, players = _make_game(n_players=4)
prev, this = gh.next_turn()
# Initial position is len-1, so first next_turn wraps to position 0 (prev=last, this=first)
self.assertIn(prev, players)
self.assertIn(this, players)
self.assertNotEqual(prev.id, this.id)
def test_turn_cycles_through_all_players(self):
gh, players = _make_game(n_players=3)
seen = set()
for _ in range(3):
_, this = gh.next_turn()
seen.add(this.id)
self.assertEqual(len(seen), 3)
def test_streak_increments_each_turn(self):
gh, _ = _make_game()
gh.next_turn()
gh.next_turn()
self.assertEqual(gh.turn.streak, 2)
def test_streak_resets_on_board_reset(self):
gh, _ = _make_game()
gh.next_turn()
gh.next_turn()
gh.reset_board()
self.assertEqual(gh.turn.streak, 0)
# ---------------------------------------------------------------------------
# Doubt card distribution
# ---------------------------------------------------------------------------
class TestDoubtCardDistribution(unittest.TestCase):
def _setup_doubt(self):
"""Returns (game_handler, doubter_player, bluffer_player) with 3 cards on the board."""
gh, players = _make_game(n_players=3)
doubter, bluffer = players[0], players[1]
gh.set_current_number(5)
gh.set_board_cards([5, 3, 3]) # bluff: 3s declared as 5s
return gh, doubter, bluffer
def test_wrong_doubt_gives_board_cards_to_doubter(self):
gh, doubter, _ = _make_game(n_players=3)[0], _make_game(n_players=3)[1][0], None
gh, players = _make_game(n_players=3)
doubter = players[0]
gh.set_current_number(7)
gh.set_board_cards([7, 7]) # honest play
cards_before = len(doubter.cards)
doubter.add_cards(gh.get_board())
gh.reset_board()
self.assertEqual(len(doubter.cards), cards_before + 2)
self.assertEqual(gh.get_board(), [])
def test_correct_doubt_gives_board_cards_to_bluffer(self):
gh, players = _make_game(n_players=3)
bluffer = players[1]
gh.set_current_number(7)
gh.set_board_cards([7, 3]) # bluff
cards_before = len(bluffer.cards)
bluffer.add_cards(gh.get_board())
gh.reset_board()
self.assertEqual(len(bluffer.cards), cards_before + 2)
self.assertEqual(gh.get_board(), [])
def test_joker_doubt_discards_joker_doubter_gets_rest(self):
gh, players = _make_game(n_players=3)
doubter = players[0]
# Accumulate 2 regular cards from a previous play, then joker play
gh.set_board_cards([5, 5]) # prior round cards
gh.set_board_cards([0, 3]) # joker + bluff card (latest)
jokers = gh.jokers_in_latest() # [0]
board = list(gh.get_board()) # [5, 5, 0, 3]
for j in jokers:
board.remove(j) # discard joker → [5, 5, 3]
cards_before = len(doubter.cards)
doubter.add_cards(board)
self.assertEqual(len(doubter.cards), cards_before + 3) # gets 3, not 4
# ---------------------------------------------------------------------------
# Card distribution (assign_cards / initialize)
# ---------------------------------------------------------------------------
class TestCardDistribution(unittest.TestCase):
def test_assign_cards_distributes_all(self):
players = [MrNoDoubt(i) for i in range(4)]
deck = list(range(52)) # arbitrary 52 cards
assign_cards(deck, players)
total = sum(len(p.cards) for p in players)
self.assertEqual(total, 52)
def test_assign_cards_round_robin(self):
players = [MrNoDoubt(i) for i in range(4)]
assign_cards([1, 2, 3, 4, 5, 6, 7, 8], players)
# Each player should have exactly 2 cards
for p in players:
self.assertEqual(len(p.cards), 2)
def test_initialize_no_player_starts_with_four_equal(self):
for _ in range(10):
players = [MrNoDoubt(i) for i in range(4)]
initialize(players, deck_size=14)
for p in players:
counts = p.cards.count_all()
self.assertFalse(any(v >= 4 for v in counts.values()))
def test_initialize_distributes_full_deck(self):
players = [MrNoDoubt(i) for i in range(4)]
initialize(players, deck_size=14, n_jollies=2)
total = sum(len(p.cards) for p in players)
self.assertEqual(total, 54) # 52 regular + 2 jokers
# ---------------------------------------------------------------------------
# Winners / playing players
# ---------------------------------------------------------------------------
class TestWinners(unittest.TestCase):
def test_set_winners_removes_from_playing(self):
gh, players = _make_game(n_players=4)
gh.next_turn() # set gh.players.this
winner = players[0]
gh.players.this = players[1] # ensure this != winner so position update works
gh.set_winners(winner)
self.assertNotIn(winner, gh.playing_players())
self.assertIn(winner, gh.get_winners())
def test_n_playing_players_decreases_after_win(self):
gh, players = _make_game(n_players=4)
gh.next_turn()
gh.players.this = players[1]
gh.set_winners(players[0])
self.assertEqual(gh.n_playing_players(), 3)
def test_winners_list_is_ordered(self):
# Each successive set_winners call appends to the end of the winners list.
gh, players = _make_game(n_players=4)
gh.next_turn()
gh.players.this = players[1]
gh.set_winners(players[0]) # 1st place
gh.players.this = players[2]
gh.set_winners(players[1]) # 2nd place
self.assertEqual(gh.get_winners(), [players[0], players[1]])
# ---------------------------------------------------------------------------
# Stats tracking
# ---------------------------------------------------------------------------
class TestStatsTracking(unittest.TestCase):
def test_play_turns_and_cards_counted(self):
# AlwaysTruthful never doubts, so play_turns must be positive after a game.
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
_, infos = dubito(all_players=list(players))
stats = infos['stats'].data
for p in players:
s = stats[p.id]
self.assertGreaterEqual(s['play_turns'], 0)
self.assertGreaterEqual(s['total_cards_played'], 0)
# play_turns ≤ turns (doubts also count as turns)
self.assertLessEqual(s['play_turns'], s['turns'])
def test_doubts_counted_for_mr_doubt(self):
players = [MrDoubt(1), MrNoDoubt(2), MrNoDoubt(3)]
_, infos = dubito(all_players=list(players), shuffle_players=False)
stats = infos['stats'].data
# MrDoubt always doubts when not first hand; must have at least one doubt
self.assertGreater(stats[1]['doubts'], 0)
def test_bluffs_tracked(self):
# RandomBoi bluffs randomly; across enough games at least one bluff occurs.
found = False
for _ in range(50):
players = [RandomBoi(1), RandomBoi(2), RandomBoi(3)]
_, infos = dubito(all_players=list(players))
stats = infos['stats'].data
if any(stats[p.id]['bluffs'] > 0 for p in players):
found = True
break
self.assertTrue(found, "No bluffs recorded across 50 games with RandomBoi")
def test_successful_doubts_lte_doubts(self):
for _ in range(10):
players = [MrDoubt(1), RandomBoi(2), MrDoubt(3), RandomBoi(4)]
_, infos = dubito(all_players=list(players))
stats = infos['stats'].data
for p in players:
s = stats[p.id]
self.assertLessEqual(s['successful_doubts'], s['doubts'])
def test_not_first_turns_lte_turns(self):
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
_, infos = dubito(all_players=list(players))
stats = infos['stats'].data
for p in players:
s = stats[p.id]
self.assertLessEqual(s['not_first_turns'], s['turns'])
def test_total_cards_played_ge_play_turns(self):
# Each play turn places at least 1 card, so total_cards_played >= play_turns.
players = [AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)]
_, infos = dubito(all_players=list(players))
stats = infos['stats'].data
for p in players:
s = stats[p.id]
self.assertGreaterEqual(s['total_cards_played'], s['play_turns'])
# ---------------------------------------------------------------------------
# Turn order after a player wins (soft win)
# ---------------------------------------------------------------------------
class TestTurnOrderAfterWin(unittest.TestCase):
"""
Verify that when a player is eliminated mid-game the remaining players
cycle correctly and the winner never appears as prev or this again.
Initial turn.position = len(players)-1 = 3, so next_turn() calls produce:
call #1 → prev=p3, this=p0
call #2 → prev=p0, this=p1
call #3 → prev=p1, this=p2 ← p2 is "this" (bot3 in user scenario)
call #4 → prev=p2, this=p3 ← p2 is "prev"
"""
def _advance(self, gh, n):
for _ in range(n):
gh.next_turn()
# ------------------------------------------------------------------
# Core scenario: [bot1 bot2 bot3 bot4] → bot3 wins → bot1 bot2 bot4
# ------------------------------------------------------------------
def test_bot3_wins_as_this_player_cycle_is_bot1_bot2_bot4(self):
"""bot3 wins during their own turn (this_player); remaining cycle: bot1, bot2, bot4."""
gh, players = _make_game(n_players=4)
# After 3 next_turn() calls: this=players[2] (bot3), prev=players[1]
self._advance(gh, 3)
self.assertEqual(gh.players.this.id, players[2].id)
gh.set_winners(players[2])
self.assertNotIn(players[2], gh.playing_players())
seen = set()
for _ in range(9): # 3 remaining players × 3 full cycles
_, this = gh.next_turn()
seen.add(this.id)
self.assertNotIn(players[2].id, seen)
self.assertEqual(seen, {players[0].id, players[1].id, players[3].id})
def test_bot3_wins_as_prev_player_cycle_is_bot1_bot2_bot4(self):
"""bot3 wins at end of their turn (prev_player); remaining cycle: bot1, bot2, bot4."""
gh, players = _make_game(n_players=4)
# After 4 next_turn() calls: prev=players[2] (bot3), this=players[3]
self._advance(gh, 4)
self.assertEqual(gh.players.prev.id, players[2].id)
gh.set_winners(players[2])
self.assertNotIn(players[2], gh.playing_players())
seen = set()
for _ in range(9):
_, this = gh.next_turn()
seen.add(this.id)
self.assertNotIn(players[2].id, seen)
self.assertEqual(seen, {players[0].id, players[1].id, players[3].id})
# ------------------------------------------------------------------
# Winner never appears as prev either
# ------------------------------------------------------------------
def test_winner_never_appears_as_prev_or_this(self):
gh, players = _make_game(n_players=4)
self._advance(gh, 3)
winner = gh.players.this
gh.set_winners(winner)
for _ in range(9):
prev, this = gh.next_turn()
self.assertNotEqual(prev.id, winner.id)
self.assertNotEqual(this.id, winner.id)
# ------------------------------------------------------------------
# set_winners() on this_player must not crash
# ------------------------------------------------------------------
def test_set_winners_this_player_does_not_crash(self):
gh, players = _make_game(n_players=4)
self._advance(gh, 3)
this = gh.players.this
gh.set_winners(this) # previously raised ValueError
self.assertNotIn(this, gh.playing_players())
self.assertIn(this, gh.get_winners())
# ------------------------------------------------------------------
# turn.position stays in-bounds across multiple eliminations
# ------------------------------------------------------------------
def test_position_valid_after_successive_eliminations(self):
gh, players = _make_game(n_players=4)
self._advance(gh, 2)
gh.set_winners(players[1]) # remove p1 while p1 is this
self._advance(gh, 1)
gh.set_winners(players[3]) # remove p3
# 2 players left; all next_turn() calls must succeed and stay in-bounds
for _ in range(4):
prev, this = gh.next_turn()
self.assertIn(prev, gh.playing_players() + gh.get_winners())
self.assertIn(this, gh.playing_players())
# ------------------------------------------------------------------
# Win detection fires for this_player (discard-phase win)
# ------------------------------------------------------------------
def test_win_detection_catches_this_player(self):
"""set_winners(this_player) reduces playing count — confirms engine can handle it."""
gh, players = _make_game(n_players=4)
self._advance(gh, 3)
this = gh.players.this
n_before = gh.n_playing_players()
gh.set_winners(this)
self.assertEqual(gh.n_playing_players(), n_before - 1)
# ------------------------------------------------------------------
# Regression: correct_doubt renamed to replay_turn
# ------------------------------------------------------------------
def test_no_crash_on_replay_turn_after_soft_win(self):
for _ in range(200):
result, _ = dubito(
all_players=[MrDoubt(1), RandomBoi(2), MrDoubt(3), RandomBoi(4)],
)
total = len(result['winners']) + len(result['losers'])
self.assertEqual(total, 4)
# ---------------------------------------------------------------------------
# Two-loser rule: game always ends with exactly 2 losers
# ---------------------------------------------------------------------------
class TestTwoLoserRule(unittest.TestCase):
def test_three_players_one_winner_two_losers_no_soft_wins(self):
# With 3 players: first to empty hand wins, the remaining 2 both lose.
# Soft wins are impossible — there is no middle position.
for _ in range(30):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), RandomBoi(3)],
)
self.assertEqual(len(result['winners']), 1)
self.assertEqual(len(result['losers']), 2)
def test_four_players_two_winners_two_losers(self):
# 1 hard win + 1 soft win + 2 losses
for _ in range(20):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
)
self.assertEqual(len(result['winners']), 2)
self.assertEqual(len(result['losers']), 2)
def test_five_players_three_winners_two_losers(self):
# 1 hard win + 2 soft wins + 2 losses
for _ in range(10):
result, _ = dubito(
all_players=[RandomBoi(i) for i in range(1, 6)],
)
self.assertEqual(len(result['winners']), 3)
self.assertEqual(len(result['losers']), 2)
def test_losers_are_disjoint_from_winners(self):
for _ in range(20):
result, _ = dubito(
all_players=[AlwaysTruthful(1), MrNoDoubt(2), MrDoubt(3), RandomBoi(4)],
)
winner_ids = {p.id for p in result['winners']}
loser_ids = {p.id for p in result['losers']}
self.assertTrue(winner_ids.isdisjoint(loser_ids))
if __name__ == '__main__':
unittest.main()