diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/board/engine.rs | 74 |
1 files changed, 39 insertions, 35 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs index 1e2c7c1..ed533df 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -81,41 +81,39 @@ impl Board { } /// Compute material advantage relative to the current player - pub fn material_advantage(&self) -> f32 { - let mut eval = 0f32; - for (piece_index, bitboard) in self.pieces.iter().enumerate() { + pub fn material(&self, color: Color) -> f32 { + let mut material = 0f32; + for (piece_index, bitboard) in self.pieces_by_color(color).iter().enumerate() { let piece_type = PieceType::from(piece_index); - let sign = if Color::from_piece(piece_type) == self.color() { - 1. - } else { - -1. - }; - - eval += sign * match piece_type { + material += match piece_type { PieceType::Pawn => { - serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { - acc + match (*square).rank() { - 6 => 3., - 5 => 2., - _ => 1., - } - }) - } - PieceType::PawnBlack => { - serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { - acc + match (*square).rank() { - 1 => 3., - 2 => 2., - _ => 1., + match color { + Color::White => { + serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { + acc + match (*square).rank() { + 6 => 3., + 5 => 2., + _ => 1., + } + }) + }, + Color::Black => { + serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { + acc + match (*square).rank() { + 1 => 3., + 2 => 2., + _ => 1., + } + }) } - }) + } } _ => { piece_type.static_eval() * pop_count(*bitboard) as f32 } }; } - eval + material } /// Returns sum of the doubled, blocked and isolated pawns @@ -180,20 +178,24 @@ impl Board { /// Evaluate a position relative to the current player pub fn evaluate(&self, precomputed_mobility: Option<f32>) -> f32 { - let opponent_mobility = self.mobility(self.color().flip()); + let color = self.color(); + let opponent_color = color.flip(); + + let opponent_mobility = self.mobility(opponent_color); let player_mobility = match precomputed_mobility { Some(m) => m, - None => self.mobility(self.color()), + None => self.mobility(color), }; let mobility_advantage = player_mobility - opponent_mobility as f32; - let material_advantage = self.material_advantage(); + let opponent_material = self.material(opponent_color); + let material_advantage = self.material(color) - opponent_material; - let pawn_structure_penalty = self.pawn_structure_penalty(self.color()) - self.pawn_structure_penalty(self.color().flip()); + let pawn_structure_penalty = self.pawn_structure_penalty(color) - self.pawn_structure_penalty(opponent_color); - let king_tropism_penalty = self.king_tropism(self.color()) - self.king_tropism(self.color().flip()); + let king_tropism_penalty = self.king_tropism(color) - self.king_tropism(opponent_color); - material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty - 0.015 * king_tropism_penalty + material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty - king_tropism_penalty * opponent_material / 3000.0 } /// Evaluate move for move ordering, prioritizing efficient captures @@ -413,7 +415,7 @@ impl Board { #[cfg(test)] mod tests { - use crate::board::{Board, engine::PerftResult}; + use crate::board::{Board, engine::PerftResult, Color}; #[test] fn perft() { @@ -449,9 +451,11 @@ mod tests { } #[test] - fn material_advantage() { + fn material() { let board = Board::new(); - assert_eq!(board.material_advantage(), 0.0, "Material advantage should be 0 at starting position"); + assert_eq!(board.material(Color::Black), 38.0); + assert_eq!(board.material(Color::White), 38.0); } + } |