-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgame_manager.py
More file actions
1080 lines (923 loc) · 39.5 KB
/
game_manager.py
File metadata and controls
1080 lines (923 loc) · 39.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import importlib.util
from typing import List, Dict, Any, Optional, Tuple
from collections import Counter
import random
from enum import Enum
from poker_agents.agent_base import PokerAgentBase
class Suit(Enum):
HEARTS = "♥"
DIAMONDS = "♦"
CLUBS = "♣"
SPADES = "♠"
VALUE_NAMES = {
14: "Ace",
13: "King",
12: "Queen",
11: "Jack",
10: "Ten",
9: "Nine",
8: "Eight",
7: "Seven",
6: "Six",
5: "Five",
4: "Four",
3: "Three",
2: "Two",
}
PLURAL_VALUE_NAMES = {
14: "Aces",
13: "Kings",
12: "Queens",
11: "Jacks",
10: "Tens",
9: "Nines",
8: "Eights",
7: "Sevens",
6: "Sixes",
5: "Fives",
4: "Fours",
3: "Threes",
2: "Twos",
}
HAND_RANK_LABELS = {
10: "Royal Flush",
9: "Straight Flush",
8: "Four of a Kind",
7: "Full House",
6: "Flush",
5: "Straight",
4: "Three of a Kind",
3: "Two Pair",
2: "One Pair",
1: "High Card",
0: "No Hand",
}
class Card:
def __init__(self, rank: str, suit: Suit):
self.rank = rank
self.suit = suit
def __str__(self):
return f"{self.rank} of {self.suit.value}"
def __repr__(self):
return f"Card({self.rank}, {self.suit.value})"
def get_value(self):
"""Get numeric value for comparison"""
if self.rank == 'A':
return 14
elif self.rank == 'K':
return 13
elif self.rank == 'Q':
return 12
elif self.rank == 'J':
return 11
else:
return int(self.rank)
class Player:
def __init__(self, name: str, chips: int = 100, agent=None):
self.name = name
self.chips = chips
self.hole_cards = []
self.current_bet = 0
self.total_bet = 0
self.is_folded = False
self.is_all_in = False
self.agent = agent
self.position = 0
self.last_action = None
self.last_action_display = None
self.best_hand_rank = 0
self.best_hand_name = None
self.pending_invalid_reason = None
self.is_eliminated = False
def add_card(self, card: Card):
self.hole_cards.append(card)
def clear_cards(self):
self.hole_cards = []
def bet(self, amount: int) -> bool:
if amount > self.chips:
return False
self.chips -= amount
self.current_bet += amount
self.total_bet += amount
if self.chips == 0:
self.is_all_in = True
return True
def fold(self):
self.is_folded = True
self.hole_cards = []
def reset_for_new_hand(self):
self.current_bet = 0
self.total_bet = 0
self.is_folded = False
self.is_all_in = False
self.clear_cards()
self.last_action = None
self.last_action_display = None
self.best_hand_rank = 0
self.best_hand_name = None
self.pending_invalid_reason = None
# do not reset is_eliminated here; elimination persists across hands
class GameState:
def __init__(self):
self.players = []
self.community_cards = []
self.pot = 0
self.current_bet = 0
self.dealer_position = -1
self.current_player = 0
self.game_phase = "preflop" # preflop, flop, turn, river, showdown
self.deck = []
self.winner = None
self.pending_players = set()
self.hand_count = 0
def reset_deck(self):
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = [Suit.HEARTS, Suit.DIAMONDS, Suit.CLUBS, Suit.SPADES]
self.deck = [Card(rank, suit) for rank in ranks for suit in suits]
random.shuffle(self.deck)
def deal_card(self) -> Card:
if self.deck:
return self.deck.pop()
return None
class GameManager:
def __init__(
self,
move_interval: float = 1.0,
starting_chips: int = PokerAgentBase.STARTING_CHIPS,
max_hand_limit: Optional[int] = None,
):
"""Initialize the game engine and eagerly load any available agents."""
PokerAgentBase.STARTING_CHIPS = starting_chips
self.game_state = GameState()
self.gui = None
self.move_interval = move_interval
self.last_action_note = None
self.pending_new_hand = False
self.game_over = False
self.starting_chips = starting_chips
self.max_hand_limit = max_hand_limit
self.load_agents()
def load_agents(self):
"""Load poker agents from the poker_agents folder"""
agents_folder = "poker_agents"
if not os.path.exists(agents_folder):
print(f"Warning: {agents_folder} folder not found")
return
agent_files = [
f for f in os.listdir(agents_folder)
if f.endswith('.py') and f not in ('agent_base.py', '__init__.py', 'agent_template.py')
]
agent_files.sort()
for i, agent_file in enumerate(agent_files):
default_name = f"Agent {i+1}"
try:
spec = importlib.util.spec_from_file_location(
f"agent_{i}",
os.path.join(agents_folder, agent_file)
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Try to instantiate the agent
player = None
if hasattr(module, 'PokerAgent'):
agent_cls = module.PokerAgent
agent = self._instantiate_agent(agent_cls, default_name)
if isinstance(agent, PokerAgentBase) and callable(getattr(agent, 'make_decision', None)):
agent.name = agent.name or default_name
agent.chips = self.starting_chips
player = Player(agent.name, self.starting_chips, agent)
if player is None:
raise ValueError("Invalid PokerAgent implementation")
player.position = i
self.game_state.players.append(player)
except Exception as e:
print(f"Error loading {agent_file}: {e}")
# Create a default player on error
player = Player(default_name, self.starting_chips)
player.position = i
self.game_state.players.append(player)
def _instantiate_agent(self, agent_cls, fallback_name: str):
"""Instantiate an agent, allowing optional name injection."""
try:
return agent_cls()
except TypeError:
# Try again allowing name to be passed explicitly
return agent_cls(name=fallback_name)
def start_new_hand(self):
"""Start a new poker hand"""
if self.game_over:
return
if (
self.max_hand_limit is not None
and self.game_state.hand_count >= self.max_hand_limit
):
self._end_game_due_to_limit()
return
self.game_state.hand_count += 1
self.game_state.reset_deck()
self.game_state.community_cards = []
self.game_state.pot = 0
self.game_state.current_bet = 0
self.game_state.winner = None
self.pending_new_hand = False
# Reset only active players (those with chips)
for player in self.game_state.players:
if player.is_eliminated:
player.is_folded = True
player.hole_cards = []
player.last_action = "eliminated"
player.last_action_display = "Eliminated"
player.best_hand_rank = 0
player.best_hand_name = None
continue
if player.chips > 0: # Only reset players who are still in the game
player.reset_for_new_hand()
else:
player.is_folded = True
player.hole_cards = []
player.best_hand_rank = 0
player.best_hand_name = None
player.pending_invalid_reason = None
# Deal hole cards only to active players
for _ in range(2):
for player in self.game_state.players:
if player.chips > 0 and not player.is_folded:
card = self.game_state.deal_card()
if card:
player.add_card(card)
self.game_state.game_phase = "preflop"
# Find first active player for current player
first_player = self._pick_first_player_for_hand()
self.game_state.current_player = first_player
self._reset_pending_players(self.game_state.current_player)
hand_msg = f"=== NEW HAND #{self.game_state.hand_count} DEALT ==="
if self.gui:
self.gui.log_message(hand_msg, color="info")
else:
self.last_action_note = hand_msg
if self.gui:
self.gui.update_display()
def deal_community_cards(self, count: int):
"""Deal community cards"""
for _ in range(count):
card = self.game_state.deal_card()
if card:
self.game_state.community_cards.append(card)
if self.gui:
self.gui.update_display()
def next_phase(self):
"""Move to the next game phase"""
if self.game_state.game_phase == "preflop":
self.deal_community_cards(3)
self.game_state.game_phase = "flop"
elif self.game_state.game_phase == "flop":
self.deal_community_cards(1)
self.game_state.game_phase = "turn"
elif self.game_state.game_phase == "turn":
self.deal_community_cards(1)
self.game_state.game_phase = "river"
elif self.game_state.game_phase == "river":
self.game_state.game_phase = "showdown"
self.determine_winner()
# Reset betting for new phase (except showdown)
if self.game_state.game_phase != "showdown":
self._reset_betting_round()
if self.gui:
self.gui.update_display()
def _reset_betting_round(self):
"""Reset betting round for new phase"""
# Reset current bet and player bets for new betting round
self.game_state.current_bet = 0
for player in self.game_state.players:
if not player.is_folded:
player.current_bet = 0
# Keep total_bet for pot calculation
self._reset_pending_players()
def player_action(self, player_index: int, action: str, amount: int = 0):
"""Process a player's action; invalid attempts trigger an automatic fold."""
if player_index >= len(self.game_state.players):
return False
player = self.game_state.players[player_index]
if player.is_folded or player.is_all_in:
self.game_state.pending_players.discard(player_index)
return False
success = False
previous_bet = self.game_state.current_bet
note_from_agent = player.pending_invalid_reason
player.pending_invalid_reason = None
self.last_action_note = None
action_display = None
if action == "fold":
player.fold()
success = True
if note_from_agent:
self.last_action_note = f"{note_from_agent} Automatic fold applied."
action_display = "Fold"
elif action == "call":
call_amount = max(0, self.game_state.current_bet - player.current_bet)
if call_amount == 0:
success = True
action_display = "Check"
else:
if call_amount > player.chips:
return self._auto_fold(
player_index,
f"attempted to call ${call_amount} with only ${player.chips}",
)
if call_amount <= 0:
return self._auto_fold(player_index, "cannot call without chips")
if player.bet(call_amount):
self.game_state.pot += call_amount
success = True
action_display = f"Call ${call_amount}"
elif action == "raise":
if amount is None:
return self._auto_fold(player_index, "raise amount missing")
if amount <= 0:
return self._auto_fold(player_index, "raise amount must be positive")
call_amount = max(0, self.game_state.current_bet - player.current_bet)
total_commit = call_amount + amount
if total_commit > player.chips:
return self._auto_fold(
player_index,
f"raise requires ${total_commit} but only ${player.chips} available",
)
if total_commit <= 0:
return self._auto_fold(player_index, "raise must increase the bet")
if player.bet(total_commit):
self.game_state.pot += total_commit
success = True
action_display = f"Raise to ${player.current_bet}"
elif action == "check":
if self.game_state.current_bet == player.current_bet:
success = True
action_display = "Check"
else:
return self._auto_fold(player_index, "cannot check while facing a bet")
else:
return self._auto_fold(player_index, f"unknown action '{action}'")
if not success:
return self._auto_fold(player_index, f"failed to execute {action}")
self.game_state.current_bet = max(previous_bet, player.current_bet)
was_raise = player.current_bet > previous_bet
if was_raise:
self.game_state.pending_players = {
idx for idx in self._players_who_can_act() if idx != player_index
}
else:
self.game_state.pending_players.discard(player_index)
self._remove_inactive_from_pending()
if self.gui:
self.gui.update_display()
player.last_action = action
if player.is_all_in and action_display:
action_display += " (All-In)"
player.last_action_display = action_display or action.title()
return True
def evaluate_hand(self, cards: List[Card]) -> Tuple[int, List[int]]:
"""Evaluate a poker hand and return (hand_rank, kickers)"""
if len(cards) < 5:
values = sorted([card.get_value() for card in cards], reverse=True)
return (1 if values else 0, values)
# Get all possible 5-card combinations
from itertools import combinations
best_hand = (0, [])
for combo in combinations(cards, 5):
hand_rank, kickers = self._evaluate_five_cards(list(combo))
if hand_rank > best_hand[0] or (hand_rank == best_hand[0] and kickers > best_hand[1]):
best_hand = (hand_rank, kickers)
return best_hand
def _evaluate_five_cards(self, cards: List[Card]) -> Tuple[int, List[int]]:
"""Evaluate a 5-card hand"""
values = [card.get_value() for card in cards]
suits = [card.suit for card in cards]
value_counts = Counter(values)
sorted_values_desc = sorted(values, reverse=True)
is_flush = len(set(suits)) == 1
is_straight, straight_high = self._is_straight(values)
count_groups = sorted(
value_counts.items(),
key=lambda item: (-item[1], -item[0])
)
# Frequency distribution simplifies identifying pairs, trips, etc.
counts = [count for _, count in count_groups]
ordered_vals = [val for val, _ in count_groups]
secondary_count = counts[1] if len(counts) > 1 else 0
if is_flush and is_straight:
if straight_high == 14 and sorted(values) == [10, 11, 12, 13, 14]:
return (10, [14])
return (9, [straight_high])
if counts[0] == 4:
four = ordered_vals[0]
kicker = max(v for v in values if v != four)
return (8, [four, kicker])
if counts[0] == 3 and secondary_count == 2:
return (7, [ordered_vals[0], ordered_vals[1]])
if is_flush:
return (6, sorted_values_desc)
if is_straight:
return (5, [straight_high])
if counts[0] == 3:
kickers = sorted([v for v in values if v != ordered_vals[0]], reverse=True)
return (4, [ordered_vals[0]] + kickers)
if counts[0] == 2 and secondary_count == 2:
high_pair, low_pair = ordered_vals[:2]
kicker = max(v for v in values if v not in (high_pair, low_pair))
return (3, [high_pair, low_pair, kicker])
if counts[0] == 2:
pair = ordered_vals[0]
kickers = sorted([v for v in values if v != pair], reverse=True)
return (2, [pair] + kickers)
return (1, sorted_values_desc)
def _is_straight(self, values: List[int]) -> Tuple[bool, int]:
"""Check if values form a straight and return (is_straight, high_card)"""
unique_values = sorted(set(values))
if len(unique_values) != 5:
return False, 0
# Wheel straight (A-2-3-4-5)
if unique_values == [2, 3, 4, 5, 14]:
return True, 5
for i in range(4):
if unique_values[i + 1] - unique_values[i] != 1:
return False, 0
return True, unique_values[-1]
def determine_winner(self) -> List[Player]:
"""Determine the winner(s) of the current hand"""
# Reset previously stored hand summaries before evaluating fresh results
for player in self.game_state.players:
player.best_hand_rank = 0
player.best_hand_name = None
active_players = [p for p in self.game_state.players if not p.is_folded]
if len(active_players) == 1:
active_players[0].best_hand_rank = 1
active_players[0].best_hand_name = "Last Player Standing"
return active_players
# Evaluate each player's best hand
player_hands = []
for player in active_players:
all_cards = player.hole_cards + self.game_state.community_cards
hand_rank, kickers = self.evaluate_hand(all_cards)
player.best_hand_rank = hand_rank
player.best_hand_name = self._hand_rank_to_name(hand_rank, kickers)
player_hands.append((player, hand_rank, kickers))
# Sort by hand strength
player_hands.sort(key=lambda x: (x[1], x[2]), reverse=True)
# Find winners (players with the same best hand)
best_hand = player_hands[0][1:]
winners = [p[0] for p in player_hands if p[1:] == best_hand]
return winners
def award_pot(self, winners: List[Player]):
"""Award the pot to the winner(s)"""
if not winners:
return
share_count = len(winners)
if share_count == 0:
return
pot_per_winner = self.game_state.pot // share_count
remainder = self.game_state.pot % share_count
for i, winner in enumerate(winners):
amount = pot_per_winner + (1 if i < remainder else 0)
winner.chips += amount
self.game_state.pot = 0
self.game_state.pending_players.clear()
# Eliminate players who failed to rebuild their stack and did not win the pot
for player in self.game_state.players:
if player in winners and player.chips > 0:
player.is_eliminated = False
elif player.chips == 0 and player not in winners:
player.is_eliminated = True
player.is_folded = True
player.last_action_display = "Eliminated"
# Victory check: one player holds every chip
total_chips = sum(p.chips for p in self.game_state.players)
champions = [p for p in self.game_state.players if p.chips == total_chips and total_chips > 0]
if champions and all(p.is_eliminated or p in champions for p in self.game_state.players):
winner = champions[0]
self.game_over = True
victory_msg = f"🎉 {winner.name} wins the tournament! 🎉"
self.last_action_note = victory_msg
if self.gui:
self.gui.log_message(victory_msg, color="info")
def get_random_action(self, player: Player) -> Tuple[str, int]:
"""Get a random action for a player"""
actions = ["fold", "call", "check", "raise"]
weights = [0.1, 0.3, 0.3, 0.3] # Favor call/check/raise over fold
action = random.choices(actions, weights=weights)[0]
amount = 0
call_amount = max(0, self.game_state.current_bet - player.current_bet)
if action == "raise":
max_additional = player.chips - call_amount
if max_additional <= 0:
if call_amount > 0 and player.chips > 0:
return "call", min(call_amount, player.chips)
return "check", 0
raise_cap = min(25, max_additional)
if raise_cap < 1:
if call_amount > 0 and player.chips > 0:
return "call", min(call_amount, player.chips)
return "check", 0
min_raise = max(1, min(5, raise_cap))
amount = random.randint(min_raise, raise_cap) if raise_cap >= min_raise else raise_cap
amount = max(1, amount)
if call_amount + amount > player.chips:
amount = player.chips - call_amount
if amount <= 0:
if call_amount > 0 and player.chips > 0:
return "call", min(call_amount, player.chips)
return "check", 0
return "raise", amount
if action == "call":
if call_amount == 0:
return "check", 0
return "call", min(call_amount, player.chips)
if action == "check":
return "check", 0
return "fold", 0
def play_autonomous_round(self):
"""Play one round of autonomous poker"""
if self.game_over:
return False
# Deal a new hand if the previous showdown requested one
if self.pending_new_hand:
self.start_new_hand()
if self.game_over:
return False
if self.gui:
self.gui.update_display()
return True
# Check if only one non-eliminated player remains with chips
active_players = [p for p in self.game_state.players if not p.is_eliminated]
if len(active_players) <= 1:
if len(active_players) == 1:
winner = active_players[0]
self.game_over = True
msg = f"🎉 GAME OVER! {winner.name} WINS THE TOURNAMENT! 🎉"
self.last_action_note = msg
if self.gui:
self.gui.log_message(msg, color="info")
return False
else:
if self.gui:
self.gui.log_message("Game ended - no players with chips")
return False
if self.game_state.game_phase == "showdown":
# Determine winner and award pot
winners = self.determine_winner()
if winners:
pot_amount = self.game_state.pot # Store pot amount before awarding
self.award_pot(winners)
winner_names = [w.name for w in winners]
if self.gui:
self.gui.update_display()
self.gui.log_message(f"Winners: {', '.join(winner_names)} win ${pot_amount}")
else:
self.last_action_note = f"Winners: {', '.join(winner_names)} win ${pot_amount}"
self.pending_new_hand = True
return True
# Check if we should progress to next phase
if self._should_progress_phase():
self.next_phase()
if self.gui:
self.gui.log_message(f"Phase progressed to: {self.game_state.game_phase}")
return True
# Get current player
current_player = self.game_state.players[self.game_state.current_player]
# Skip players with no chips
if current_player.chips <= 0:
self.next_player()
return True
if current_player.is_folded or current_player.is_all_in:
self.next_player()
return True
# Get random action from agent
action, amount = self._get_agent_action(current_player)
# Log the action
if self.gui:
self.gui.log_message(f"{current_player.name} {action}s" + (f" ${amount}" if amount > 0 else ""))
# Execute action
success = self.player_action(self.game_state.current_player, action, amount)
# Set last action after successful execution
if success:
current_player.last_action = action
if success:
self.next_player()
return True
def _should_progress_phase(self):
"""Check if we should progress to the next phase"""
# Count active players
active_players = [p for p in self.game_state.players if not p.is_folded]
if len(active_players) <= 1:
return True
if not self.game_state.pending_players:
return True
def _get_agent_action(self, player):
"""Get action from player's agent"""
agent = player.agent
if agent and hasattr(agent, 'make_decision'):
try:
game_state = self._build_agent_game_state(player)
prepared = False
if isinstance(agent, PokerAgentBase):
agent._prepare_turn(game_state)
prepared = True
decision = agent.make_decision(game_state)
action, amount = self._normalize_agent_action(decision)
if action is None or not self._is_valid_agent_action(player, action, amount):
player.pending_invalid_reason = (
f"{player.name}'s agent attempted an invalid move ({decision})."
)
if isinstance(agent, PokerAgentBase):
agent.debug(
f"Invalid decision {decision} with call_required={game_state['call_required']} "
f"and stack={player.chips}"
)
return "fold", 0
if action == "all-in":
call_required = game_state['call_required']
available = player.chips
if available <= 0:
return "fold", 0
if call_required > available:
if isinstance(agent, PokerAgentBase):
agent.debug(
f"Invalid all-in: needs ${call_required} to call but only has ${available}"
)
return "fold", 0
raise_amount = available - call_required
if raise_amount <= 0:
return "call", available
return "raise", raise_amount
return action, amount
except Exception as exc:
if isinstance(agent, PokerAgentBase):
agent.debug(f"Error during decision: {exc}")
return "fold", 0
finally:
if isinstance(agent, PokerAgentBase):
agent._finish_turn()
# Fallback to random action
return self.get_random_action(player)
def next_player(self):
"""Move to the next active player"""
if not self.game_state.players:
return
total_players = len(self.game_state.players)
start_index = self.game_state.current_player
for offset in range(1, total_players + 1):
candidate = (start_index + offset) % total_players
candidate_player = self.game_state.players[candidate]
if (candidate_player.is_folded or candidate_player.is_all_in or
candidate_player.chips <= 0 or candidate_player.is_eliminated):
continue
if self.game_state.pending_players and candidate not in self.game_state.pending_players:
continue
self.game_state.current_player = candidate
return
# Fallback to original player if no valid candidate found
self.game_state.current_player = start_index
def _pick_first_player_for_hand(self) -> int:
"""Rotate the first-to-act position, skipping eliminated or broke players."""
players = self.game_state.players
if not players:
return 0
total_players = len(players)
start = (self.game_state.dealer_position + 1) % total_players
for offset in range(total_players):
candidate = (start + offset) % total_players
candidate_player = players[candidate]
if candidate_player.chips > 0 and not candidate_player.is_eliminated:
self.game_state.dealer_position = candidate
return candidate
return 0
def _end_game_due_to_limit(self):
"""Declare a winner when the configured hand limit is reached."""
if self.game_over:
return
players = self.game_state.players
winners = []
max_chips = 0
if players:
max_chips = max(p.chips for p in players)
winners = [p for p in players if p.chips == max_chips]
self.game_state.winner = winners if len(winners) != 1 else winners[0]
self.game_over = True
self.pending_new_hand = False
self.game_state.pending_players.clear()
if not winners:
message = "Hand limit reached. No winners could be determined."
elif len(winners) == 1:
message = (
f"Hand limit reached. {winners[0].name} wins with ${max_chips} in chips."
)
else:
names = ", ".join(p.name for p in winners)
message = (
f"Hand limit reached. {names} tie for the lead with ${max_chips} each."
)
self.last_action_note = message
if self.gui:
self.gui.log_message(message, color="info")
self.gui.show_status_message(message, error=False)
self.gui.update_display()
def _players_who_can_act(self):
"""Return indices of players who can take an action"""
return [
idx for idx, player in enumerate(self.game_state.players)
if (not player.is_folded and not player.is_all_in and
player.chips > 0 and not player.is_eliminated)
]
def _reset_pending_players(self, starting_index=None):
"""Reset the set of players who still need to act in the current betting round"""
self.game_state.pending_players = set(self._players_who_can_act())
if not self.game_state.pending_players:
return
target_index = self.game_state.current_player if starting_index is None else starting_index
if target_index not in self.game_state.pending_players:
total_players = len(self.game_state.players)
for offset in range(total_players):
candidate = (target_index + offset) % total_players
if candidate in self.game_state.pending_players:
self.game_state.current_player = candidate
break
else:
self.game_state.current_player = target_index
def _remove_inactive_from_pending(self):
"""Remove players who can no longer act from the pending set"""
inactive = {
idx for idx in list(self.game_state.pending_players)
if idx >= len(self.game_state.players)
or self.game_state.players[idx].is_folded
or self.game_state.players[idx].is_all_in
or self.game_state.players[idx].chips <= 0
}
self.game_state.pending_players -= inactive
def _auto_fold(self, player_index: int, reason: str) -> bool:
"""Force a player to fold after an invalid move and record a user-facing message."""
if player_index >= len(self.game_state.players):
return False
player = self.game_state.players[player_index]
if not player.is_folded:
player.fold()
player.last_action = "fold"
player.last_action_display = "Invalid -> Fold"
player.pending_invalid_reason = None
if isinstance(player.agent, PokerAgentBase):
player.agent.debug(f"Forcing fold: {reason}")
self.game_state.pending_players.discard(player_index)
self.last_action_note = f"{player.name}: invalid action - {reason}. Automatic fold applied."
return True
def pop_last_action_note(self) -> Optional[str]:
"""Return and clear the latest action note (e.g., invalid move warnings)."""
note = self.last_action_note
self.last_action_note = None
return note
def _hand_rank_to_name(self, hand_rank: int, kickers: List[int]) -> str:
"""Translate a numeric hand rank and kickers into a human-readable label."""
label = HAND_RANK_LABELS.get(hand_rank, "Unknown Hand")
if hand_rank == 10: # Royal Flush
return label
if hand_rank == 9: # Straight Flush
return f"{label} ({self._value_to_name(kickers[0])} high)"
if hand_rank == 8: # Four of a Kind
return f"Four of {self._value_to_plural_name(kickers[0])}"
if hand_rank == 7: # Full House
return (
f"Full House ({self._value_to_plural_name(kickers[0])} over "
f"{self._value_to_plural_name(kickers[1])})"
)
if hand_rank == 6: # Flush
return f"Flush ({self._value_to_name(kickers[0])} high)"
if hand_rank == 5: # Straight
return f"Straight to {self._value_to_name(kickers[0])}"
if hand_rank == 4: # Three of a Kind
return f"Three of {self._value_to_plural_name(kickers[0])}"
if hand_rank == 3: # Two Pair
high_pair, low_pair = kickers[:2]
return (
f"Two Pair ({self._value_to_plural_name(high_pair)} and "
f"{self._value_to_plural_name(low_pair)})"
)
if hand_rank == 2: # One Pair
return f"Pair of {self._value_to_plural_name(kickers[0])}"
if hand_rank == 1: # High Card
return f"High Card {self._value_to_name(kickers[0])}"
return label
def _value_to_name(self, value: int) -> str:
"""Return the singular card rank name for a numeric value."""
return VALUE_NAMES.get(value, str(value))
def _value_to_plural_name(self, value: int) -> str:
"""Return the pluralised card rank name for a numeric value."""
return PLURAL_VALUE_NAMES.get(value, f"{self._value_to_name(value)}s")
def _build_agent_game_state(self, player: Player) -> Dict[str, Any]:
"""Create a restricted game state view for agents."""
player_index = self.game_state.players.index(player)
total_players = len(self.game_state.players)
previous_player = None
if total_players > 1:
previous_player = self.game_state.players[(player_index - 1) % total_players]
# Amount this player must contribute to call the current bet
call_required = max(0, self.game_state.current_bet - player.current_bet)
return {
'self': {
'name': player.name,
'chips': player.chips,
'hole_cards': list(player.hole_cards),
'current_bet': player.current_bet,
'total_bet': player.total_bet,
'is_all_in': player.is_all_in,
},
'community_cards': list(self.game_state.community_cards),
'pot': self.game_state.pot,
'current_bet': self.game_state.current_bet,
'call_required': call_required,
'game_phase': self.game_state.game_phase,
'other_player_moves': [
{
'name': other.name,
'last_action': other.last_action,
}
for other in self.game_state.players
if other is not player
],
'previous_player_action': (
{
'name': previous_player.name,
'last_action': previous_player.last_action,
}
if previous_player and previous_player is not player
else None
),
}
def _normalize_agent_action(self, decision) -> Tuple[Optional[str], Optional[int]]:
"""Normalize an agent's decision into (action, amount)."""
action = None
amount = 0
if isinstance(decision, tuple):
if not decision:
return None, None
action = decision[0]