ducy 0.2.0

A performant analysis library for poker.
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
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
use std::fmt::Display;

use crate::{
    deck::{Deck, Rank, RankSet},
    ranking::standard_hand_ranker::RankOrder,
};

/// Marker trait for types that represent a hand's ranking.
pub trait HandRanking {}

/// Standard poker hand rankings from high card through straight flush.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum StandardHandRanks {
    /// Five unpaired, unconnected cards.
    HighCard {
        /// Highest kicker.
        c1: Rank,
        /// Second kicker.
        c2: Rank,
        /// Third kicker.
        c3: Rank,
        /// Fourth kicker.
        c4: Rank,
        /// Fifth kicker.
        c5: Rank,
    },
    /// Two cards of the same rank.
    OnePair {
        /// Pair rank.
        p: Rank,
        /// First kicker.
        c1: Rank,
        /// Second kicker.
        c2: Rank,
        /// Third kicker.
        c3: Rank,
    },
    /// Two distinct pairs.
    TwoPair {
        /// Higher pair rank.
        p1: Rank,
        /// Lower pair rank.
        p2: Rank,
        /// Kicker.
        c1: Rank,
    },
    /// Three cards of the same rank.
    ThreeOfAKind {
        /// Trips rank.
        t: Rank,
        /// First kicker.
        c1: Rank,
        /// Second kicker.
        c2: Rank,
    },
    /// Five consecutive ranks.
    Straight {
        /// High card of the straight.
        s: Rank,
    },
    /// Five cards of the same suit.
    Flush {
        /// Highest card.
        c1: Rank,
        /// Second card.
        c2: Rank,
        /// Third card.
        c3: Rank,
        /// Fourth card.
        c4: Rank,
        /// Fifth card.
        c5: Rank,
    },
    /// Three of a kind plus a pair.
    FullHouse {
        /// Trips rank.
        t: Rank,
        /// Pair rank.
        p: Rank,
    },
    /// Four cards of the same rank.
    FourOfAKind {
        /// Quads rank.
        q: Rank,
        /// Kicker.
        c: Rank,
    },
    /// Five consecutive cards of the same suit.
    StraightFlush {
        /// High card of the straight flush.
        sf: Rank,
    },
}

impl HandRanking for StandardHandRanks {}

impl Ord for StandardHandRanks {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.get_score().cmp(&other.get_score())
    }
}

impl PartialOrd for StandardHandRanks {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Display for StandardHandRanks {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StandardHandRanks::HighCard { c1, c2, c3, c4, c5 } => {
                write!(f, "High Card {} {} {} {} {}", c1, c2, c3, c4, c5)
            }
            StandardHandRanks::OnePair { p, c1, c2, c3 } => {
                write!(f, "Pair of {p}, {c1} {c2} {c3}")
            }
            StandardHandRanks::TwoPair { p1, p2, c1 } => write!(f, "Two Pair {p1} over {p2}, {c1}"),
            StandardHandRanks::ThreeOfAKind { t, c1, c2 } => {
                write!(f, "Three of a Kind {t}, {c1} {c2}")
            }
            StandardHandRanks::Straight { s } => write!(f, "Straight {s} high"),
            StandardHandRanks::Flush { c1, c2, c3, c4, c5 } => {
                write!(f, "Flush {c1} {c2} {c3} {c4} {c5}")
            }
            StandardHandRanks::FullHouse { t, p } => write!(f, "Full House {t} full of {p}"),
            StandardHandRanks::FourOfAKind { q, c } => write!(f, "Four of a Kind {q}, {c}"),
            StandardHandRanks::StraightFlush { sf } => write!(f, "Straight Flush {sf} high"),
        }
    }
}

const FIVE_OPTIONS: u32 = 13 * 13 * 13 * 13 * 13;
const FOUR_OPTIONS: u32 = 13 * 13 * 13 * 13;
const THREE_OPTIONS: u32 = 13 * 13 * 13;
const TWO_OPTIONS: u32 = 13 * 13;
const ONE_OPTION: u32 = 13;

