pkr 0.1.0

A library for evaluating poker hands
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
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
use std::error::Error;

use crate::card::{Card, Rank, Suit};

use super::evaluator::evaluator::evaluate;

// The minimum and maximum number of cards a hand can consist of.
const MIN_CARDS: usize = 2;
const MAX_CARDS: usize = 9;

/// Represents a poker hand.
///
/// A poker hand consists of `MIN_CARDS` to `MAX_CARDS` number of cards.
#[derive(Clone)]
pub struct Hand {
    cards: Vec<Card>,
}

impl Hand {
    /// Creates a new `Hand` from a vector of cards.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::card::Card;
    /// use pkr::hand::Hand;
    ///
    /// let cards = vec![
    ///     Card::new_from_str("Ah").unwrap(),
    ///     Card::new_from_str("Kh").unwrap(),
    ///     Card::new_from_str("Qh").unwrap(),
    ///     Card::new_from_str("Jh").unwrap(),
    ///     Card::new_from_str("Th").unwrap(),
    /// ];
    ///
    /// let hand = Hand::new(cards).unwrap();
    ///
    /// assert_eq!(hand.get_cards().len(), 5);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a `Box<dyn Error>` if the hand does not have between `MIN_CARDS`
    /// and `MAX_CARDS` number of cards.
    pub fn new(cards: Vec<Card>) -> Result<Hand, Box<dyn Error>> {
        let num_cards = cards.len();
        if num_cards < MIN_CARDS || num_cards > MAX_CARDS {
            return Err(format!(
                "A poker hand must have between {} and {} cards.",
                MIN_CARDS, MAX_CARDS
            )
            .into());
        }

        Ok(Hand { cards })
    }

    /// Creates a new `Hand` from a string.
    ///
    /// # Arguments
    ///
    /// * `s` - A string slice that holds the card identifiers.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::hand::Hand;
    ///
    /// let hand = Hand::new_from_str("As Ks Qs Js Ts").unwrap();
    /// assert_eq!(hand.get_cards().len(), 5);
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a `Box<dyn Error>` if the string does not represent a valid hand
    /// the hand does not have between `MIN_CARDS` and `MAX_CARDS` number of cards.
    pub fn new_from_str(s: &str) -> Result<Self, Box<dyn Error>> {
        let strings: Vec<&str> = s.split_whitespace().collect();
        if strings.len() < MIN_CARDS || strings.len() > MAX_CARDS {
            return Err(format!(
                "A poker hand must have between {} and {} cards.",
                MIN_CARDS, MAX_CARDS
            )
            .into());
        }
        let mut cards = Vec::new();
        for s in strings {
            let card = Card::new_from_str(s).map_err(|_| format!("Invalid card string: {}", s))?;
            cards.push(card);
        }
        Ok(Hand { cards })
    }

    /// Adds a single card to the hand.
    ///
    /// # Arguments
    ///
    /// * `new_card` - A card to be added to the hand.
    ///
    /// # Errors
    ///
    /// Returns a `Box<dyn Error>` if adding the card would result in more than 7 cards in the hand.
    pub fn add_card(&mut self, new_card: Card) -> Result<(), Box<dyn Error>> {
        if self.cards.len() + 1 > MAX_CARDS {
            return Err("Too many cards in the hand.".into());
        }
        self.cards.push(new_card);
        Ok(())
    }

    /// Adds multiple cards to the hand.
    ///
    /// # Arguments
    ///
    /// * `new_cards` - A vector of cards to be added to the hand.
    ///
    /// # Errors
    ///
    /// Returns a `Box<dyn Error>` if adding the cards would result in more than 7 cards in the hand.
    pub fn add_cards(&mut self, new_cards: Vec<Card>) -> Result<(), Box<dyn Error>> {
        if self.cards.len() + new_cards.len() > MAX_CARDS {
            return Err("Too many cards to add.".into());
        }
        for card in new_cards {
            self.cards.push(card);
        }
        Ok(())
    }

    /// Returns a reference to the cards in the hand.
    pub fn get_cards(&self) -> &Vec<Card> {
        &self.cards
    }

    /// Returns the number of cards in the hand.
    pub fn get_count(&self) -> usize {
        self.cards.len()
    }

