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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
pub mod connectfour;
pub mod dots;


#[derive(Hash, Clone)]
pub struct ValidMove<G: Game> {
    valid_move: G::Move,
    valid_for: G,
}

pub struct ValidMoveMut<'gs, G: Game + 'gs> {
    valid_move: G::Move,
    valid_for: &'gs mut G,
}

impl<G: Game> ValidMove<G> {
    pub fn valid_move(&self) -> &G::Move {
        &self.valid_move
    }

    pub fn apply(mut self) -> G {
        self.valid_for.apply(self.valid_move);
        self.valid_for
    }
}

impl<'gs, G: Game> ValidMoveMut<'gs, G> {
    pub fn valid_move(&self) -> &G::Move {
        &self.valid_move
    }

    #[allow(dead_code)]
    fn apply(self) {
        self.valid_for.apply(self.valid_move);
    }
}

pub type Score = i32;

pub trait ParseGame: Game {
    fn parse_move(&self, &str) -> Option<Self::Move>;
}

use rand::Rng;
pub trait RandGame: Game + Clone {
    fn random_move<R: Rng>(&mut self, rng: &mut R) -> Option<ValidMoveMut<Self>> {
        let mut moves = self.possible_moves();
        rng.shuffle(&mut moves);
        moves.into_iter().next().map(move |m| {
            ValidMoveMut {
                valid_move: m.valid_move,
                valid_for: self,
            }
        })
    }

    fn random_outcome<R: Rng>(mut self, rng: &mut R) -> Option<Self::Agent> {

        while !self.has_winner() {
            match self.random_move(rng) {
                None => return None,
                Some(m) => m.apply(),
            }
            if self.has_winner() {
                return self.winner();
            }
        }
        None
    }

    fn monte_carlo<R: Rng>(self, rng: &mut R, trials: u32) -> u32 {

        let ref_player = self.ref_player();
        (0..trials)
            .flat_map(|_| (&self).clone().random_outcome(rng))
            .filter(move |c| *c == ref_player)
            .map(|_| 1)
            .sum()
    }
}

pub trait Game: Clone + Send {
    type Move: Clone + Copy + Send + Ord;
    type Agent: PartialEq + Clone + Copy + Send + Ord;
    fn to_act(&self) -> Self::Agent;
    fn player_weight(&self, &Self::Agent) -> Score;
    fn winner(&self) -> Option<Self::Agent>;
    fn agent_id(&self, &Self::Agent) -> u32;

    fn ref_player(&self) -> Self::Agent;
    fn new(&Self::Agent) -> Self;
    fn possible_moves(&self) -> Vec<ValidMove<Self>>;

    fn move_valid(&self, &Self::Move) -> bool;
    fn has_won(&self, agent: &Self::Agent) -> bool;
    fn apply(&mut self, Self::Move);

    fn try_move_mut(&mut self, m: Self::Move) -> bool {
        self.verify_move_mut(m).map(|m| m.apply()).is_some()
    }
    fn verify_move_mut(&mut self, m: Self::Move) -> Option<ValidMoveMut<Self>> {
        if !self.move_valid(&m) {
            return None;
        }

        Some(ValidMoveMut {
            valid_move: m,
            valid_for: self,
        })
    }

    fn verify_move(self: Self, m: Self::Move) -> Option<ValidMove<Self>> {
        if !self.move_valid(&m) {
            return None;
        }

        if self.has_winner() {
            return None;
        }

        Some(ValidMove {
            valid_move: m,
            valid_for: self,
        })
    }

    fn has_winner(&self) -> bool {
        self.winner().is_some()
    }


    fn try_move(&mut self, m: Self::Move) -> bool {
        let x = self.clone().verify_move(m);
        x.map(move |valid_move| {
            let new_board = valid_move.apply();
            *self = new_board;
        }).is_some()
    }

    fn try_moves<I>(&mut self, moves: I) -> Vec<bool>
    where
        I: Iterator<Item = Self::Move>,
    {
        moves.map(move |m| self.try_move(m)).collect()
    }
}