diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-08-15 03:32:02 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-08-15 03:59:38 +0300 |
commit | 2120ff5c2118f49bc17ad18d429c344f199e2da1 (patch) | |
tree | fc2e13d9d59cd58f483a2de0bb6c3a76be335001 | |
parent | 369f52284a6443c599c15bf9d0bf2206a489452d (diff) | |
download | chessnost-2120ff5c2118f49bc17ad18d429c344f199e2da1.tar.gz |
feat: move king to center in the endgame
-rw-r--r-- | src/grossmeister/evaluation.rs | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/grossmeister/evaluation.rs b/src/grossmeister/evaluation.rs index 308f320..eb59b92 100644 --- a/src/grossmeister/evaluation.rs +++ b/src/grossmeister/evaluation.rs @@ -76,7 +76,24 @@ const KING_BONUS: [f32; 64] = [ 0.20, 0.10, 0.30, -0.20, 0.00, 0.10, 0.30, 0.20 ]; +const KING_BONUS_ENGAME: [f32; 64] = [ + // A B C D E F G H + -0.50, -0.40, -0.30, -0.20, -0.20, -0.30, -0.40, -0.50, + -0.30, -0.20, -0.10, 0.00, 0.00, -0.10, -0.20, -0.30, + -0.30, -0.10, 0.20, 0.30, 0.30, 0.20, -0.10, -0.30, + -0.30, -0.10, 0.30, 0.40, 0.40, 0.30, -0.10, -0.30, + -0.30, -0.10, 0.30, 0.40, 0.40, 0.30, -0.10, -0.30, + -0.30, -0.10, 0.20, 0.30, 0.30, 0.20, -0.10, -0.30, + -0.30, -0.30, 0.00, 0.00, 0.00, 0.00, -0.30, -0.30, + -0.50, -0.30, -0.30, -0.30, -0.30, -0.30, -0.30, -0.50 +]; + impl Grossmeister { + pub fn is_endgame(&self) -> bool { + // No queens on the board + self.board.piece_sets[Piece::Queen as usize].pop_count() == 0 && + self.board.piece_sets[Piece::QueenBlack as usize].pop_count() == 0 + } /// Evaluate a position relative to the current player pub fn evaluate(&self) -> f32 { let color = self.board.color(); @@ -99,6 +116,7 @@ impl Grossmeister { /// Count player pieces' material, giving bonus for pieces standing well pub fn material(&self, color: Color) -> f32 { + let is_endgame = self.is_endgame(); let mut material = 0f32; for (piece_index, bitboard) in self.board.pieces_by_color(color).iter().enumerate() { let piece_type = Piece::from(piece_index); @@ -108,7 +126,11 @@ impl Grossmeister { Piece::Knight => KNIGHT_BONUS, Piece::Bishop => BISHOP_BONUS, Piece::Rook => ROOK_BONUS, - Piece::King => KING_BONUS, + Piece::King => if is_endgame { + KING_BONUS_ENGAME + } else { + KING_BONUS + }, Piece::Queen => QUEEN_BONUS, _ => panic!("Unreachable") }; @@ -463,4 +485,29 @@ mod tests { assert_eq!(score1.abs(), score2.abs()); } + #[test] + fn not_endgame() { + let board = Board::new(); + let gm = Grossmeister::new(board); + let is_endgame = gm.is_endgame(); + assert!(!is_endgame); + } + + #[test] + fn is_endgame() { + let fen = String::from("8/1p6/p4k1K/6r1/8/8/8/8 w - - 10 49"); + let board = Board::from_FEN(fen); + let gm = Grossmeister::new(board); + let is_endgame = gm.is_endgame(); + assert!(is_endgame); + } + + #[test] + fn is_endgame_no_queens() { + let fen = String::from("rnb1kbnr/pppppppp/8/8/8/8/PPPPPPPP/RNB1KBNR w KQkq - 0 1"); + let board = Board::from_FEN(fen); + let gm = Grossmeister::new(board); + let is_endgame = gm.is_endgame(); + assert!(is_endgame); + } } |