summaryrefslogtreecommitdiff
path: root/07/src/stuff_joker.rs
blob: 47f535f90b899539da1fb1ddc96e7a1ea56209e0 (plain)
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
pub const NUM_CARDS: u8 = 13;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Card {
    A, K, Q, J, T,
    Nine, Eight, Seven, Six, Five, Four, Three, Two
}

impl Card {
    fn key(self: & Self) -> usize {
        match self {
            Self::Two => 0,
            Self::Three => 1,
            Self::Four => 2,
            Self::Five => 3,
            Self::Six => 4,
            Self::Seven => 5,
            Self::Eight => 6,
            Self::Nine => 7,
            Self::T => 8,
            Self::J => 9,
            Self::Q => 10,
            Self::K => 11,
            Self::A => 12
        }
    }
    
    pub fn value(self: & Self) -> u8 {
        match self {
            Self::Two => 2,
            Self::Three => 3,
            Self::Four => 4,
            Self::Five => 5,
            Self::Six => 6,
            Self::Seven => 7,
            Self::Eight => 8,
            Self::Nine => 9,
            Self::T => 10,
            Self::J => 1,
            Self::Q => 11,
            Self::K => 12,
            Self::A => 13
        }
    }
}

impl TryFrom<u8> for Card {
    type Error = &'static str;

    fn try_from(c: u8) -> Result<Self, Self::Error> {
        match c {
            b'A' => Ok(Self::A),
            b'K' => Ok(Self::K),
            b'Q' => Ok(Self::Q),
            b'J' => Ok(Self::J),
            b'T' => Ok(Self::T),
            b'9' => Ok(Self::Nine),
            b'8' => Ok(Self::Eight),
            b'7' => Ok(Self::Seven),
            b'6' => Ok(Self::Six),
            b'5' => Ok(Self::Five),
            b'4' => Ok(Self::Four),
            b'3' => Ok(Self::Three),
            b'2' => Ok(Self::Two),
            _    => Err("ASCII character not a valid card")
        }
    }
}

impl PartialOrd for Card {
    fn partial_cmp(self: & Self, other: & Self) -> Option<core::cmp::Ordering> {
        self.value().partial_cmp(& other.value())
    }
}

impl Ord for Card {
    fn cmp(self: & Self, other: & Self) -> core::cmp::Ordering {
        self.value().cmp(& other.value())
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct Hand {
    cards: [Card; 5],
    histogram: [u8; NUM_CARDS as usize],
    num_jokers: u8
}

impl Hand {
    fn count(self: & Self, card: Card) -> u8 {
        if card == Card::J {
            self.num_jokers
        }
        else {
            self.histogram[card.key()]
        }  
    }
    
    pub fn new(cards: [Card; 5]) -> Self {
        let mut histogram: [u8; NUM_CARDS as usize] = [0; NUM_CARDS as usize];
        let mut num_jokers: u8 = 0;
        for card in cards.into_iter() {
            if card == Card::J {
                num_jokers += 1;
            }
            else {
                histogram[card.key()] += 1;
            }
        }
        Self { cards: cards, histogram: histogram, num_jokers: num_jokers }
    }

    pub fn is_n_of_a_kind(self: & Self, n: u8, count_jokers: bool) -> bool {
        let num_jokers = if count_jokers { self.count(Card::J) } else { 0 };
        self.histogram.into_iter().find(|& count| count + num_jokers == n).is_some()
    }

    pub fn is_full_house(self: & Self) -> bool {
        (self.is_n_of_a_kind(3, false) && self.is_n_of_a_kind(2, false))
            || (self.is_two_pairs() && self.count(Card::J) >= 1 )
    }

    pub fn is_two_pairs(self: & Self) -> bool {
        self.histogram.into_iter()
            .position(|count| count == 2)
            .and_then(|i| (& self.histogram[i+1..]).into_iter()
                      .position(|& count| count == 2))
            .is_some()
    }

    pub fn strength(self: & Self) -> u8 {
        if self.is_n_of_a_kind(5, true)      { 6 }
        else if self.is_n_of_a_kind(4, true) { 5 }
        else if self.is_full_house()         { 4 }
        else if self.is_n_of_a_kind(3, true) { 3 }
        else if self.is_two_pairs()          { 2 }
        else if self.is_n_of_a_kind(2, true) { 1 }
        else                                 { 0 }
    }
}

impl PartialOrd for Hand {
    fn partial_cmp(self: & Self, other: & Self) -> Option<core::cmp::Ordering> {
        let strength_ord = self.strength().cmp(& other.strength());
        if strength_ord == core::cmp::Ordering::Equal {
            Some(self.cards.cmp(& other.cards))
        }
        else {
            Some(strength_ord)
        }
   }
}

impl Ord for Hand {
    fn cmp(self: & Self, other: & Self) -> core::cmp::Ordering {
        let strength_ord = self.strength().cmp(& other.strength());
        if strength_ord == core::cmp::Ordering::Equal {
            self.cards.cmp(& other.cards)
        }
        else {
            strength_ord
        }
   }
}

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

    #[test]
    fn test_joker_1() {
        assert!(Hand::new([Card::T, Card::Five, Card::Five, Card::J, Card::Five]).is_n_of_a_kind(4, true));
    }

    #[test]
    fn test_joker_fh() {
        assert!(Hand::new([Card::T, Card::T, Card::Five, Card::Five, Card::J]).is_full_house());
    }
}