diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-30 22:35:14 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-30 22:36:08 +0300 |
commit | 46fd45da0ebd90a0c1bfeb3c4df5ea0f62601b1d (patch) | |
tree | 22b641a7384cdf372649c4c58d08ef04dd7a54da /src/board | |
parent | 7849b36891ccf709a11f311c5bb84cb1466b991a (diff) | |
download | chessnost-46fd45da0ebd90a0c1bfeb3c4df5ea0f62601b1d.tar.gz |
feat: implement piece-square tables99-accuracy
Diffstat (limited to 'src/board')
-rw-r--r-- | src/board/engine.rs | 111 |
1 files changed, 85 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 } |