const ONE_PAIR_BASE: u32 = FIVE_OPTIONS;
const TWO_PAIR_BASE: u32 = ONE_PAIR_BASE + FOUR_OPTIONS;
const TRIP_BASE: u32 = TWO_PAIR_BASE + THREE_OPTIONS;
const STRAIGHT_BASE: u32 = TRIP_BASE + THREE_OPTIONS;
const FLUSH_BASE: u32 = STRAIGHT_BASE + ONE_OPTION;
const FULL_HOUSE_BASE: u32 = FLUSH_BASE + FIVE_OPTIONS;
const FOUR_OF_KIND_BASE: u32 = FULL_HOUSE_BASE + TWO_OPTIONS;
const STRAIGHT_FLUSH_BASE: u32 = FOUR_OF_KIND_BASE + TWO_OPTIONS;

impl StandardHandRanks {
    /// Returns a numeric score for comparison; higher is better.
    pub fn get_score(&self) -> u32 {
        match self {
            StandardHandRanks::HighCard { c1, c2, c3, c4, c5 } => {
                Self::get_score_from_ranks(&[c1, c2, c3, c4, c5])
            }
            StandardHandRanks::OnePair { p, c1, c2, c3 } => {
                Self::get_score_from_ranks(&[p, c1, c2, c3]) + ONE_PAIR_BASE
            }
            StandardHandRanks::TwoPair { p1, p2, c1 } => {
                Self::get_score_from_ranks(&[p1, p2, c1]) + TWO_PAIR_BASE
            }

            StandardHandRanks::ThreeOfAKind { t, c1, c2 } => {
                Self::get_score_from_ranks(&[t, c1, c2]) + TRIP_BASE
            }
            StandardHandRanks::Straight { s } => RankOrder::AceIsHigh.get_score(s) + STRAIGHT_BASE,
            StandardHandRanks::Flush { c1, c2, c3, c4, c5 } => {
                Self::get_score_from_ranks(&[c1, c2, c3, c4, c5]) + FLUSH_BASE
            }
            StandardHandRanks::FullHouse { t, p } => {
                Self::get_score_from_ranks(&[t, p]) + FULL_HOUSE_BASE
            }
            StandardHandRanks::FourOfAKind { q, c } => {
                Self::get_score_from_ranks(&[q, c]) + FOUR_OF_KIND_BASE
            }
            StandardHandRanks::StraightFlush { sf } => {
                RankOrder::AceIsHigh.get_score(sf) + STRAIGHT_FLUSH_BASE
            }
        }
    }

    fn get_score_from_ranks(values: &[&Rank]) -> u32 {
        let mut val = 0;
        for rank in values {
            val = val * 13 + RankOrder::AceIsHigh.get_score(rank)
        }
        val
    }
}

/// Evaluates a set of cards to determine the best standard poker hand.
pub struct StandardHandRanker {}

impl StandardHandRanker {
    /// Returns the best hand ranking from the given cards.
    pub fn get_rank(deck: &Deck) -> StandardHandRanks {
        Self::get_rank_at_least(deck, None).unwrap()
    }

