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
//! Black-White seperable policy
//!
//! Because `Agent` get single policy to play game, policy structure for black-white seperation is required.
//! It get two different policies and playing game with given seperately, black and white.
//!
//! # Examples
//! ```ignore
//! # #[macro_use] extern crate connect6;
//! # use connect6::{agent::Agent, policy::{RandomPolicy, MultiPolicy}};
//! io_policy_stdio!(io_policy);
//! let mut rand_policy = RandomPolicy::new();
//!
//! let mut multi_policy = MultiPolicy::new(&mut rand_policy, &mut io_policy);
//! Agent::debug(&mut multi_policy).play().unwrap();
//! ```
use game::{Game, Player};
use policy::Policy;

#[cfg(test)]
mod tests;

/// Black-White seperable policy
///
/// Because `Agent` get single policy to play game, policy structure for black-white seperation is required.
/// It get two different policies and playing game with given seperately, black and white.
///
/// # Examples
/// ```ignore
/// # #[macro_use] extern crate connect6;
/// # use connect6::{agent::Agent, policy::{RandomPolicy, MultiPolicy}};
/// io_policy_stdio!(io_policy);
/// let mut rand_policy = RandomPolicy::new();
///
/// let mut multi_policy = MultiPolicy::new(&mut rand_policy, &mut io_policy);
/// Agent::debug(&mut multi_policy).play().unwrap();
/// ```
pub struct MultiPolicy<'a, 'b> {
    black_policy: &'a mut Policy,
    white_policy: &'b mut Policy,
}

impl<'a, 'b> MultiPolicy<'a, 'b> {
    /// Construct a new `MultiPolicy`
    ///
    /// # Examples
    /// ```rust
    /// # extern crate connect6;
    /// # use connect6::policy::{RandomPolicy, DefaultPolicy, MultiPolicy};
    /// let mut rand_policy = RandomPolicy::new();
    /// let mut default_policy = DefaultPolicy::new();
    /// let mut multi_policy = MultiPolicy::new(&mut rand_policy, &mut default_policy);
    /// ```
    pub fn new(black_policy: &'a mut Policy, white_policy: &'b mut Policy) -> MultiPolicy<'a, 'b> {
        MultiPolicy {
            black_policy,
            white_policy,
        }
    }
}

impl<'a, 'b> Policy for MultiPolicy<'a, 'b> {
    /// Condition on `game.turn` to pass policy seperately
    fn next(&mut self, game: &Game) -> Option<(usize, usize)> {
        match game.get_turn() {
            Player::None => {
                panic!("seperate_policy::init couldn't get next policy for player none")
            }
            Player::Black => self.black_policy.next(game),
            Player::White => self.white_policy.next(game),
        }
    }
}