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
//! Play game with random selection. //! //! # Examples //! ```rust //! # extern crate connect6; //! # use connect6::{agent::Agent, policy::RandomPolicy}; //! let mut policy = RandomPolicy::new(); //! let result = Agent::new(&mut policy).play(); //! assert!(result.is_ok()); //! ``` use game::Game; use policy::{Policy, Simulate}; use rand; use rand::prelude::SliceRandom; #[cfg(test)] mod tests; /// Play game with random selection. /// /// # Examples /// ```rust /// # extern crate connect6; /// # use connect6::{agent::Agent, policy::RandomPolicy}; /// let mut policy = RandomPolicy::new(); /// let result = Agent::new(&mut policy).play(); /// assert!(result.is_ok()); /// ``` pub struct RandomPolicy {} impl RandomPolicy { /// Construct a new RandomPolicy pub fn new() -> RandomPolicy { RandomPolicy {} } } impl Policy for RandomPolicy { /// make random selection fn next(&mut self, game: &Game) -> Option<(usize, usize)> { let sim = Simulate::from_game(game); let node = sim.node.borrow(); // choose position from vector `possible` node.possible.choose(&mut rand::thread_rng()).map(|x| *x) } }