    /// Returns the best hand ranking if it meets or exceeds the minimum, or `None`.
    pub fn get_rank_at_least(
        deck: &Deck,
        must_be_at_least: Option<StandardHandRanks>,
    ) -> Option<StandardHandRanks> {
        let rank_to_beat = must_be_at_least.map(|x| x.get_score()).unwrap_or(0);

        if let Some(sf) = Self::get_best_straight_flush(deck) {
            Some(StandardHandRanks::StraightFlush { sf })
        } else {
            if rank_to_beat >= STRAIGHT_FLUSH_BASE {
                return None;
            }
            let rank_count = deck.get_rank_count();
            let best_quads = rank_count.find_highest_with_n(&[], 4);

            if let Some(quad) = best_quads
                && let Some(kicker) = rank_count.find_highest_with_n(&[quad], 1)
            {
                return Some(StandardHandRanks::FourOfAKind { q: quad, c: kicker });
            }

            if rank_to_beat >= FOUR_OF_KIND_BASE {
                return None;
            }
            let best_trips = rank_count.find_highest_with_n(&[], 3);

            if let Some(trip) = best_trips
                && let Some(pair) = rank_count.find_highest_with_n(&[trip], 2)
            {
                return Some(StandardHandRanks::FullHouse { t: trip, p: pair });
            }

            if rank_to_beat >= FULL_HOUSE_BASE {
                return None;
            }
            if let Some(flush_ranks) = Self::get_flush(deck) {
                return Some(StandardHandRanks::Flush {
                    c1: flush_ranks[0],
                    c2: flush_ranks[1],
                    c3: flush_ranks[2],
                    c4: flush_ranks[3],
                    c5: flush_ranks[4],
                });
            }
            if rank_to_beat >= FLUSH_BASE {
                return None;
            }
            if let Some(s) = Self::get_straight(deck) {
                return Some(StandardHandRanks::Straight { s });
            }
            if rank_to_beat >= STRAIGHT_BASE {
                return None;
            }
            if let Some(trip) = best_trips
                && let Some(c1) = rank_count.find_highest_with_n(&[trip], 1)
                && let Some(c2) = rank_count.find_highest_with_n(&[trip, c1], 1)
            {
                return Some(StandardHandRanks::ThreeOfAKind { t: trip, c1, c2 });
            }

            if rank_to_beat >= TRIP_BASE {
                return None;
            }
            if let Some(best_pair) = rank_count.find_highest_with_n(&[], 2) {
                if let Some(second_best_pair) = rank_count.find_highest_with_n(&[best_pair], 2)
                    && let Some(c) =
                        rank_count.find_highest_with_n(&[best_pair, second_best_pair], 1)
                {
                    return Some(StandardHandRanks::TwoPair {
                        p1: best_pair,
                        p2: second_best_pair,
                        c1: c,
                    });
                }

                if rank_to_beat >= TWO_PAIR_BASE {
                    return None;
                }
                if let Some(c1) = rank_count.find_highest_with_n(&[best_pair], 1)
                    && let Some(c2) = rank_count.find_highest_with_n(&[best_pair, c1], 1)
                    && let Some(c3) = rank_count.find_highest_with_n(&[best_pair, c1, c2], 1)
                {
                    return Some(StandardHandRanks::OnePair {
                        p: best_pair,
                        c1,
                        c2,
                        c3,
                    });
                }
            }
            if rank_to_beat >= ONE_PAIR_BASE {
                return None;
            }
            if let Some(highest_cards) = deck
                .get_combined_ranks()
                .get_highest_five(&RankOrder::AceIsHigh)
            {
                return Some(StandardHandRanks::HighCard {
                    c1: highest_cards[0],
                    c2: highest_cards[1],
                    c3: highest_cards[2],
                    c4: highest_cards[3],
                    c5: highest_cards[4],
                });
            }
            None
        }
    }

    fn get_straight(deck: &Deck) -> Option<Rank> {
        let combined_ranks = deck.get_combined_ranks();
        Self::get_straight_from_rank_bitfield(&combined_ranks)
    }

    fn get_straight_from_rank_bitfield(rank_bitfield: &RankSet) -> Option<Rank> {
        rank_bitfield.matches_pattern(0b11111, 5)
    }

    fn get_flush(deck: &Deck) -> Option<[Rank; 5]> {
        let mut best: Option<[Rank; 5]> = None;
        for (bits, _) in deck.get_single_suit_ranks() {
            if bits.num_unique_ranks() >= 5 {
                match (best, bits.get_highest_five(&RankOrder::AceIsHigh)) {
                    (Some(existing), Some(newest)) => {
                        for i in 0..5 {
                            match RankOrder::AceIsHigh.cmp(newest[i], existing[i]) {
                                std::cmp::Ordering::Greater => {
                                    best = Some(newest);
                                    break;
                                }
                                std::cmp::Ordering::Less => break,
                                std::cmp::Ordering::Equal => {}
                            }
                        }
                    }
                    (None, Some(newest)) => best = Some(newest),
                    (_, None) => {}
                }
            }
        }
        best
    }

    fn get_best_straight_flush(deck: &Deck) -> Option<Rank> {
        let mut found: Option<Rank> = None;
        for (single_suit_rank, _) in deck.get_single_suit_ranks() {
            match (
                Self::get_straight_from_rank_bitfield(&single_suit_rank),
                found,
            ) {
                (Some(straight), Some(found_val)) => {
                    if RankOrder::AceIsHigh.cmp(straight, found_val) == std::cmp::Ordering::Greater
                    {
                        found = Some(straight)
                    }
                }
                (Some(straight), None) => found = Some(straight),
                (None, _) => {}
            }
        }
        found
    }
}

