open_pql/base/board/
flop.rs

1use super::{mem, Card, Card64, From, Into, PQLCardCount, Rank, Rank16};
2
3#[derive(Clone, Copy, Debug, Into, From, Default, Eq, PartialEq)]
4pub struct Flop(pub Card, pub Card, pub Card);
5
6impl Flop {
7    pub(crate) fn count_by_rank(self, r: Rank) -> PQLCardCount {
8        u8::from(self.0.r == r)
9            + u8::from(self.1.r == r)
10            + u8::from(self.2.r == r)
11    }
12
13    pub(crate) fn sorted_ranks(self) -> (Rank, Rank, Rank) {
14        let (mut x, mut y, mut z) = (self.0.r, self.1.r, self.2.r);
15
16        if x < y {
17            mem::swap(&mut x, &mut y);
18        }
19
20        if y < z {
21            mem::swap(&mut y, &mut z);
22        }
23
24        if x < y {
25            mem::swap(&mut x, &mut y);
26        }
27
28        (x, y, z)
29    }
30}
31
32impl From<Flop> for Card64 {
33    fn from(flop: Flop) -> Self {
34        let mut c = Self::empty();
35
36        c.set(flop.0);
37        c.set(flop.1);
38        c.set(flop.2);
39
40        c
41    }
42}
43
44impl From<Flop> for Rank16 {
45    fn from(flop: Flop) -> Self {
46        let mut r = Self::empty();
47
48        r.set(flop.0.r);
49        r.set(flop.1.r);
50        r.set(flop.2.r);
51
52        r
53    }
54}
55
56impl From<[Card; 3]> for Flop {
57    fn from(a: [Card; 3]) -> Self {
58        Self(a[0], a[1], a[2])
59    }
60}
61
62impl From<Flop> for [Card; 3] {
63    fn from(flop: Flop) -> Self {
64        [flop.0, flop.1, flop.2]
65    }
66}
67
68#[cfg(any(test, feature = "benchmark"))]
69impl From<&[Card]> for Flop {
70    fn from(cs: &[Card]) -> Self {
71        Self(cs[0], cs[1], cs[2])
72    }
73}
74
75#[cfg(test)]
76pub mod tests {
77    use super::*;
78    use crate::*;
79
80    impl Arbitrary for Flop {
81        fn arbitrary(g: &mut quickcheck::Gen) -> Self {
82            let cards = CardN::<3>::arbitrary(g);
83
84            Self(cards[0], cards[1], cards[2])
85        }
86    }
87
88    #[quickcheck]
89    fn test_into_rank16_and_array(flop: Flop) {
90        let r: Rank16 = flop.into();
91        let arr: [_; 3] = flop.into();
92
93        assert!(r.contains_rank(flop.0.r));
94        assert!(r.contains_rank(flop.1.r));
95        assert!(r.contains_rank(flop.2.r));
96
97        assert_eq!(arr[0], flop.0);
98        assert_eq!(arr[1], flop.1);
99        assert_eq!(arr[2], flop.2);
100    }
101}