Add history heuristics, reorder killer move to come after captures

This commit is contained in:
stefiosif
2025-02-07 19:51:18 +02:00
parent bdd065efff
commit 614590b18e
6 changed files with 62 additions and 11 deletions

View File

@@ -1,25 +1,36 @@
use crate::{board::mailbox::Mailbox, movegen::r#move::Move};
use crate::{
board::{board::Color, mailbox::Mailbox},
movegen::r#move::Move,
};
use super::MAX_HISTORY;
// ttmove: -100, mvvlva: [-32, 5], killer:6, quiet[7, ..]
pub fn score_move(
mailbox: &Mailbox,
mv: Move,
killer_move: Option<Move>,
color: Color,
history_heuristic: &[[[i16; 64]; 64]; 2],
tt_move: Option<Move>,
) -> i16 {
if Some(mv) == tt_move {
return -100;
}
if mv.is_capture() {
let aggressor = mailbox.piece_at(mv.src()).unwrap();
if let Some(victim) = mailbox.piece_at(mv.dst()) {
return aggressor.0.idx() as i16 - 8 * victim.0.idx() as i16;
}
return 0; //en passant
}
if Some(mv) == killer_move {
return -90;
return 6;
}
let aggressor = mailbox.piece_at(mv.src()).expect("No aggressor found.");
if let Some(victim) = mailbox.piece_at(mv.dst()) {
return (aggressor.0.idx() - 8 * victim.0.idx()) as i16;
}
100
-history_heuristic[color][mv.src()][mv.dst()] + MAX_HISTORY + 6 + 1
}
#[cfg(test)]
@@ -41,11 +52,29 @@ mod tests {
let castle = Move::new_with_type(Square::E1, Square::C1, MoveType::QueenCastle);
let mut moves = vec![castle, queen_takes_pawn, pawn_takes_queen];
moves.sort_unstable_by_key(|mv| score_move(&game.mailbox, *mv, None, None));
moves.sort_unstable_by_key(|mv| {
score_move(
&game.mailbox,
*mv,
None,
game.current_player(),
&[[[0; 64]; 64]; 2],
None,
)
});
assert_eq!(moves, vec![pawn_takes_queen, queen_takes_pawn, castle]);
moves.sort_unstable_by_key(|mv| score_move(&game.mailbox, *mv, None, Some(castle)));
moves.sort_unstable_by_key(|mv| {
score_move(
&game.mailbox,
*mv,
None,
game.current_player(),
&[[[0; 64]; 64]; 2],
Some(castle),
)
});
assert_eq!(moves, vec![castle, pawn_takes_queen, queen_takes_pawn]);