Pass Mailbox instead of Game on related functions for clarity

This commit is contained in:
stefiosif
2024-09-21 15:42:41 +03:00
parent 1ed4a0c2f9
commit a96d3d27b1
6 changed files with 25 additions and 21 deletions

View File

@@ -47,7 +47,7 @@ impl Game {
pub fn make_move(&mut self, mv: &Move) {
self.history
.push_move_parameters(MoveParameters::build(&self, mv));
.push_move_parameters(MoveParameters::build(self, mv));
let board = &mut self.board;
let mailbox = &mut self.mailbox;

View File

@@ -3,6 +3,7 @@ use crate::movegen::r#move::{Move, MoveType};
use super::{
board::PieceType,
game::Game,
mailbox::Mailbox,
state::{Castle, State},
};
@@ -55,8 +56,8 @@ impl MoveParameters {
let mut move_parameters = Self::new();
move_parameters.add_move(*mv);
move_parameters.add_irreversible_parameters(game.board.state);
move_parameters.add_moved_piece(game, mv);
move_parameters.add_captured_piece(game, mv);
move_parameters.add_moved_piece(&game.mailbox, mv);
move_parameters.add_captured_piece(&game.mailbox, mv);
move_parameters.add_promoted_piece(mv);
move_parameters
@@ -66,13 +67,13 @@ impl MoveParameters {
self.mv = Some(mv);
}
fn add_moved_piece(&mut self, game: &Game, mv: &Move) {
self.moved_piece = game.mailbox.find_piece_at(mv.src);
fn add_moved_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
self.moved_piece = mailbox.find_piece_at(mv.src);
}
fn add_captured_piece(&mut self, game: &Game, mv: &Move) {
fn add_captured_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
if let MoveType::Capture | MoveType::PromotionCapture(_) = mv.move_type {
self.captured_piece = game.mailbox.find_piece_at(mv.dst)
self.captured_piece = mailbox.find_piece_at(mv.dst)
}
}
@@ -91,7 +92,7 @@ impl MoveParameters {
impl Default for MoveParameters {
fn default() -> Self {
MoveParameters::new()
Self::new()
}
}

View File

@@ -4,6 +4,7 @@ use std::str::Chars;
use crate::board::{
board::PieceType,
game::Game,
mailbox::Mailbox,
square::{coords_to_square, square_to_algebraic},
};
@@ -117,12 +118,17 @@ impl Move {
None
};
Ok(Self::build_with_type(game, src, dst, promote_into))
Ok(Self::build_with_type(&game.mailbox, src, dst, promote_into))
}
fn build_with_type(game: &Game, src: usize, dst: usize, promote_into: Option<Promote>) -> Self {
let moving = game.mailbox.find_piece_at(src);
let captured = game.mailbox.find_piece_at(dst);
fn build_with_type(
mailbox: &Mailbox,
src: usize,
dst: usize,
promote_into: Option<Promote>,
) -> Self {
let moving = mailbox.find_piece_at(src);
let captured = mailbox.find_piece_at(dst);
match (captured, promote_into) {
(Some(_), None) => return Self::new_with_type(src, dst, MoveType::Capture),

View File

@@ -1,4 +1,4 @@
use crate::{board::game::Game, movegen::r#move::Move};
use crate::{board::mailbox::Mailbox, movegen::r#move::Move};
// Rows: Aggressors (P, N, B, R, Q, K)
// Columns: Victims (K, Q, R, B, N, P)
@@ -12,11 +12,8 @@ const MVV_LVA: [[usize; 6]; 6] = [
[5, 4, 3, 2, 1, 0],
];
pub fn score_by_mvv_lva(game: &Game, mv: Move) -> usize {
match (
game.mailbox.find_piece_at(mv.src),
game.mailbox.find_piece_at(mv.dst),
) {
pub const fn score_by_mvv_lva(mailbox: &Mailbox, mv: Move) -> usize {
match (mailbox.find_piece_at(mv.src), mailbox.find_piece_at(mv.dst)) {
(Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()],
_ => 0,
}
@@ -36,7 +33,7 @@ mod tests {
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, f3f5);
let actual = score_by_mvv_lva(&game.mailbox, f3f5);
let expected = MVV_LVA[PieceType::Queen.idx()][PieceType::Pawn.idx()];
assert_eq!(expected, actual);

View File

@@ -23,7 +23,7 @@ pub fn negamax(
let mut legal_moves = 0;
let mut pseudo_legal_moves = game.board.pseudo_moves_all();
pseudo_legal_moves.sort_unstable_by_key(|mv| score_by_mvv_lva(&game, *mv));
pseudo_legal_moves.sort_unstable_by_key(|mv| score_by_mvv_lva(&game.mailbox, *mv));
for mv in pseudo_legal_moves {
game.make_move(&mv);

View File

@@ -30,7 +30,7 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option<Move>,
})
.collect();
captures.sort_unstable_by_key(|mv| score_by_mvv_lva(&game, *mv));
captures.sort_unstable_by_key(|mv| score_by_mvv_lva(&game.mailbox, *mv));
for mv in captures {
game.make_move(&mv);