Add move ordering heuristic MVV/LVA

This commit is contained in:
stefiosif
2024-09-14 20:29:53 +03:00
parent d3c4078fcc
commit b26357a205
4 changed files with 72 additions and 11 deletions

View File

@@ -0,0 +1,42 @@
use crate::{board::board::Board, movegen::r#move::Move};
// Rows: Aggressors (P, N, B, R, Q, K)
// Columns: Victims (K, Q, R, B, N, P)
const MVV_LVA: [[usize; 6]; 6] = [
[30, 29, 28, 27, 26, 00],
[25, 24, 23, 22, 21, 00],
[20, 19, 18, 17, 16, 00],
[15, 14, 13, 12, 11, 00],
[10, 09, 08, 07, 06, 00],
[05, 04, 03, 02, 01, 00],
];
pub fn score_by_mvv_lva(board: &Board, mv: Move) -> usize {
match (board.piece_type_at(mv.src), board.piece_type_at(mv.dst)) {
(Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()],
_ => 0,
}
}
#[cfg(test)]
mod tests {
use crate::{
board::{board::PieceType, fen::from_fen, square::Square},
movegen::r#move::{Move, MoveType},
search::move_ordering::{score_by_mvv_lva, MVV_LVA},
};
const FEN: &'static str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
#[test]
fn test_score_by_mvv_lva() -> Result<(), String> {
let game = from_fen(FEN)?;
let f3f5 = Move::new_with_type(Square::F3, Square::F5, MoveType::Capture);
let actual = score_by_mvv_lva(&game.board, f3f5);
let expected = MVV_LVA[PieceType::Queen.idx()][PieceType::Pawn.idx()];
assert_eq!(expected, actual);
Ok(())
}
}