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
//! Three-card poker hand evaluation implementation.
//!
//! This module provides the evaluation methods for three-card poker hands,
//! including hand type detection, ranking, and comparison. Three-card poker
//! has a simpler hand hierarchy than five-card poker, with the following
//! hand types from best to worst:
//!
//! 1. Straight flush (12 possible hands)
//! 2. Three of a kind (13 possible hands)
//! 3. Straight (12 possible hands)
//! 4. Flush (274 possible hands)
//! 5. Pair (156 possible hands)
//! 6. High card (274 possible hands)
//!
//! Total possible three-card hands: 741
use std::{cmp, fmt};
use super::{hand_class::ThreeCardHandClass, lookup_table::constants::*};
use crate::{
Rank,
evaluate::{eval::Eval, poker_type::ThreeCard},
};
impl Eval<ThreeCard> {
/// The best possible three-card hand (ace-high straight flush).
pub const BEST: Self = Self::new(1, 1 << 12);
/// The worst possible three-card hand (three-high).
pub const WORST: Self = Self::new(WORST_HIGH_CARD, 1 << 3);
/// Returns `true` if this hand is better than the other hand.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let straight_flush: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
/// let pair: Vec<_> = cards!("As Ac 2h").try_collect().unwrap();
///
/// let sf_eval = evaluator.evaluate_three(straight_flush).unwrap();
/// let pair_eval = evaluator.evaluate_three(pair).unwrap();
///
/// assert!(sf_eval.is_better_than(pair_eval));
/// ```
pub const fn is_better_than(self, other: Self) -> bool { self.hand_rank() < other.hand_rank() }
/// Returns `true` if this hand is worse than the other hand.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let high_card: Vec<_> = cards!("Ah Kc 2h").try_collect().unwrap();
/// let pair: Vec<_> = cards!("As Ac 2h").try_collect().unwrap();
///
/// let hc_eval = evaluator.evaluate_three(high_card).unwrap();
/// let pair_eval = evaluator.evaluate_three(pair).unwrap();
///
/// assert!(hc_eval.is_worse_than(pair_eval));
/// ```
pub const fn is_worse_than(self, other: Self) -> bool { self.hand_rank() > other.hand_rank() }
/// Checks whether this evaluation would neither beat nor lose to another
/// evaluation. Functionally equivalent to `self == other` as [`Eval`]
/// implements [`Eq`].
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand1: Vec<_> = cards!("Ah Kc 2h").try_collect().unwrap();
/// let hand2: Vec<_> = cards!("Ad Ks 2s").try_collect().unwrap();
///
/// let eval1 = evaluator.evaluate_three(hand1).unwrap();
/// let eval2 = evaluator.evaluate_three(hand2).unwrap();
///
/// assert!(eval1.is_equal_to(eval2));
/// assert_eq!(eval1, eval2);
/// ```
pub const fn is_equal_to(self, other: Self) -> bool { self.hand_rank() == other.hand_rank() }
/// Explicitly classify this evaluation into an enum that is useful for
/// exhaustive pattern matching. See [`ThreeCardHandClass`] for more.
///
/// # Example
///
/// ```
/// use poker::{Evaluator, Rank, cards, evaluate::ThreeCardHandClass};
///
/// let ev = Evaluator::new();
/// let hand = cards!("Th Jh Qh").try_collect::<Vec<_>>().unwrap();
/// assert!(matches!(
/// ev.evaluate_three(hand).unwrap().classify(),
/// ThreeCardHandClass::StraightFlush { rank: Rank::Queen },
/// ));
/// ```
pub const fn classify(self) -> ThreeCardHandClass {
if let Some(rank) = self.straight_flush() {
ThreeCardHandClass::StraightFlush { rank }
} else if let Some(rank) = self.three_of_a_kind() {
ThreeCardHandClass::ThreeOfAKind { rank }
} else if let Some(rank) = self.straight() {
ThreeCardHandClass::Straight { rank }
} else if let Some(rank) = self.flush() {
ThreeCardHandClass::Flush { rank }
} else if let Some(rank) = self.pair() {
ThreeCardHandClass::Pair { rank }
} else if let Some(rank) = self.high_card() {
ThreeCardHandClass::HighCard { rank }
} else {
unreachable!();
}
}
/// Returns `true` if this hand is a high card (no pairs, straights,
/// flushes, etc.).
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_high_card());
/// ```
pub const fn is_high_card(self) -> bool {
const BEST: i16 = WORST_PAIR + 1;
const WORST: i16 = WORST_HIGH_CARD;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the highest card rank if this is a high card hand.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.high_card(), Some(Rank::Ace));
/// ```
pub const fn high_card(self) -> Option<Rank> {
if self.is_high_card() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
/// Returns `true` if this hand contains exactly one pair.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_pair());
/// ```
pub const fn is_pair(self) -> bool {
const BEST: i16 = WORST_FLUSH + 1;
const WORST: i16 = WORST_PAIR;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the rank of the pair if this hand contains a pair.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.pair(), Some(Rank::Ace));
/// ```
pub const fn pair(self) -> Option<Rank> {
if self.is_pair() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
/// Returns `true` if this hand is a flush (all three cards of the same
/// suit, but not a straight flush).
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_flush());
/// ```
pub const fn is_flush(self) -> bool {
const BEST: i16 = WORST_STRAIGHT + 1;
const WORST: i16 = WORST_FLUSH;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the highest card rank if this hand is a flush.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.flush(), Some(Rank::Ace));
/// ```
pub const fn flush(self) -> Option<Rank> {
if self.is_flush() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
/// Returns `true` if this hand is a straight (three consecutive ranks, but
/// not all the same suit).
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_straight());
/// ```
pub const fn is_straight(self) -> bool {
const BEST: i16 = WORST_THREE_OF_A_KIND + 1;
const WORST: i16 = WORST_STRAIGHT;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the highest card rank if this hand is a straight.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.straight(), Some(Rank::Ace));
/// ```
pub const fn straight(self) -> Option<Rank> {
if self.is_straight() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
/// Returns `true` if this hand contains three cards of the same rank.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_three_of_a_kind());
/// ```
pub const fn is_three_of_a_kind(self) -> bool {
const BEST: i16 = WORST_STRAIGHT_FLUSH + 1;
const WORST: i16 = WORST_THREE_OF_A_KIND;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the rank of the three of a kind if this hand contains three of a
/// kind.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.three_of_a_kind(), Some(Rank::Ace));
/// ```
pub const fn three_of_a_kind(self) -> Option<Rank> {
if self.is_three_of_a_kind() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
/// Returns `true` if this hand is a straight flush (three consecutive
/// ranks, all the same suit).
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert!(eval.is_straight_flush());
/// ```
pub const fn is_straight_flush(self) -> bool {
const BEST: i16 = 1;
const WORST: i16 = WORST_STRAIGHT_FLUSH;
matches!(self.hand_rank(), BEST..=WORST)
}
/// Returns the highest card rank if this hand is a straight flush.
///
/// # Examples
///
/// ```
/// use poker::{Evaluator, Rank, cards};
///
/// let evaluator = Evaluator::new();
/// let hand: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
/// let eval = evaluator.evaluate_three(hand).unwrap();
///
/// assert_eq!(eval.straight_flush(), Some(Rank::Ace));
/// ```
pub const fn straight_flush(self) -> Option<Rank> {
if self.is_straight_flush() {
let flags = self.rank_flags();
let mut i = 0;
while flags & (1 << i) == 0 {
i += 1;
}
Some(Rank::ALL_VARIANTS[i])
} else {
None
}
}
}
impl fmt::Display for Eval<ThreeCard> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(rank) = self.straight_flush() {
f.write_fmt(format_args!("{}-high straight flush", rank.as_str_name()))
} else if let Some(rank) = self.three_of_a_kind() {
f.write_fmt(format_args!(
"three of a kind, {}",
rank.as_str_name_plural()
))
} else if let Some(rank) = self.straight() {
f.write_fmt(format_args!("{}-high straight", rank.as_str_name()))
} else if let Some(rank) = self.flush() {
f.write_fmt(format_args!("{}-high flush", rank.as_str_name()))
} else if let Some(rank) = self.pair() {
f.write_fmt(format_args!("pair, {}", rank.as_str_name_plural()))
} else if let Some(rank) = self.high_card() {
f.write_fmt(format_args!("high card, {}", rank.as_str_name()))
} else {
unreachable!()
}
}
}
impl cmp::Ord for Eval<ThreeCard> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
let s = self.hand_rank();
let o = other.hand_rank();
if s < o {
cmp::Ordering::Greater
} else if s > o {
cmp::Ordering::Less
} else {
cmp::Ordering::Equal
}
}
}
impl cmp::PartialOrd for Eval<ThreeCard> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}
#[cfg(test)]
mod tests {
use super::ThreeCardHandClass;
use crate::{Evaluator, Rank, cards};
#[test]
fn straight_flush_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_straight_flush());
assert_eq!(eval.straight_flush(), Some(Rank::Ace));
assert!(!eval.is_three_of_a_kind());
assert!(!eval.is_straight());
assert!(!eval.is_flush());
assert!(!eval.is_pair());
assert!(!eval.is_high_card());
}
#[test]
fn three_of_a_kind_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_three_of_a_kind());
assert_eq!(eval.three_of_a_kind(), Some(Rank::Ace));
assert!(!eval.is_straight_flush());
assert!(!eval.is_straight());
assert!(!eval.is_flush());
assert!(!eval.is_pair());
assert!(!eval.is_high_card());
}
#[test]
fn straight_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_straight());
assert_eq!(eval.straight(), Some(Rank::Ace));
assert!(!eval.is_straight_flush());
assert!(!eval.is_three_of_a_kind());
assert!(!eval.is_flush());
assert!(!eval.is_pair());
assert!(!eval.is_high_card());
}
#[test]
fn flush_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_flush());
assert_eq!(eval.flush(), Some(Rank::Ace));
assert!(!eval.is_straight_flush());
assert!(!eval.is_three_of_a_kind());
assert!(!eval.is_straight());
assert!(!eval.is_pair());
assert!(!eval.is_high_card());
}
#[test]
fn pair_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_pair());
assert_eq!(eval.pair(), Some(Rank::Ace));
assert!(!eval.is_straight_flush());
assert!(!eval.is_three_of_a_kind());
assert!(!eval.is_straight());
assert!(!eval.is_flush());
assert!(!eval.is_high_card());
}
#[test]
fn high_card_detection() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(eval.is_high_card());
assert_eq!(eval.high_card(), Some(Rank::Ace));
assert!(!eval.is_straight_flush());
assert!(!eval.is_three_of_a_kind());
assert!(!eval.is_straight());
assert!(!eval.is_flush());
assert!(!eval.is_pair());
}
#[test]
fn hand_comparison() {
let evaluator = Evaluator::new();
let straight_flush: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
let three_of_a_kind: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
let straight: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
let flush: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
let pair: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
let high_card: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
let sf_eval = evaluator.evaluate_three(straight_flush).unwrap();
let trip_eval = evaluator.evaluate_three(three_of_a_kind).unwrap();
let str_eval = evaluator.evaluate_three(straight).unwrap();
let flush_eval = evaluator.evaluate_three(flush).unwrap();
let pair_eval = evaluator.evaluate_three(pair).unwrap();
let hc_eval = evaluator.evaluate_three(high_card).unwrap();
// Test all comparisons
assert!(sf_eval.is_better_than(trip_eval));
assert!(trip_eval.is_better_than(str_eval));
assert!(str_eval.is_better_than(flush_eval));
assert!(flush_eval.is_better_than(pair_eval));
assert!(pair_eval.is_better_than(hc_eval));
// Test reverse comparisons
assert!(hc_eval.is_worse_than(pair_eval));
assert!(pair_eval.is_worse_than(flush_eval));
assert!(flush_eval.is_worse_than(str_eval));
assert!(str_eval.is_worse_than(trip_eval));
assert!(trip_eval.is_worse_than(sf_eval));
}
#[test]
fn display_format() {
let evaluator = Evaluator::new();
let straight_flush: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
let sf_eval = evaluator.evaluate_three(straight_flush).unwrap();
assert_eq!(format!("{}", sf_eval), "ace-high straight flush");
let three_of_a_kind: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
let trip_eval = evaluator.evaluate_three(three_of_a_kind).unwrap();
assert_eq!(format!("{}", trip_eval), "three of a kind, aces");
let straight: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
let str_eval = evaluator.evaluate_three(straight).unwrap();
assert_eq!(format!("{}", str_eval), "ace-high straight");
let flush: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
let flush_eval = evaluator.evaluate_three(flush).unwrap();
assert_eq!(format!("{}", flush_eval), "ace-high flush");
let pair: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
let pair_eval = evaluator.evaluate_three(pair).unwrap();
assert_eq!(format!("{}", pair_eval), "pair, aces");
let high_card: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
let hc_eval = evaluator.evaluate_three(high_card).unwrap();
assert_eq!(format!("{}", hc_eval), "high card, ace");
}
#[test]
fn best_and_worst_constants() {
let evaluator = Evaluator::new();
// Best possible hand: A-K-Q straight flush
let best_hand: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
let best_eval = evaluator.evaluate_three(best_hand).unwrap();
// Worst possible hand: 4-3-2 high card
let worst_hand: Vec<_> = cards!("4h 3c 2d").try_collect().unwrap();
let worst_eval = evaluator.evaluate_three(worst_hand).unwrap();
assert!(best_eval.is_better_than(worst_eval));
assert!(worst_eval.is_worse_than(best_eval));
}
#[test]
fn low_straight_detection() {
let evaluator = Evaluator::new();
// Test A-2-3 straight (wheel)
let wheel: Vec<_> = cards!("Ah 2c 3d").try_collect().unwrap();
let wheel_eval = evaluator.evaluate_three(wheel).unwrap();
assert!(wheel_eval.is_straight());
assert_eq!(wheel_eval.straight(), Some(Rank::Three));
}
#[test]
fn classify_straight_flush() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Th Jh Qh").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::StraightFlush { rank: Rank::Queen }
));
// Test ace-high straight flush
let ace_sf: Vec<_> = cards!("Ah Kh Qh").try_collect().unwrap();
let ace_eval = evaluator.evaluate_three(ace_sf).unwrap();
assert!(matches!(
ace_eval.classify(),
ThreeCardHandClass::StraightFlush { rank: Rank::Ace }
));
}
#[test]
fn classify_three_of_a_kind() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("As Ac Ad").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::ThreeOfAKind { rank: Rank::Ace }
));
}
#[test]
fn classify_straight() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kc Qd").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::Straight { rank: Rank::Ace }
));
// Test wheel straight (A-2-3)
let wheel: Vec<_> = cards!("Ah 2c 3d").try_collect().unwrap();
let wheel_eval = evaluator.evaluate_three(wheel).unwrap();
assert!(matches!(
wheel_eval.classify(),
ThreeCardHandClass::Straight { rank: Rank::Three }
));
}
#[test]
fn classify_flush() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kh 2h").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::Flush { rank: Rank::Ace }
));
}
#[test]
fn classify_pair() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("As Ac 2d").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::Pair { rank: Rank::Ace }
));
}
#[test]
fn classify_high_card() {
let evaluator = Evaluator::new();
let hand: Vec<_> = cards!("Ah Kc 2d").try_collect().unwrap();
let eval = evaluator.evaluate_three(hand).unwrap();
assert!(matches!(
eval.classify(),
ThreeCardHandClass::HighCard { rank: Rank::Ace }
));
}
#[test]
fn classify_exhaustive_pattern_matching() {
let evaluator = Evaluator::new();
let hands = vec![
(
cards!("Ah Kh Qh").try_collect::<Vec<_>>().unwrap(),
"straight flush",
),
(
cards!("As Ac Ad").try_collect::<Vec<_>>().unwrap(),
"three of a kind",
),
(
cards!("Ah Kc Qd").try_collect::<Vec<_>>().unwrap(),
"straight",
),
(cards!("Ah Kh 2h").try_collect::<Vec<_>>().unwrap(), "flush"),
(cards!("As Ac 2d").try_collect::<Vec<_>>().unwrap(), "pair"),
(
cards!("Ah Kc 2d").try_collect::<Vec<_>>().unwrap(),
"high card",
),
];
for (hand, expected) in hands {
let eval = evaluator.evaluate_three(hand).unwrap();
let description = match eval.classify() {
ThreeCardHandClass::StraightFlush { .. } => "straight flush",
ThreeCardHandClass::ThreeOfAKind { .. } => "three of a kind",
ThreeCardHandClass::Straight { .. } => "straight",
ThreeCardHandClass::Flush { .. } => "flush",
ThreeCardHandClass::Pair { .. } => "pair",
ThreeCardHandClass::HighCard { .. } => "high card",
};
assert_eq!(description, expected);
}
}
}