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
use crate::hand::Hand;
use crate::game_rules::DoublePolicy;

/// The game outcome of a played hand.
#[derive(PartialEq, Debug)]
pub enum HandOutcome {
    /// The hand is considered won when it either:
    ///   * is not busted and has a higher score than the dealer;
    ///   * is not busted and the dealer busted;
    ///   * is a blackjack against a non-blackjack for the dealer;
    /// If the hand was insured, whether the insurance bet won or lost has no
    /// impact on the fact the hand is considered won or not.
    Win,

    /// The hand is considered pushed when it either:
    ///   * is not busted, is not a blackjack and has the same score as a dealer
    ///     non-blackjack;
    ///   * is a blackjack against a dealer blackjack.
    /// If the hand was insured, whether the insurance bet won or lost has no
    /// impact on the fact the hand is considered pushed or not.
    Push,

    /// The hand lose, which includes surrendered hands and lost insured hands
    /// that won their insurance.
    Lose,
}

/// Determines the result and outcome of a played hand against a given dealer
///
/// The result is given as a normalized bet of 1.0, a simple win gives +1.0,
/// a simple loss gives -1.0, a won doubled-down hand +2.0, a lost doubled-down
/// hand a -2.0, a natural +1.5, a surrenderred hand gives -0.5. Insurance is
/// also taken into account, adding 1.0 to the result if the dealer received a
/// blackjack, substracting 0.5 if not.
pub fn hand_result(player: &Hand, dealer: &Hand) -> (HandOutcome, f64) {
    let (outcome, mut res) = if player.is_surrendered() {
        (HandOutcome::Lose, -0.5)
    } else if player.is_busted() {
        (HandOutcome::Lose, -1.0)
    } else {
        if player.is_bj() && !dealer.is_bj() {
            (HandOutcome::Win, 1.5)
        } else if dealer.is_busted() {
            (HandOutcome::Win, 1.0)
        } else {
            let player_val = player.value() + (if player.is_bj() {1} else {0});
            let dealer_val = dealer.value() + (if dealer.is_bj() {1} else {0});

            if player_val == dealer_val {
                (HandOutcome::Push, 0.0)
            } else if player_val > dealer_val {
                (HandOutcome::Win, 1.0)
            } else {
                (HandOutcome::Lose, -1.0)
            }
        }
    };

    if player.is_doubled() {
        res *= 2.0;
    }

    if player.is_insured() {
        if dealer.is_bj() {
            res += 1.0;
        } else {
            res -= 0.5;
        }
    }

    (outcome, res)
}

/// Determines whether a player hand can double-down based on the game policy.
///
/// # Parameters
///
///  * `policy` - The game policy in action concerning doubling-down;
///  * `das`    - Whether double-after-split (DAS) is allowed or not;
///  * `hand`   - The player's hand.
pub fn may_double(policy: DoublePolicy, das: bool, hand: &Hand) -> bool {
    if hand.is_split() && !das {
        return false;
    }

    match policy {
        DoublePolicy::AnyHand => true,
        DoublePolicy::AnyTwo => hand.count() == 2,
        DoublePolicy::Hard9To11 =>
            !hand.is_soft() && (9..=11).contains(&hand.value()),
        DoublePolicy::Hard10To11 =>
            !hand.is_soft() && (10..=11).contains(&hand.value()),
        DoublePolicy::NoDouble => false,
    }
}

#[cfg(test)]
mod tests {
    use crate::card::Card;
    use crate::hand_logic::{hand_result, may_double, HandOutcome};
    use crate::hand_logic::HandOutcome::*;
    use crate::hand::Hand;

    #[test]
    fn it_returns_the_hand_result() {
        test_hand_result(Push,  0.0, &[10, 10],   &[10, 10],      0);
        test_hand_result(Win,   1.0, &[10, 10],   &[10, 9],       0);
        test_hand_result(Lose, -1.0, &[10, 6],    &[10, 9],       0);

        test_hand_result(Push,  0.0, &[10, 10],   &[10, 10],      DOUBLED);
        test_hand_result(Win,   2.0, &[10, 10],   &[10, 9],       DOUBLED);
        test_hand_result(Lose, -2.0, &[10, 6],    &[10, 9],       DOUBLED);

        test_hand_result(Win,   1.5, &[10, 1],    &[7, 7, 7],     0);
        test_hand_result(Push,  0.0, &[10, 1],    &[1, 10],       0);
        test_hand_result(Lose, -1.0, &[7, 7, 7],  &[10, 1],       0);

        test_hand_result(Lose, -1.0, &[7, 7, 8],  &[10, 7],       0);
        test_hand_result(Lose, -1.0, &[7, 7, 8],  &[10, 6, 9],    0);
        test_hand_result(Win,   1.0, &[8, 8],     &[10, 6, 9],    0);

        test_hand_result(Lose, -0.5, &[8, 8],     &[10, 7],       SURRENDERED);
        test_hand_result(Lose, -0.5, &[8, 8],     &[10, 6, 9],    SURRENDERED);

        test_hand_result(Push,  0.0, &[1, 10],    &[7, 7, 7],     SPLIT);

        test_hand_result(Push,  1.0, &[1, 10],    &[1, 10],       INSURED);
        test_hand_result(Lose,  0.0, &[7, 7, 7],  &[1, 10],       INSURED);
        test_hand_result(Win,   0.5, &[7, 7, 7],  &[1, 9],        INSURED);
        test_hand_result(Lose, -1.5, &[10, 6, 7], &[1, 9],        INSURED);
    }

