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
use crate::game::symbol::Symbol;
use crate::game::symbol::Symbol::*;
use crate::game::NUM_REELS;

/// Calculates payout.
///
/// # Panics
///
/// Panics if the number of elements in the `symbols` is not 3.
pub fn payout(symbols: &Vec<Symbol>) -> u32 {
    assert_eq!(
        symbols.len(),
        NUM_REELS,
        "`symbols` must contain {} symbols! Contains: {}",
        NUM_REELS,
        symbols.len()
    );

    if is_all(symbols, Jackpot) {
        return 1666;
    } else if is_all(symbols, Seven) {
        return 300;
    } else if is_all(symbols, TripleBar) {
        return 100;
    } else if is_all(symbols, DoubleBar) {
        return 50;
    } else if is_all(symbols, Bar) {
        return 25;
    } else if is_all(symbols, Cherry)
        || symbols
            .iter()
            .map(|x| x.to_string())
            .filter(|x| x.contains("Bar"))
            .count()
            == 3
    {
        return 12;
    } else if symbols.iter().filter(|x| x == &&Cherry).count() == 2 {
        return 6;
    } else if symbols.iter().filter(|x| x == &&Cherry).count() == 1 {
        return 3;
    }

    0
}

// Returns `true` if `vec` contains the same symbols.
fn is_all(vec: &Vec<Symbol>, expected: Symbol) -> bool {
    vec.iter().all(|x| x == &expected)
}

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

    #[test]
    fn test_payout() {
        assert_eq!(payout(&vec![Jackpot; 3]), 1666);
        assert_eq!(payout(&vec![Seven; 3]), 300);
        assert_eq!(payout(&vec![TripleBar; 3]), 100);
        assert_eq!(payout(&vec![DoubleBar; 3]), 50);
        assert_eq!(payout(&vec![Bar; 3]), 25);
        assert_eq!(payout(&vec![Cherry; 3]), 12);
        assert_eq!(payout(&vec![Bar, DoubleBar, TripleBar]), 12);
        assert_eq!(payout(&vec![Cherry, Cherry, Blank]), 6);
        assert_eq!(payout(&vec![Bar, Blank, Cherry]), 3);
        assert_eq!(payout(&vec![Bar, Blank, Seven]), 0);
    }

    #[test]
    #[should_panic]
    fn payout_vec_length_not_3() {
        payout(&vec![Bar, Blank, Blank, Bar]);
    }

    #[test]
    fn test_is_all() {
        let cherries = vec![Cherry, Cherry, Cherry];

        assert!(is_all(&cherries, Cherry));
    }
}