    /// Returns the score of a Hand instance by calling the `evaluate` function.
    /// The score makes hands comparable by strength.
    ///
    /// The hand's score is used to rank the hand in comparison with other
    /// poker hands. A higher score represents a stronger hand. This is useful
    /// in games of poker where the strength of a player's hand needs to be
    /// compared to the hands of others.
    ///
    /// # Returns
    ///
    /// * `u32` - An unsigned 32-bit integer representing the score of the hand.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::hand::Hand;
    ///
    /// let hand1 = Hand::new_from_str("Ts Js Qs Ks As").unwrap();
    /// assert_eq!(hand1.get_score(), 8000014);
    ///
    /// let hand2 = Hand::new_from_str("As Ah Ac Ad Ks").unwrap();
    /// assert_eq!(hand2.get_score(), 7000237);
    ///
    /// assert!(hand1.get_score() > hand2.get_score());
    /// ```
    pub fn get_score(&self) -> u32 {
        evaluate(self)
    }

    /// Returns the ranks of all cards in the hand, ignoring the suits.
    ///
    /// This can be useful when only the ranks of the cards matter for a certain
    /// operation or comparison, and the suits are irrelevant.
    ///
    /// # Returns
    ///
    /// A Vec of Rank representing the ranks of all cards in the hand.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::card::{Card, Rank, Suit};
    /// use pkr::hand::Hand;
    ///
    /// let hand = Hand::new(vec![
    ///     Card::new(Rank::Ace, Suit::Heart),
    ///     Card::new(Rank::Two, Suit::Spade),
    ///     Card::new(Rank::Four, Suit::Diamond),
    ///     Card::new(Rank::Five, Suit::Heart),
    ///     Card::new(Rank::Three, Suit::Heart),
    /// ]).unwrap();
    ///
    /// let ranks = hand.get_ranks();
    /// assert_eq!(ranks, vec![Rank::Ace, Rank::Two, Rank::Four, Rank::Five, Rank::Three]);
    /// ```
    pub fn get_ranks(&self) -> Vec<Rank> {
        self.cards.iter().map(|card| card.rank).collect()
    }