    #[test]
    fn it_determines_whether_it_can_double_down() {
        use crate::game_rules::DoublePolicy::*;

        assert!(may_double(AnyHand,     true,   &Hand::from(&[4, 7][..])));
        assert!(may_double(AnyHand,     false,  &Hand::from(&[4, 7][..])));
        assert!(may_double(AnyHand,     false,  &Hand::from(&[4, 3, 8][..])));
        let mut hand = Hand::from(&[4, 4][..]);
        hand.split();
        hand.add(Card(3));
        hand.add(Card(8));
        assert!(!may_double(AnyHand,    false,  &hand));
        assert!(may_double(AnyHand,     true,   &hand));

        assert!(may_double(AnyTwo,      true,   &Hand::from(&[4, 7][..])));
        assert!(may_double(AnyTwo,      false,  &Hand::from(&[4, 7][..])));
        assert!(may_double(AnyTwo,      false,  &Hand::from(&[1, 7][..])));
        assert!(!may_double(AnyTwo,     false,  &Hand::from(&[4, 3, 8][..])));

        assert!(!may_double(Hard9To11,  true,   &Hand::from(&[5, 3][..])));
        assert!(may_double(Hard9To11,   true,   &Hand::from(&[4, 5][..])));
        assert!(may_double(Hard9To11,   true,   &Hand::from(&[4, 6][..])));
        assert!(may_double(Hard9To11,   true,   &Hand::from(&[4, 7][..])));
        assert!(!may_double(Hard9To11,  true,   &Hand::from(&[4, 8][..])));
        assert!(!may_double(Hard9To11,  true,   &Hand::from(&[1, 2][..])));

        assert!(!may_double(Hard10To11, true,   &Hand::from(&[5, 3][..])));
        assert!(!may_double(Hard10To11, true,   &Hand::from(&[4, 5][..])));
        assert!(may_double(Hard10To11,  true,   &Hand::from(&[4, 6][..])));
        assert!(may_double(Hard10To11,  true,   &Hand::from(&[4, 7][..])));
        assert!(!may_double(Hard10To11, true,   &Hand::from(&[4, 8][..])));
        assert!(!may_double(Hard10To11, true,   &Hand::from(&[1, 2][..])));

        assert!(!may_double(NoDouble,   true,   &Hand::from(&[5, 3][..])));
        assert!(!may_double(NoDouble,   true,   &Hand::from(&[4, 5][..])));
        assert!(!may_double(NoDouble,   true,   &Hand::from(&[4, 6][..])));
        assert!(!may_double(NoDouble,   true,   &Hand::from(&[4, 7][..])));
        assert!(!may_double(NoDouble,   true,   &Hand::from(&[4, 8][..])));
        assert!(!may_double(NoDouble,   true,   &Hand::from(&[1, 2][..])));
    }

    const DOUBLED: u32      = 1 << 0;
    const SURRENDERED: u32  = 1 << 1;
    const INSURED: u32      = 1 << 2;
    const SPLIT: u32        = 1 << 3;

    fn test_hand_result(expected_outcome: HandOutcome,
                        expected_result: f64,
                        player_vals: &[u8],
                        dealer: &[u8],
                        opts: u32) {
        let mut player = Hand::from(player_vals);
        if opts & DOUBLED > 0 { player.double_down(); }
        if opts & SURRENDERED > 0 { player.surrender(); }
        if opts & INSURED > 0 { player.insure(); }
        if opts & SPLIT > 0 {
            player.split();
            player.add(Card(player_vals[1]));
        }
        let dealer = Hand::from(dealer);

        let (outcome, result) = hand_result(&player, &dealer);

        assert_eq!(outcome, expected_outcome, "\nplayer={player:?}\ndealer={dealer:?}");
        assert_eq!(result, expected_result);
    }
}