diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/board/engine.rs | 111 | ||||
-rw-r--r-- | src/square.rs | 8 |
2 files changed, 93 insertions, 26 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs index e951178..1e9db6a 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -16,6 +16,73 @@ pub struct PerftResult { checks: u64, } +const PAWN_BONUS: [f32; 64] = [ + 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, + 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, + 0.10, 0.10, 0.20, 0.30, 0.30, 0.20, 0.10, 0.10, + 0.05, 0.05, 0.10, 0.25, 0.25, 0.10, 0.05, 0.05, + 0.00, 0.00, 0.00, 0.20, 0.20, 0.00, 0.00, 0.00, + 0.05, -0.05, -0.10, 0.00, 0.00, -0.10, -0.05, 0.05, + 0.05, 0.10, 0.10, -0.20, -0.20, 0.10, 0.10, 0.05, + 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 +]; + +const KNIGHT_BONUS: [f32; 64] = [ + -0.50, -0.40, -0.30, -0.30, -0.30, -0.30, -0.40, -0.50, + -0.40, -0.20, 0.00, 0.00, 0.00, 0.00, -0.20, -0.40, + -0.30, 0.00, 0.10, 0.15, 0.15, 0.10, 0.00, -0.30, + -0.30, 0.05, 0.15, 0.20, 0.20, 0.15, 0.05, -0.30, + -0.30, 0.00, 0.15, 0.20, 0.20, 0.15, 0.00, -0.30, + -0.30, 0.05, 0.10, 0.15, 0.15, 0.10, 0.05, -0.30, + -0.40, -0.20, 0.00, 0.05, 0.05, 0.00, -0.20, -0.40, + -0.50, -0.40, -0.30, -0.30, -0.30, -0.30, -0.40, -0.50, +]; + +const BISHOP_BONUS: [f32; 64] = [ + -0.20, -0.10, -0.10, -0.10, -0.10, -0.10, -0.10, -0.20, + -0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.10, + -0.10, 0.00, 0.05, 0.10, 0.10, 0.05, 0.00, -0.10, + -0.10, 0.05, 0.05, 0.10, 0.10, 0.05, 0.05, -0.10, + -0.10, 0.00, 0.10, 0.10, 0.10, 0.10, 0.00, -0.10, + -0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, -0.10, + -0.10, 0.05, 0.00, 0.00, 0.00, 0.00, 0.05, -0.10, + -0.20, -0.10, -0.10, -0.10, -0.10, -0.10, -0.10, -0.20, +]; + +const ROOK_BONUS: [f32; 64] = [ + 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, + 0.05, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10, 0.05, + -0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.05, + -0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.05, + -0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.05, + -0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.05, + -0.05, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.05, + 0.00, 0.00, 0.00, 0.05, 0.05, 0.00, 0.00, 0.00 +]; + +const QUEEN_BONUS: [f32; 64] = [ + -0.20, -0.10, -0.10, -0.05, -0.05, -0.10, -0.10, -0.20, + -0.10, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, -0.10, + -0.10, 0.00, 0.05, 0.05, 0.05, 0.05, 0.00, -0.10, + -0.05, 0.00, 0.05, 0.05, 0.05, 0.05, 0.00, -0.05, + 0.00, 0.00, 0.05, 0.05, 0.05, 0.05, 0.00, -0.05, + -0.10, 0.05, 0.05, 0.05, 0.05, 0.05, 0.00, -0.10, + -0.10, 0.00, 0.05, 0.00, 0.00, 0.00, 0.00, -0.10, + -0.20, -0.10, -0.10, -0.05, -0.05, -0.10, -0.10, -0.20 +]; + +const KING_BONUS: [f32; 64] = [ + -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, + -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, + -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, + -0.30, -0.40, -0.40, -0.50, -0.50, -0.40, -0.40, -0.30, + -0.20, -0.30, -0.30, -0.40, -0.40, -0.30, -0.30, -0.20, + -0.10, -0.20, -0.20, -0.20, -0.20, -0.20, -0.20, -0.10, + 0.20, 0.20, 0.00, 0.00, 0.00, 0.00, 0.20, 0.20, + 0.20, 0.30, 0.10, 0.00, 0.00, 0.10, 0.30, 0.20 +]; + + impl Board { pub fn perft(&mut self, depth: u8, print: bool) -> PerftResult { let mut result = PerftResult::default(); @@ -82,38 +149,30 @@ impl Board { result } - /// Compute material advantage relative to the current player + /// Count player pieces' material, giving bonus for pieces standing well 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); - material += match piece_type { - PieceType::Pawn => { + + let bonus_table = match piece_type { + PieceType::Pawn => PAWN_BONUS, + PieceType::Knight => KNIGHT_BONUS, + PieceType::Bishop => BISHOP_BONUS, + PieceType::Rook => ROOK_BONUS, + PieceType::King => KING_BONUS, + PieceType::Queen => QUEEN_BONUS, + _ => panic!("Unreachable") + }; + + material += serialize_bitboard(*bitboard).iter().fold(0., |acc, square| { + acc + piece_type.static_eval() + bonus_table[ 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., - } - }) - } + Color::White => *square as usize, + Color::Black => square.mirror() as usize, } - } - _ => { - piece_type.static_eval() * pop_count(*bitboard) as f32 - } - }; + ] + }); } material } diff --git a/src/square.rs b/src/square.rs index e518a92..2e47bea 100644 --- a/src/square.rs +++ b/src/square.rs @@ -81,6 +81,14 @@ impl Square { Ok(Self::from_coords(rank as u8, file)) } + + /// Returns the square on the mirrored board, + /// e.g E4 becomes E5, G2 becomes G7 + pub fn mirror(&self) -> Self { + let rank = self.rank(); + let file = self.file(); + Self::from_coords(7 - rank, file) + } } |