-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpoker_cli.py
More file actions
193 lines (170 loc) · 7 KB
/
poker_cli.py
File metadata and controls
193 lines (170 loc) · 7 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
#!/usr/bin/env python3
"""
Command-line poker game for environments without GUI
"""
from typing import Optional
from game_manager import GameManager, Card, Suit
from poker_agents.agent_base import PokerAgentBase
class PokerCLI:
def __init__(
self,
move_interval: float = 1.0,
starting_chips: Optional[int] = None,
max_hand_limit: Optional[int] = None,
):
starting_stack = (
PokerAgentBase.STARTING_CHIPS if starting_chips is None else starting_chips
)
self.game = GameManager(
move_interval=move_interval,
starting_chips=starting_stack,
max_hand_limit=max_hand_limit,
)
self.running = True
def display_game_state(self):
"""Display current game state"""
print("\n" + "="*60)
limit = self.game.max_hand_limit
hand_no = self.game.game_state.hand_count
hand_line = f"HAND #: {hand_no}"
if limit:
hand_line += f" / {limit}"
print(hand_line)
print(f"POT: ${self.game.game_state.pot}")
print(f"PHASE: {self.game.game_state.game_phase.upper()}")
if self.game.game_state.players:
current_idx = self.game.game_state.current_player
current_idx = min(current_idx, len(self.game.game_state.players) - 1)
print(f"CURRENT PLAYER: {self.game.game_state.players[current_idx].name}")
else:
print("CURRENT PLAYER: None")
print("="*60)
# Display community cards
if self.game.game_state.community_cards:
print("COMMUNITY CARDS:")
for i, card in enumerate(self.game.game_state.community_cards):
print(f" {i+1}. {card}")
else:
print("COMMUNITY CARDS: None yet")
print("\nPLAYERS:")
for i, player in enumerate(self.game.game_state.players):
status = []
if player.is_folded:
status.append("FOLDED")
if player.is_all_in:
status.append("ALL IN")
if getattr(player, "is_eliminated", False):
status.append("ELIMINATED")
if player.current_bet > 0:
status.append(f"BET: ${player.current_bet}")
status_str = f" ({', '.join(status)})" if status else ""
print(f" {i+1}. {player.name}: ${player.chips} chips{status_str}")
if player.hole_cards and not player.is_folded:
print(f" Cards: {[str(card) for card in player.hole_cards]}")
if player.best_hand_name:
print(f" Best Hand: {player.best_hand_name}")
if player.last_action_display:
print(f" Last Move: {player.last_action_display}")
def show_menu(self):
"""Show the main menu"""
print("\n" + "="*40)
print("POKER GAME MENU")
print("="*40)
print("1. New Hand")
print("2. Next Phase")
print("3. Play Autonomous Round")
print("4. Show Game State")
print("5. Quit")
print("="*40)
def player_action_menu(self):
"""Show player action menu"""
current_player = self.game.game_state.players[self.game.game_state.current_player]
print(f"\n{current_player.name}'s turn:")
print("1. Fold")
print("2. Call")
print("3. Check")
print("4. Raise")
print("5. Back to main menu")
choice = input("Choose action (1-5): ").strip()
if choice == "1":
success = self.game.player_action(self.game.game_state.current_player, "fold")
self._handle_action_result(success, f"{current_player.name} folded")
elif choice == "2":
success = self.game.player_action(self.game.game_state.current_player, "call")
self._handle_action_result(success, f"{current_player.name} called")
elif choice == "3":
success = self.game.player_action(self.game.game_state.current_player, "check")
self._handle_action_result(success, f"{current_player.name} checked")
elif choice == "4":
try:
amount = int(input("Raise amount: $"))
success = self.game.player_action(self.game.game_state.current_player, "raise", amount)
self._handle_action_result(
success,
f"{current_player.name} raised by ${amount}",
"Invalid raise amount",
)
except ValueError:
print("Invalid amount")
elif choice == "5":
return
else:
print("Invalid choice")
def play_autonomous_round(self):
"""Play one autonomous round"""
print("Playing autonomous round...")
continue_playing = self.game.play_autonomous_round()
self.display_game_state()
self._report_last_note()
if not continue_playing:
print("Round completed!")
def next_player(self):
"""Move to next player"""
self.game.game_state.current_player = (self.game.game_state.current_player + 1) % len(self.game.game_state.players)
def _handle_action_result(self, success, success_message=None, failure_message=None):
"""Print action outcomes and advance to the next player when needed."""
note = self.game.pop_last_action_note()
if note:
print(f"!!! {note}")
elif success and success_message:
print(success_message)
elif not success and failure_message:
print(failure_message)
if success:
self.next_player()
def _report_last_note(self):
"""Print any deferred engine notes (e.g., invalid move auto-folds)."""
note = self.game.pop_last_action_note()
if note:
print(f"!!! {note}")
def run(self):
"""Run the CLI game"""
print("AI Poker Competition - Command Line Interface")
print("=" * 50)
while self.running:
self.show_menu()
choice = input("Choose option (1-5): ").strip()
if choice == "1":
self.game.start_new_hand()
self._report_last_note()
if self.game.game_over:
print("Game has concluded.")
self.running = False
else:
self.display_game_state()
elif choice == "2":
self.game.next_phase()
print(f"Phase changed to: {self.game.game_state.game_phase}")
self.display_game_state()
elif choice == "3":
self.play_autonomous_round()
elif choice == "4":
self.display_game_state()
elif choice == "5":
print("Thanks for playing!")
self.running = False
else:
print("Invalid choice, please try again")
if __name__ == "__main__":
cli = PokerCLI()
cli.run()