open_pql/functions/
nut_hi.rs

1use super::*;
2///  It determines whether a specified player has the "nuts" (the best possible hand) on a given street in poker. For example, calling `nutHi(p1, flop)` returns `true` if player one (`p1`) holds the hand `'Qs5s'` and the community board shows `'JsKsAs'`. It's important to note that the function assumes all dead cards are known. Thus, `nutHi(p1, flop)` for `p1='As2s'` and `board='KsQsJs'` would initially return `false`. However, if a dead card `'Ts'` is introduced, the function would then return `true`.
3#[pqlfn(arg, rtn, eval)]
4pub fn nut_hi(
5    hand: &Hand,
6    street: PQLStreet,
7    (game, board, dead): (PQLGame, Board, DeadCards),
8) -> PQLBoolean {
9    // TODO: optimize
10
11    let known_cards = Card64::from(dead) | (board, street).into() | hand.into();
12
13    let player_rating = hi_rating(hand, street, (game, board));
14
15    for other in (!known_cards).iter().combinations(game.n_cards().into()) {
16        let other_rating = hi_rating(&other, street, (game, board));
17
18        if other_rating > player_rating {
19            return false;
20        }
21    }
22
23    true
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use crate::*;
30
31    #[test]
32    fn test_nut_hi_holdem() {
33        let g = PQLGame::Holdem;
34
35        assert!(nut_hi(
36            cards!("Qs5s").as_ref(),
37            PQLStreet::Flop,
38            (g, board!("AsJsKs 3h4c"), DeadCards::default())
39        ));
40
41        assert!(!nut_hi(
42            cards!("As2s").as_ref(),
43            PQLStreet::Flop,
44            (g, board!("KsQsJs 3h4c"), DeadCards::default())
45        ));
46
47        assert!(nut_hi(
48            cards!("As2s").as_ref(),
49            PQLStreet::Flop,
50            (g, board!("KsQsJs 3h4c"), c64!("Ts").into())
51        ));
52    }
53
54    #[test]
55    fn test_nut_hi_omaha() {
56        let g = PQLGame::Omaha;
57
58        assert!(nut_hi(
59            cards!("9s8s 2s2h").as_ref(),
60            PQLStreet::River,
61            (g, board!("KsQsJsTs 2c"), DeadCards::default())
62        ));
63    }
64}