#[cfg(test)]
mod test {

    use crate::{deck::Rank, ranking::hand_rank::StandardHandRanks};

    #[test]
    pub fn test_rank() {
        let high_card_lowest = StandardHandRanks::HighCard {
            c1: Rank::Seven,
            c2: Rank::Five,
            c3: Rank::Four,
            c4: Rank::Three,
            c5: Rank::Two,
        };
        let high_card_highest = StandardHandRanks::HighCard {
            c1: Rank::Ace,
            c2: Rank::King,
            c3: Rank::Queen,
            c4: Rank::Jack,
            c5: Rank::Nine,
        };

        let one_pair_lowest = StandardHandRanks::OnePair {
            p: Rank::Two,
            c1: Rank::Five,
            c2: Rank::Four,
            c3: Rank::Three,
        };

        let one_pair_highest = StandardHandRanks::OnePair {
            p: Rank::Ace,
            c1: Rank::King,
            c2: Rank::Queen,
            c3: Rank::Jack,
        };

        let two_pair_lowest = StandardHandRanks::TwoPair {
            p1: Rank::Three,
            p2: Rank::Two,
            c1: Rank::Four,
        };

        let two_pair_highest = StandardHandRanks::TwoPair {
            p1: Rank::Ace,
            p2: Rank::King,
            c1: Rank::Queen,
        };

        let trip_lowest = StandardHandRanks::ThreeOfAKind {
            t: Rank::Two,
            c1: Rank::Four,
            c2: Rank::Three,
        };

        let trip_highest = StandardHandRanks::ThreeOfAKind {
            t: Rank::Ace,
            c1: Rank::King,
            c2: Rank::Queen,
        };

        let straight_lowest = StandardHandRanks::Straight { s: Rank::Five };

        let straight_highest = StandardHandRanks::Straight { s: Rank::Ace };

        let flush_lowest = StandardHandRanks::Flush {
            c1: Rank::Seven,
            c2: Rank::Six,
            c3: Rank::Five,
            c4: Rank::Four,
            c5: Rank::Three,
        };

        let flush_highest = StandardHandRanks::Flush {
            c1: Rank::Ace,
            c2: Rank::King,
            c3: Rank::Queen,
            c4: Rank::Jack,
            c5: Rank::Nine,
        };

        let full_house_lowest = StandardHandRanks::FullHouse {
            t: Rank::Two,
            p: Rank::Three,
        };

        let full_house_highest = StandardHandRanks::FullHouse {
            t: Rank::Ace,
            p: Rank::King,
        };

        let quads_lowest = StandardHandRanks::FourOfAKind {
            q: Rank::Two,
            c: Rank::Three,
        };

        let quads_highest = StandardHandRanks::FourOfAKind {
            q: Rank::Ace,
            c: Rank::King,
        };

        let sf_lowest = StandardHandRanks::StraightFlush { sf: Rank::Five };
        let sf_highest = StandardHandRanks::StraightFlush { sf: Rank::Ace };

        let mut all_hands = vec![
            one_pair_highest,
            one_pair_lowest,
            two_pair_highest,
            two_pair_lowest,
            high_card_highest,
            high_card_lowest,
            trip_lowest,
            trip_highest,
            straight_highest,
            straight_lowest,
            flush_highest,
            flush_lowest,
            full_house_highest,
            full_house_lowest,
            quads_highest,
            quads_lowest,
            sf_lowest,
            sf_highest,
        ];

        all_hands.sort();

        assert_eq!(
            all_hands,
            [
                high_card_lowest,
                high_card_highest,
                one_pair_lowest,
                one_pair_highest,
                two_pair_lowest,
                two_pair_highest,
                trip_lowest,
                trip_highest,
                straight_lowest,
                straight_highest,
                flush_lowest,
                flush_highest,
                full_house_lowest,
                full_house_highest,
                quads_lowest,
                quads_highest,
                sf_lowest,
                sf_highest
            ]
        )
    }
}