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
extern crate time;
use {Player, MoveResult};
use game_manager::{Game, State};
use gdl::{Move, Score, Role};
use std::cmp::{max, min};
pub struct AlphaBetaPlayer {
depth_limit: u32,
best_move: Option<Move>
}
impl AlphaBetaPlayer {
pub fn new(depth: u32) -> AlphaBetaPlayer {
AlphaBetaPlayer { depth_limit: depth, best_move: None }
}
fn best_move(&mut self, game: &Game) -> MoveResult<Move> {
let role = game.role();
let cur_state = game.current_state();
let mut moves = game.legal_moves(cur_state, role);
assert!(moves.len() >= 1, "No legal moves");
if moves.len() == 1 {
return Ok(moves.swap_remove(0));
}
let mut max = 0;
let mut res = moves[0].clone();
self.best_move = Some(res.clone());
let opponent = opponent(game, role);
for m in moves {
let score = match self.min_score(game, cur_state, &opponent, m.clone(), 0, 100, 0) {
Ok(score) => score,
Err(m) => return Err(m)
};
if score == 100 {
return Ok(m);
} else if score > max {
max = score;
self.best_move = Some(m.clone());
res = m
}
check_time_result!(self, game);
}
Ok(res)
}
fn max_score(&mut self, game: &Game, state: &State, role: &Role, alpha: u8, beta: u8,
depth: u32) -> MoveResult<Score> {
if depth >= self.depth_limit {
return Ok(game.goal(state, game.role()));
}
if game.is_terminal(state) {
return Ok(game.goal(state, game.role()));
}
let moves = game.legal_moves(state, role);
assert!(!moves.is_empty(), "No legal moves");
let opponent = opponent(game, role);
let mut alpha = alpha;
for m in moves {
let res = match self.min_score(game, state, &opponent, m, alpha, beta, depth + 1) {
Ok(score) => score,
e @ Err(_) => return e
};
alpha = max(res, alpha);
if alpha >= beta {
return Ok(beta);
}
check_time_result!(self, game);
}
Ok(alpha)
}
fn min_score(&mut self, game: &Game, state: &State, role: &Role, last_move: Move, alpha: u8,
beta: u8, depth: u32) -> MoveResult<Score> {
let moves = game.legal_moves(state, role);
assert!(!moves.is_empty(), "No legal moves");
let mut beta = beta;
for m in moves {
let move_vec = if game.roles()[0] == *role {
vec![m, last_move.clone()]
} else {
vec![last_move.clone(), m]
};
let s = game.next_state(state, &*move_vec);
let opponent = opponent(game, role);
let res = match self.max_score(game, &s, &opponent, alpha, beta, depth) {
Ok(score) => score,
e @ Err(_) => return e
};
beta = min(res, beta);
if beta <= alpha {
return Ok(alpha);
}
check_time_result!(self, game);
}
Ok(beta)
}
}
impl Player for AlphaBetaPlayer {
fn name(&self) -> String {
"AlphaBetaPlayer".to_string()
}
fn select_move(&mut self, game: &Game) -> Move {
let m = match self.best_move(&game) {
Ok(m) => m,
Err(m) => { warn!("Out of time"); m }
};
info!("Selecting move {}", m.to_string());
m
}
fn out_of_time(&mut self, _: &Game) -> Move {
self.best_move.take().unwrap()
}
}
fn opponent<'a>(game: &'a Game, role: &'a Role) -> &'a Role {
let roles = game.roles();
assert!(roles.len() == 2, "Must be a two player game");
let res: Vec<_> = roles.into_iter().filter(|r| *r != role).collect();
assert_eq!(res.len(), 1);
res[0]
}