monster/path_exploration/
coin_flip.rs

1use super::ExplorationStrategy;
2use rand::{rngs::ThreadRng, thread_rng, Rng};
3use std::cell::RefCell;
4
5pub struct CoinFlipStrategy {
6    rng: RefCell<ThreadRng>,
7}
8
9impl CoinFlipStrategy {
10    pub fn new() -> Self {
11        Self {
12            rng: RefCell::new(thread_rng()),
13        }
14    }
15}
16
17impl Default for CoinFlipStrategy {
18    fn default() -> CoinFlipStrategy {
19        CoinFlipStrategy::new()
20    }
21}
22
23impl ExplorationStrategy for CoinFlipStrategy {
24    fn choose_path(&self, branch1: u64, branch2: u64) -> u64 {
25        if self.rng.borrow_mut().gen::<bool>() {
26            branch1
27        } else {
28            branch2
29        }
30    }
31}