terminal-poker 1.0.1

Heads-up No-Limit Texas Hold'em poker for the terminal
Documentation
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
use super::actions::{Action, AvailableActions};
use super::deck::{Card, Deck};
use super::hand::{evaluate_hand, HandEvaluation};
use serde::{Deserialize, Serialize};

pub const BIG_BLIND: u32 = 2;
pub const SMALL_BLIND: u32 = 1;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Player {
    Human,
    Bot,
}

impl Player {
    pub fn opponent(&self) -> Self {
        match self {
            Player::Human => Player::Bot,
            Player::Bot => Player::Human,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GamePhase {
    Preflop,
    Flop,
    Turn,
    River,
    Showdown,
    HandComplete,
    SessionEnd,
    Summary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Street {
    Preflop,
    Flop,
    Turn,
    River,
}

impl From<GamePhase> for Option<Street> {
    fn from(phase: GamePhase) -> Self {
        match phase {
            GamePhase::Preflop => Some(Street::Preflop),
            GamePhase::Flop => Some(Street::Flop),
            GamePhase::Turn => Some(Street::Turn),
            GamePhase::River => Some(Street::River),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct GameState {
    pub phase: GamePhase,
    pub deck: Deck,
    pub player_cards: Vec<Card>,
    pub bot_cards: Vec<Card>,
    pub board: Vec<Card>,
    pub pot: u32,
    pub player_stack: u32,
    pub bot_stack: u32,
    pub player_bet: u32,
    pub bot_bet: u32,
    pub to_act: Player,
    pub button: Player,
    pub last_aggressor: Option<Player>,
    pub preflop_aggressor: Option<Player>,
    pub last_raise_size: u32,
    pub hand_number: u32,
    pub starting_stack: u32,
    pub hands_played: u32,
    pub hands_won: u32,
    pub biggest_pot_won: u32,
    pub biggest_pot_lost: u32,
    pub last_action: Option<(Player, Action)>,
    pub showdown_result: Option<ShowdownResult>,
    pub actions_this_street: u8,
}

#[derive(Debug, Clone)]
pub struct ShowdownResult {
    pub winner: Option<Player>,
    pub player_hand: HandEvaluation,
    pub bot_hand: HandEvaluation,
    pub pot_won: u32,
}

impl GameState {
    pub fn new(starting_stack_bb: u32) -> Self {
        let starting_stack = starting_stack_bb * BIG_BLIND;
        let mut state = Self {
            phase: GamePhase::Preflop,
            deck: Deck::new(),
            player_cards: Vec::new(),
            bot_cards: Vec::new(),
            board: Vec::new(),
            pot: 0,
            player_stack: starting_stack,
            bot_stack: starting_stack,
            player_bet: 0,
            bot_bet: 0,
            to_act: Player::Human,
            button: Player::Bot,
            last_aggressor: None,
            preflop_aggressor: None,
            last_raise_size: BIG_BLIND,
            hand_number: 0,
            starting_stack,
            hands_played: 0,
            hands_won: 0,
            biggest_pot_won: 0,
            biggest_pot_lost: 0,
            last_action: None,
            showdown_result: None,
            actions_this_street: 0,
        };
        state.start_new_hand();
        state
    }

    pub fn start_new_hand(&mut self) {
        self.hand_number += 1;
        self.button = self.button.opponent();
        self.phase = GamePhase::Preflop;
        self.deck = Deck::new();
        self.deck.shuffle();
        self.player_cards = self.deck.deal_n(2);
        self.bot_cards = self.deck.deal_n(2);
        self.board.clear();
        self.pot = 0;
        self.player_bet = 0;
        self.bot_bet = 0;
        self.last_aggressor = None;
        self.preflop_aggressor = None;
        self.last_raise_size = BIG_BLIND;
        self.last_action = None;
        self.showdown_result = None;
        self.actions_this_street = 0;

        // Post blinds - button posts SB, other player posts BB
        // In heads-up, button acts first preflop
        match self.button {
            Player::Human => {
                // Human is button (SB), Bot is BB
                let sb = SMALL_BLIND.min(self.player_stack);
                let bb = BIG_BLIND.min(self.bot_stack);
                self.player_stack -= sb;
                self.player_bet = sb;
                self.bot_stack -= bb;
                self.bot_bet = bb;
                self.pot = sb + bb;
                self.to_act = Player::Human; // Button acts first preflop
            }
            Player::Bot => {
                // Bot is button (SB), Human is BB
                let sb = SMALL_BLIND.min(self.bot_stack);
                let bb = BIG_BLIND.min(self.player_stack);
                self.bot_stack -= sb;
                self.bot_bet = sb;
                self.player_stack -= bb;
                self.player_bet = bb;
                self.pot = sb + bb;
                self.to_act = Player::Bot; // Button acts first preflop
            }
        }
    }

    pub fn apply_action(&mut self, player: Player, action: Action) {
        self.last_action = Some((player, action));
        self.actions_this_street += 1;

        match action {
            Action::Fold => {
                self.handle_fold(player);
                return;
            }
            Action::Check => {
                // Nothing to do
            }
            Action::Call(amount) => {
                self.add_chips(player, amount);
            }
            Action::Bet(amount) | Action::Raise(amount) => {
                let current_bet = self.current_bet(player);
                let to_add = amount - current_bet;
                let old_max = self.max_bet(); // Capture before mutation
                self.add_chips(player, to_add);
                self.last_aggressor = Some(player);
                self.last_raise_size = amount - old_max;
                if self.phase == GamePhase::Preflop {
                    self.preflop_aggressor = Some(player);
                }
            }
            Action::AllIn(amount) => {
                let current_bet = self.current_bet(player);
                let to_add = amount - current_bet;
                let old_max = self.max_bet(); // Capture before mutation
                self.add_chips(player, to_add);
                if amount > old_max {
                    self.last_aggressor = Some(player);
                    self.last_raise_size = amount - old_max;
                    if self.phase == GamePhase::Preflop {
                        self.preflop_aggressor = Some(player);
                    }
                }
            }
        }

        // Check if betting round is complete
        if self.is_betting_round_complete() {
            self.advance_phase();
        } else {
            self.to_act = player.opponent();
        }
    }

    fn add_chips(&mut self, player: Player, amount: u32) {
        match player {
            Player::Human => {
                let actual = amount.min(self.player_stack);
                self.player_stack -= actual;
                self.player_bet += actual;
                self.pot += actual;
            }
            Player::Bot => {
                let actual = amount.min(self.bot_stack);
                self.bot_stack -= actual;
                self.bot_bet += actual;
                self.pot += actual;
            }
        }
    }

    fn current_bet(&self, player: Player) -> u32 {
        match player {
            Player::Human => self.player_bet,
            Player::Bot => self.bot_bet,
        }
    }

    pub fn max_bet(&self) -> u32 {
        self.player_bet.max(self.bot_bet)
    }

    fn handle_fold(&mut self, folder: Player) {
        let winner = folder.opponent();
        let pot = self.pot;

        match winner {
            Player::Human => {
                self.player_stack += pot;
                self.hands_won += 1;
                if pot > self.biggest_pot_won {
                    self.biggest_pot_won = pot;
                }
            }
            Player::Bot => {
                self.bot_stack += pot;
                if pot > self.biggest_pot_lost {
                    self.biggest_pot_lost = pot;
                }
            }
        }

        self.pot = 0;
        self.hands_played += 1;
        self.phase = GamePhase::HandComplete;
    }

    fn is_betting_round_complete(&self) -> bool {
        // Both players all-in: always complete
        if self.player_stack == 0 && self.bot_stack == 0 {
            return true;
        }

        // One player all-in
        if self.player_stack == 0 || self.bot_stack == 0 {
            let allin_bet = if self.player_stack == 0 {
                self.player_bet
            } else {
                self.bot_bet
            };
            let other_bet = if self.player_stack == 0 {
                self.bot_bet
            } else {
                self.player_bet
            };

            // Short all-in (pushed for <= other's existing bet): complete immediately
            if allin_bet <= other_bet {
                return true;
            }

            // All-in for more: complete when other player matches
            return self.player_bet == self.bot_bet;
        }

        // Bets must be equal
        if self.player_bet != self.bot_bet {
            return false;
        }

        // Preflop special case: BB gets option if no raise
        if self.phase == GamePhase::Preflop {
            if self.last_aggressor.is_none() {
                // No raise yet, BB gets option. Round complete only when BB has checked.
                let bb_player = self.button.opponent();
                return self
                    .last_action
                    .map(|(actor, action)| actor == bb_player && action == Action::Check)
                    .unwrap_or(false);
            }
        }

        // Postflop: both players must have acted for round to complete
        // (With no aggressor and equal bets, need at least 2 actions = both checked)
        if self.last_aggressor.is_none() {
            return self.actions_this_street >= 2;
        }

        // With an aggressor, if bets are equal, the other player called
        true
    }

    pub fn advance_phase(&mut self) {
        // Return excess chips for unequal all-in bets (heads-up: no side pots needed)
        if self.player_bet != self.bot_bet {
            let effective = self.player_bet.min(self.bot_bet);
            if self.player_bet > effective {
                let excess = self.player_bet - effective;
                self.player_stack += excess;
                self.pot -= excess;
            } else {
                let excess = self.bot_bet - effective;
                self.bot_stack += excess;
                self.pot -= excess;
            }
        }

        // Reset street bets and action counter
        self.player_bet = 0;
        self.bot_bet = 0;
        self.last_aggressor = None;
        self.actions_this_street = 0;

        match self.phase {
            GamePhase::Preflop => {
                self.board.extend(self.deck.deal_n(3));
                self.phase = GamePhase::Flop;
            }
            GamePhase::Flop => {
                self.board.extend(self.deck.deal_n(1));
                self.phase = GamePhase::Turn;
            }
            GamePhase::Turn => {
                self.board.extend(self.deck.deal_n(1));
                self.phase = GamePhase::River;
            }
            GamePhase::River => {
                self.resolve_showdown();
                return;
            }
            _ => return,
        }

        // Postflop: BB (non-button) acts first
        self.to_act = self.button.opponent();
    }

    fn resolve_showdown(&mut self) {
        let player_eval = evaluate_hand(&self.player_cards, &self.board);
        let bot_eval = evaluate_hand(&self.bot_cards, &self.board);

        let winner = match player_eval.rank.cmp(&bot_eval.rank) {
            std::cmp::Ordering::Greater => Some(Player::Human),
            std::cmp::Ordering::Less => Some(Player::Bot),
            std::cmp::Ordering::Equal => {
                match player_eval.kickers.cmp(&bot_eval.kickers) {
                    std::cmp::Ordering::Greater => Some(Player::Human),
                    std::cmp::Ordering::Less => Some(Player::Bot),
                    std::cmp::Ordering::Equal => None, // Split pot
                }
            }
        };

        let pot = self.pot;
        match winner {
            Some(Player::Human) => {
                self.player_stack += pot;
                self.hands_won += 1;
                if pot > self.biggest_pot_won {
                    self.biggest_pot_won = pot;
                }
            }
            Some(Player::Bot) => {
                self.bot_stack += pot;
                if pot > self.biggest_pot_lost {
                    self.biggest_pot_lost = pot;
                }
            }
            None => {
                // Split pot - odd chip goes to out-of-position player (non-button)
                let half = pot / 2;
                let remainder = pot % 2;
                if self.button == Player::Human {
                    // Bot is out of position, gets odd chip
                    self.player_stack += half;
                    self.bot_stack += half + remainder;
                } else {
                    // Human is out of position, gets odd chip
                    self.player_stack += half + remainder;
                    self.bot_stack += half;
                }
            }
        }

        self.showdown_result = Some(ShowdownResult {
            winner,
            player_hand: player_eval,
            bot_hand: bot_eval,
            pot_won: pot,
        });

        self.pot = 0;
        self.hands_played += 1;
        self.phase = GamePhase::Showdown;
    }

    pub fn amount_to_call(&self, player: Player) -> u32 {
        let current = self.current_bet(player);
        let max = self.max_bet();
        if max > current {
            max - current
        } else {
            0
        }
    }

    pub fn available_actions(&self) -> AvailableActions {
        let stack = match self.to_act {
            Player::Human => self.player_stack,
            Player::Bot => self.bot_stack,
        };

        let to_call = self.amount_to_call(self.to_act);
        let min_raise_to = self.max_bet() + self.last_raise_size.max(BIG_BLIND);

        AvailableActions::new(to_call, min_raise_to, stack, BIG_BLIND)
    }

    #[allow(dead_code)]
    pub fn pot_odds(&self) -> Option<(f64, f64)> {
        let to_call = self.amount_to_call(Player::Human);
        if to_call == 0 {
            return None;
        }

        let pot_after_call = self.pot + to_call;
        let ratio = pot_after_call as f64 / to_call as f64;
        let equity_needed = to_call as f64 / pot_after_call as f64;

        Some((ratio, equity_needed))
    }

    pub fn is_player_turn(&self) -> bool {
        self.to_act == Player::Human
            && !matches!(
                self.phase,
                GamePhase::Showdown
                    | GamePhase::HandComplete
                    | GamePhase::SessionEnd
                    | GamePhase::Summary
            )
    }

    pub fn session_profit_bb(&self) -> f64 {
        let current = self.player_stack as f64;
        let starting = self.starting_stack as f64;
        (current - starting) / BIG_BLIND as f64
    }
}