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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use game::{RandGame, Game, ParseGame};
use std::fmt;
use std::io;
use rand;
use strategies::Strategy;
pub trait Player<G>
where
    G: Game + Send,
    G::Agent: Send,
    G::Move: Send + Ord,
{
    fn choose_move(&mut self, game: &G) -> G::Move;
    fn display_name(&self) -> &str;
    fn player_type(&self) -> &str;
    fn full_name(&self) -> String {
        format!("{} ({})", self.display_name(), self.player_type())
    }
}

pub struct HumanPlayer {
    name: String,
}

impl HumanPlayer {
    pub fn new(name: &str) -> Self {
        HumanPlayer { name: String::from(name) }
    }
}

impl<G> Player<G> for HumanPlayer
where
    G: ParseGame + Send,
    G::Agent: Send + fmt::Display,
    G::Move: Send + Ord + fmt::Debug,
{
    fn display_name(&self) -> &str {
        self.name.as_str()
    }

    fn player_type(&self) -> &str {
        "Human"
    }

    fn choose_move(&mut self, game: &G) -> G::Move {
        let agent = game.to_act();
        println!("{}'s move.", agent);

        loop {
            println!("What is your move?");
            let mut choice = String::new();
            io::stdin().read_line(&mut choice).expect(
                "Failed to read line... something is mad broke.",
            );
            println!("");

            let choice = match game.parse_move(choice.trim()) {
                Some(m) => m,
                None => continue,
            };

            println!("{:?}", choice);

            if !game.move_valid(&choice) {
                println!("Invalid move..");
                continue;
            }

            return choice;
        }
    }
}

use std::marker::PhantomData;
pub struct AIPlayer<G: Game, S: Strategy<G>> {
    name: String,
    strategy: S,
    _phantom: PhantomData<G>,
}

impl<G, S> Player<G> for AIPlayer<G, S>
where
    G: RandGame + Send + fmt::Display,
    G::Agent: Send,
    G::Move: Send + Ord + fmt::Debug,
    S: Strategy<G>,
{
    fn display_name(&self) -> &str {
        self.name.as_str()
    }

    fn player_type(&self) -> &str {
        "Computer"
    }

    fn choose_move(&mut self, board: &G) -> G::Move {
        println!("Computer is thinking.....");
        let m = self.strategy.decide(board);
        println!("CHOSE MOVE: {:?}", m);
        m
    }
}

impl<S, G: Game + fmt::Display> AIPlayer<G, S>
where
    S: Strategy<G>,
{
    pub fn new(name: &str, params: S::Params) -> Self {
        AIPlayer {
            name: String::from(name),
            strategy: S::create(params),
            _phantom: PhantomData,
        }
    }
}

pub type Plr<'a, G> = &'a mut Player<G>;

pub struct Runner<'a, G: Game + 'a> {
    board: G,
    players: (Plr<'a, G>, Plr<'a, G>),
}

impl<'a, G> Runner<'a, G>
where
    G: Game + Send + fmt::Display + Clone,
    G::Agent: Send + rand::Rand + fmt::Display,
    G::Move: Send + Ord,
{
    pub fn new(p1: Plr<'a, G>, p2: Plr<'a, G>) -> Self {
        // Self::new_with_first_to_act(rand::random::<G::Agent>(), p1, p2)
        Self::new_with_first_to_act(rand::random::<G::Agent>(), p1, p2)
    }

    pub fn new_with_first_to_act(agent: G::Agent, p1: Plr<'a, G>, p2: Plr<'a, G>) -> Self {
        Runner {
            board: G::new(&agent),
            players: (p1, p2),
        }
    }

    fn init(&mut self) {
        println!("CONNECT FOUR");
        println!("Player 1 is {}", self.players.0.full_name());
        println!("Player 2 is {}", self.players.1.full_name());
        println!("Flipping to see who starts...");

        println!("{} goes first!", self.board.to_act());
    }

    fn check_winner(&mut self) -> bool {
        self.board.has_winner()
    }

    fn step(&mut self) {
        let cloned_board = self.board.clone();
        println!("{}", cloned_board);
        if self.board.agent_id(&cloned_board.to_act()) == 0 {
            let p1_move = (*self).players.0.choose_move(&cloned_board);
            self.board.try_move(p1_move);
        } else {
            let p2_move = (*self).players.1.choose_move(&cloned_board);
            self.board.try_move(p2_move);
        }
    }


    fn game_loop(&mut self) {
        while !self.check_winner() {
            self.step()
        }

        let winner = self.board.winner().unwrap();
        println!("Winner is: {}", winner);
    }

    pub fn run<'b>(p1: Plr<'b, G>, p2: Plr<'b, G>) -> Option<G::Agent> {
        let mut runner = Runner::new(p1, p2);
        runner.init();
        runner.game_loop();
        runner.board.winner()
    }
}