From edf252ab85bb790c2fabaa048350d2bd7dc75ada Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 27 Jan 2023 23:03:04 +0300 Subject: feat: add king tropism to evaluation --- src/board/engine.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/board/engine.rs b/src/board/engine.rs index e11d96e..1e2c7c1 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -154,6 +154,30 @@ impl Board { result } + /// Returns the weighted sum of distances from attacking pieces to a king + /// The higher this value, the more likely the king is to be in danger + pub fn king_tropism(&self, color: Color) -> f32 { + let mut result = 0.0; + + let king_square = bitscan(match color { + Color::White => self.pieces[PieceType::King as usize], + Color::Black => self.pieces[PieceType::KingBlack as usize], + }); + + for (piece_type, bitboard) in self.pieces_by_color(color.flip()).iter().enumerate() { + if piece_type != PieceType::King as usize && piece_type != PieceType::Pawn as usize { + for square in serialize_bitboard(*bitboard) { + let distance = + (king_square.rank() as f32 - square.rank() as f32).abs() + + (king_square.file() as f32 - square.file() as f32).abs(); + + result += distance * PieceType::from(piece_type).static_eval(); + } + } + } + result + } + /// Evaluate a position relative to the current player pub fn evaluate(&self, precomputed_mobility: Option) -> f32 { let opponent_mobility = self.mobility(self.color().flip()); @@ -167,7 +191,9 @@ impl Board { let pawn_structure_penalty = self.pawn_structure_penalty(self.color()) - self.pawn_structure_penalty(self.color().flip()); - material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty + let king_tropism_penalty = self.king_tropism(self.color()) - self.king_tropism(self.color().flip()); + + material_advantage + 0.1 * mobility_advantage - 0.5 * pawn_structure_penalty - 0.015 * king_tropism_penalty } /// Evaluate move for move ordering, prioritizing efficient captures -- cgit v1.2.3