Skip to main content

games/
slot_machine.rs

1//! A 3 Column slot machine with 7 possible characters per column
2//! ```
3//! let slots = games::slot_machine::SlotMachine::new();
4//! let mut picks = slots.picks();
5//! if picks[0] == picks[1] || picks[0] == picks[2] || picks[1] == picks[2] {
6//!     if picks[0] == picks[1] && picks[1] == picks[2] {
7//!         println!("You have 3 matching characters!")
8//!     } else {
9//!         println!("You have 2 matching characters!")
10//!     }
11//! } else {
12//!     println!("You have no matching characters")
13//! }
14
15use rand::RngExt;
16
17const ROW_LEN: usize = 7;
18const ROW: [char; ROW_LEN] = ['🍒', '🍊', '🍓', '🍍', '🍇', '🍉', '⭐'];
19
20/// Slot machine
21#[repr(C)]
22#[derive(
23    Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash,
24)]
25pub struct SlotMachine([char; 3]);
26
27impl SlotMachine {
28    /// Spin the slot machine
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    /// The 3 choices chosen at random by slot machine
34    pub fn picks(self) -> [char; 3] {
35        self.0
36    }
37}
38
39impl Default for SlotMachine {
40    fn default() -> Self {
41        let mut rng = crate::get_rng();
42        let mut picks = ['\0', '\0', '\0'];
43        picks[0] = ROW[rng.random_range(0..ROW_LEN)];
44        picks[1] = ROW[rng.random_range(0..ROW_LEN)];
45        picks[2] = ROW[rng.random_range(0..ROW_LEN)];
46
47        SlotMachine(picks)
48    }
49}