    /// Returns a string representation of the `Hand`.
    ///
    /// The string consists of card identifiers separated by spaces. Each card
    /// identifier consists of two characters: the rank and the suit. For
    /// example, the ace of clubs is represented as "Ac".
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::hand::Hand;
    /// use pkr::card::{Card, Rank, Suit};
    ///
    /// let hand = Hand::new(vec![
    ///     Card { rank: Rank::Ace, suit: Suit::Club },
    ///     Card { rank: Rank::King, suit: Suit::Spade },
    ///     Card { rank: Rank::Queen, suit: Suit::Heart },
    ///     Card { rank: Rank::Jack, suit: Suit::Diamond },
    ///     Card { rank: Rank::Ten, suit: Suit::Club },
    /// ]).unwrap();
    ///
    /// assert_eq!(hand.as_str(), "Ac Ks Qh Jd Tc");
    /// ```
    pub fn as_str(&self) -> String {
        self.cards
            .iter()
            .map(|card| card.as_str())
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Sorts the cards in the hand by suit in ascending order.
    ///
    /// The relative order of cards with the same suit is maintained.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::hand::Hand;
    /// use pkr::card::{Card, Rank, Suit};
    ///
    /// let mut hand = Hand::new(vec![
    ///     Card { rank: Rank::Ace, suit: Suit::Heart },
    ///     Card { rank: Rank::King, suit: Suit::Club },
    ///     Card { rank: Rank::Queen, suit: Suit::Spade },
    ///     Card { rank: Rank::Jack, suit: Suit::Diamond },
    ///     Card { rank: Rank::Ten, suit: Suit::Heart },
    /// ]).unwrap();
    ///
    /// hand.sort_by_suit();
    ///
    /// assert_eq!(hand.as_str(), "Kc Jd Ah Th Qs");
    /// ```
    pub fn sort_by_suit(&mut self) {
        self.cards
            .sort_by(|a, b| a.suit.partial_cmp(&b.suit).unwrap());
    }

    /// Sorts the hand by rank, preserving the original order within each rank.
    ///
    /// # Arguments
    ///
    /// * `ascending` - A boolean indicating if sorting should be in ascending
    ///                 order (true) or descending order (false).
    ///
    /// # Errors
    ///
    /// Returns a `Box<dyn Error>` if the ranks cannot be compared.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::card::{Card, Rank, Suit};
    /// use pkr::hand::Hand;
    ///
    /// let mut hand = Hand::new_from_str("Ah 2s 4d 5h 3h").unwrap();
    /// hand.sort_by_rank(true).unwrap();
    /// assert_eq!(hand.as_str(), "2s 3h 4d 5h Ah");
    ///
    /// hand.sort_by_rank(false).unwrap();
    /// assert_eq!(hand.as_str(), "Ah 5h 4d 3h 2s");
    /// ```
    pub fn sort_by_rank(&mut self, ascending: bool) -> Result<(), Box<dyn Error>> {
        if ascending {
            self.cards
                .sort_by(|a, b| a.rank.partial_cmp(&b.rank).unwrap());
        } else {
            self.cards
                .sort_by(|a, b| b.rank.partial_cmp(&a.rank).unwrap());
        }
        Ok(())
    }

    /// Returns all cards in the hand of a given suit.
    ///
    /// # Arguments
    ///
    /// * `suit` - A suit of which the cards are to be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use pkr::hand::Hand;
    /// use pkr::card::{Card, Rank, Suit};
    ///
    /// let hand = Hand::new(vec![
    ///     Card { rank: Rank::Two, suit: Suit::Heart },
    ///     Card { rank: Rank::Three, suit: Suit::Heart },
    ///     Card { rank: Rank::Four, suit: Suit::Spade },
    ///     Card { rank: Rank::Five, suit: Suit::Diamond },
    ///     Card { rank: Rank::Six, suit: Suit::Heart },
    /// ]).unwrap();
    ///
    /// let hearts = hand.cards_of_suit(Suit::Heart);
    /// assert_eq!(hearts.len(), 3);
    /// ```
    pub fn cards_of_suit(&self, suit: Suit) -> Vec<Card> {
        self.cards
            .iter()
            .filter(|&card| card.suit == suit)
            .cloned()
            .collect()
    }
}

#[test]
fn test_create_hand() {
    let cards = vec![
        Card::new_from_str("2h").unwrap(),
        Card::new_from_str("3d").unwrap(),
        Card::new_from_str("4s").unwrap(),
        Card::new_from_str("5c").unwrap(),
        Card::new_from_str("6h").unwrap(),
        Card::new_from_str("7d").unwrap(),
        Card::new_from_str("8s").unwrap(),
    ];

    let hand = Hand::new(cards);

    assert!(hand.is_ok());

    let hand = hand.unwrap();
    assert_eq!(hand.get_cards().len(), 7)
}

#[test]
fn test_create_hand_with_wrong_number_of_cards() {
    let cards = vec![Card::new_from_str("3d").unwrap()];

    let result = Hand::new(cards);
    assert!(result.is_err());
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_straight_flushes() {
        let hand = Hand::new_from_str("2s As Js Ks Qs 9c Ts").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 14);

        let hand = Hand::new_from_str("2s Kc Js Ks Qs 9s Ts").unwrap();
        let score = hand.get_score();

        assert_eq!(score, 8_000_000 + 13);

        let hand = Hand::new_from_str("9h 8h Jc Tc Qh Jh Th").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 12);

