pub trait Evaluate: Sized {
// Required methods
fn value_for(&self, color: Color) -> f64;
fn get_current_player_color(&self) -> Color;
fn get_legal_moves(&self) -> Vec<Move>;
fn apply_eval_move(&self, m: Move) -> Self;
// Provided methods
fn get_best_next_move(&self, depth: i32) -> (Move, u64, f64) { ... }
fn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64) { ... }
fn minimax(
&self,
depth: i32,
alpha: f64,
beta: f64,
is_maximizing: bool,
getting_move_for: Color,
board_count: &mut u64,
) -> f64 { ... }
}Expand description
Evaluate a board and extract information, such as the best and worst moves.
Required Methods§
Sourcefn value_for(&self, color: Color) -> f64
fn value_for(&self, color: Color) -> f64
Get the value of the board for a given color. This subtracts the opponents value, and accounts for piece positions and material value.
Sourcefn get_current_player_color(&self) -> Color
fn get_current_player_color(&self) -> Color
Get the current player’s color.
Sourcefn get_legal_moves(&self) -> Vec<Move>
fn get_legal_moves(&self) -> Vec<Move>
Get the legal moves for the current player.
Sourcefn apply_eval_move(&self, m: Move) -> Self
fn apply_eval_move(&self, m: Move) -> Self
Apply a move to the board for evaluation.
Provided Methods§
Sourcefn get_best_next_move(&self, depth: i32) -> (Move, u64, f64)
fn get_best_next_move(&self, depth: i32) -> (Move, u64, f64)
Get the best move for the current player with depth number of moves
of lookahead.
This method returns
- The best move
- The number of boards evaluated to come to a conclusion
- The rating of the best move
It’s best not to use the rating value by itself for anything, as it is relative to the other player’s move ratings as well.
Examples found in repository?
18fn get_cpu_move(b: &Board, best: bool) -> Move {
19 let (m, count, _) = if best {
20 b.get_best_next_move(4)
21 } else {
22 b.get_worst_next_move(4)
23 };
24
25 print!("CPU evaluated {} moves before choosing to ", count);
26 match m {
27 Move::Piece(from, to) => match (b.get_piece(from), b.get_piece(to)) {
28 (Some(piece), Some(takes)) => println!(
29 "take {}({}) with {}({})",
30 takes.get_name(),
31 to,
32 piece.get_name(),
33 from
34 ),
35 (Some(piece), None) => {
36 println!("move {}({}) to {}", piece.get_name(), from, to)
37 }
38 _ => println!("move {} to {}", from, to),
39 },
40 Move::KingSideCastle => {
41 println!("castle kingside")
42 }
43 Move::QueenSideCastle => {
44 println!("castle queenside")
45 }
46 Move::Resign => println!("resign"),
47 }
48
49 m
50}Sourcefn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64)
fn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64)
Get the best move for the current player with depth number of moves
of lookahead.
This method returns
- The best move
- The number of boards evaluated to come to a conclusion
- The rating of the best move
It’s best not to use the rating value by itself for anything, as it is relative to the other player’s move ratings as well.
Examples found in repository?
18fn get_cpu_move(b: &Board, best: bool) -> Move {
19 let (m, count, _) = if best {
20 b.get_best_next_move(4)
21 } else {
22 b.get_worst_next_move(4)
23 };
24
25 print!("CPU evaluated {} moves before choosing to ", count);
26 match m {
27 Move::Piece(from, to) => match (b.get_piece(from), b.get_piece(to)) {
28 (Some(piece), Some(takes)) => println!(
29 "take {}({}) with {}({})",
30 takes.get_name(),
31 to,
32 piece.get_name(),
33 from
34 ),
35 (Some(piece), None) => {
36 println!("move {}({}) to {}", piece.get_name(), from, to)
37 }
38 _ => println!("move {} to {}", from, to),
39 },
40 Move::KingSideCastle => {
41 println!("castle kingside")
42 }
43 Move::QueenSideCastle => {
44 println!("castle queenside")
45 }
46 Move::Resign => println!("resign"),
47 }
48
49 m
50}Sourcefn minimax(
&self,
depth: i32,
alpha: f64,
beta: f64,
is_maximizing: bool,
getting_move_for: Color,
board_count: &mut u64,
) -> f64
fn minimax( &self, depth: i32, alpha: f64, beta: f64, is_maximizing: bool, getting_move_for: Color, board_count: &mut u64, ) -> f64
Perform minimax on a certain position, and get the minimum or maximum value for a board. To get the best move, you minimize the values of the possible outcomes from your own position, and maximize the values of the replies made by the other player.
In other words, choose moves with the assumption that your opponent will make the best possible replies to your moves. Moves that are seemingly good, but are easily countered, are categorically eliminated by this algorithm.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.