        let hand = Hand::new_from_str("2s 7s Js 9s 8s 9c Ts").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 11);

        let hand = Hand::new_from_str("9d 8d Td 7d 6d 3c Th Kh Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 10);

        let hand = Hand::new_from_str("9d 8d 5d 6d 7d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 9);

        let hand = Hand::new_from_str("4c 5c 6c 7c 8c 3c 2c").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 8);

        let hand = Hand::new_from_str("7d 7c 7s 6d 5d 3d 4d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 7);

        let hand = Hand::new_from_str("6d 5d 4d 3d 2d Ad").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 6);

        let hand = Hand::new_from_str("2d Ad 3d 4d 5d 3c Th").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 8_000_000 + 5);
    }

    #[test]
    fn test_four_of_a_kind() {
        let hand = Hand::new_from_str("As Ac Ad Ah Ts 9c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 7_000_000 + (14 << 4) + 12);

        let hand = Hand::new_from_str("As Ac Ad Ah").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 7_000_000 + 14);

        let hand = Hand::new_from_str("9c Ks Kc Kd Kh Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 7_000_000 + (13 << 4) + 10);

        let hand = Hand::new_from_str("Qs Qc Qd Qh 8s 9c 9s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 7_000_000 + (12 << 4) + 9);

        let hand = Hand::new_from_str("2s 2c 2d 2h As 9c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 7_000_000 + (2 << 4) + 14);
    }

    #[test]
    fn test_full_house() {
        let hand = Hand::new_from_str("As Ac Ad Kh Ts Kc Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 6_000_000 + (14 << 4) + 13);

        let hand = Hand::new_from_str("Ks Qc Kd Kh Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 6_000_000 + (13 << 4) + 12);

        let hand = Hand::new_from_str("Tc 9s 9c Td 9h Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 6_000_000 + (10 << 4) + 9);

        let hand = Hand::new_from_str("4s 4c 4d 5h 5s 9c 9s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 6_000_000 + (4 << 4) + 9);

        let hand = Hand::new_from_str("2s 2c 2d 3h As 3c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 6_000_000 + (2 << 4) + 3);
    }

    #[test]
    fn test_flush() {
        let hand = Hand::new_from_str("As Ks Qs Js 9s 8s 7s").unwrap();
        let score = hand.get_score();
        assert_eq!(
            score,
            5_000_000 + (14 << 16) + (13 << 12) + (12 << 8) + (11 << 4) + 9
        );

        // Check corner case vs. lowest full house
        let hand = Hand::new_from_str("2s 2c 2d 3h As 3c Qs").unwrap();
        let score_low_fh = hand.get_score();
        assert!(score < score_low_fh);

        let hand = Hand::new_from_str("Ks Kd Qs Js 9s 9d 7s").unwrap();
        let score = hand.get_score();
        assert_eq!(
            score,
            5_000_000 + (13 << 16) + (12 << 12) + (11 << 8) + (9 << 4) + 7
        );

        let hand = Hand::new_from_str("Qs Js 9s 8s 7s").unwrap();
        let score = hand.get_score();
        assert_eq!(
            score,
            5_000_000 + (12 << 16) + (11 << 12) + (9 << 8) + (8 << 4) + 7
        );

        let hand = Hand::new_from_str("7s Kd Qd 4s 5s 3s 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(
            score,
            5_000_000 + (7 << 16) + (5 << 12) + (4 << 8) + (3 << 4) + 2
        );
    }

    #[test]
    fn test_straight() {
        let hand = Hand::new_from_str("2d Ac Js Ks Qs 9c Ts").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 14);

        let hand = Hand::new_from_str("2s Kc Jh Kd Qs 9s Ts").unwrap();
        let score = hand.get_score();

        assert_eq!(score, 4_000_000 + 13);

        let hand = Hand::new_from_str("9c 8h Jc Tc Qs Jh Th").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 12);

        let hand = Hand::new_from_str("2c 7c Js 9s 8h 9c Ts").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 11);

        let hand = Hand::new_from_str("9h 8d Ts 7d 6c 3c Th Kh Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 10);

        let hand = Hand::new_from_str("9c 8h 5d 6d 7d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 9);

        let hand = Hand::new_from_str("4c 5d 6c 7h 8c 3d 2c").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 8);

        let hand = Hand::new_from_str("7d 7c 7s 6d 5c 3d 4d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 7);

        let hand = Hand::new_from_str("6d 5d 4d 3c 2d Ac").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 6);

        let hand = Hand::new_from_str("2d Ac 3d 4d 5d 3c Th").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 4_000_000 + 5);
    }

    #[test]
    fn test_three_of_a_kind() {
        let hand = Hand::new_from_str("2s Ac Ad Ah Ts 9c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + (14 << 8) + (12 << 4) + 10);

        let hand = Hand::new_from_str("As Ac Ad 2h").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + (14 << 4) + 2);

        let hand = Hand::new_from_str("As Ac Ad").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + 14);

        let hand = Hand::new_from_str("9c Ks Kc Kd Ah Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + (13 << 8) + (14 << 4) + 10);

        let hand = Hand::new_from_str("9c 3s 2c 2d Kh Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + (2 << 8) + (13 << 4) + 10);

        let hand = Hand::new_from_str("2s 2c 2d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 3_000_000 + 2);
    }

    #[test]
    fn test_two_pair() {
        let hand = Hand::new_from_str("Ks Ac Ad Kh Ts 2c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (14 << 8) + (13 << 4) + 12);

        let hand = Hand::new_from_str("Ks Qc Kd Ah Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (13 << 8) + (12 << 4) + 14);

        let hand = Hand::new_from_str("Ks Qc Kd Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (13 << 4) + 12);

        let hand = Hand::new_from_str("Tc 8s 9c 8d 9h Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (10 << 8) + (9 << 4) + 8);

        let hand = Hand::new_from_str("4s 4c 2d 5h 5s 9c 9s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (9 << 8) + (5 << 4) + 4);

        let hand = Hand::new_from_str("2s 2c 3h 3c").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 2_000_000 + (3 << 4) + 2);
    }

    #[test]
    fn test_pair() {
        let hand = Hand::new_from_str("Ks Ac Ad 9h Js 2c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (14 << 12) + (13 << 8) + (12 << 4) + 11);

        let hand = Hand::new_from_str("Ks 2c Kd Ah Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (13 << 12) + (14 << 8) + (12 << 4) + 2);

        let hand = Hand::new_from_str("Ks 2c Kd Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (13 << 8) + (12 << 4) + 2);

        let hand = Hand::new_from_str("Ks 2c Kd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (13 << 4) + 2);

        let hand = Hand::new_from_str("Ks Kd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + 13);

        let hand = Hand::new_from_str("Tc 3s 5c 8d 9h Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (10 << 12) + (9 << 8) + (8 << 4) + 5);

        let hand = Hand::new_from_str("4s 4c 2d 3h 5s 9c Ts").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (4 << 12) + (10 << 8) + (9 << 4) + 5);

        let hand = Hand::new_from_str("2s 2c Ah Kc").unwrap();
        let score = hand.get_score();
        assert_eq!(score, 1_000_000 + (2 << 8) + (14 << 4) + 13);
    }

    #[test]
    fn test_high_card() {
        let hand = Hand::new_from_str("Ks Ac 9d 8h Js 2c Qs").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (14 << 16) + (13 << 12) + (12 << 8) + (11 << 4) + 9);

        let hand = Hand::new_from_str("Ks 2c Jd Ah Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (14 << 16) + (13 << 12) + (12 << 8) + (11 << 4) + 2);

        let hand = Hand::new_from_str("Ks 2c 3d Qd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (13 << 12) + (12 << 8) + (3 << 4) + 2);

        let hand = Hand::new_from_str("Ks 2c 4d").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (13 << 8) + (4 << 4) + 2);

        let hand = Hand::new_from_str("As Kd").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (14 << 4) + 13);

        let hand = Hand::new_from_str("Ac 3s 5c 8d 9h Ts 2s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (14 << 16) + (10 << 12) + (9 << 8) + (8 << 4) + 5);

        let hand = Hand::new_from_str("7s 4c 2d 3h 5s 8c 9s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (9 << 16) + (8 << 12) + (7 << 8) + (5 << 4) + 4);

        let hand = Hand::new_from_str("2s 3c 4h 5c 7s").unwrap();
        let score = hand.get_score();
        assert_eq!(score, (7 << 16) + (5 << 12) + (4 << 8) + (3 << 4) + 2);
    }
    #[test]
    fn test_corner_cases() {
        let hand1 = Hand::new_from_str("2d Ad 3d 4d 5d").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 8_000_000 + 5);

        let hand2 = Hand::new_from_str("As Ac Ad Ah Ks").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 7_000_000 + (14 << 4) + 13);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2s 2c 2d 2h").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 7_000_000 + 2);

        let hand2 = Hand::new_from_str("As Ac Ad Kh Kc").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 6_000_000 + (14 << 4) + 13);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2s 2c 2d 3h 3c").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 6_000_000 + (2 << 4) + 3);

        let hand2 = Hand::new_from_str("As Ks Qs Js 9s").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(
            score2,
            5_000_000 + (14 << 16) + (13 << 12) + (12 << 8) + (11 << 4) + 9
        );

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("7s 4s 5s 3s 2s").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(
            score1,
            5_000_000 + (7 << 16) + (5 << 12) + (4 << 8) + (3 << 4) + 2
        );

        let hand2 = Hand::new_from_str("Ac Js Ks Qs Ts").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 4_000_000 + 14);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2d Ac 3d 4d 5d").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 4_000_000 + 5);

        let hand2 = Hand::new_from_str("Ks Ac Ad Ah Qs").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 3_000_000 + (14 << 8) + (13 << 4) + 12);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2s 2c 2d").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 3_000_000 + 2);

        let hand2 = Hand::new_from_str("Ks Ac Ad Kh Qs").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 2_000_000 + (14 << 8) + (13 << 4) + 12);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2s 2c 3h 3c").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 2_000_000 + (3 << 4) + 2);

        let hand2 = Hand::new_from_str("Ks Ac Ad Js Qs").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, 1_000_000 + (14 << 12) + (13 << 8) + (12 << 4) + 11);

        assert!(score1 > score2);

        let hand1 = Hand::new_from_str("2s 2c").unwrap();
        let score1 = hand1.get_score();
        assert_eq!(score1, 1_000_000 + 2);

        let hand2 = Hand::new_from_str("Ks Ac 9d Js Qs").unwrap();
        let score2 = hand2.get_score();
        assert_eq!(score2, (14 << 16) + (13 << 12) + (12 << 8) + (11 << 4) + 9);

        assert!(score1 > score2);
    }
}