Pass Mailbox instead of Game on related functions for clarity
This commit is contained in:
@@ -47,7 +47,7 @@ impl Game {
|
|||||||
|
|
||||||
pub fn make_move(&mut self, mv: &Move) {
|
pub fn make_move(&mut self, mv: &Move) {
|
||||||
self.history
|
self.history
|
||||||
.push_move_parameters(MoveParameters::build(&self, mv));
|
.push_move_parameters(MoveParameters::build(self, mv));
|
||||||
|
|
||||||
let board = &mut self.board;
|
let board = &mut self.board;
|
||||||
let mailbox = &mut self.mailbox;
|
let mailbox = &mut self.mailbox;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use crate::movegen::r#move::{Move, MoveType};
|
|||||||
use super::{
|
use super::{
|
||||||
board::PieceType,
|
board::PieceType,
|
||||||
game::Game,
|
game::Game,
|
||||||
|
mailbox::Mailbox,
|
||||||
state::{Castle, State},
|
state::{Castle, State},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -55,8 +56,8 @@ impl MoveParameters {
|
|||||||
let mut move_parameters = Self::new();
|
let mut move_parameters = Self::new();
|
||||||
move_parameters.add_move(*mv);
|
move_parameters.add_move(*mv);
|
||||||
move_parameters.add_irreversible_parameters(game.board.state);
|
move_parameters.add_irreversible_parameters(game.board.state);
|
||||||
move_parameters.add_moved_piece(game, mv);
|
move_parameters.add_moved_piece(&game.mailbox, mv);
|
||||||
move_parameters.add_captured_piece(game, mv);
|
move_parameters.add_captured_piece(&game.mailbox, mv);
|
||||||
move_parameters.add_promoted_piece(mv);
|
move_parameters.add_promoted_piece(mv);
|
||||||
|
|
||||||
move_parameters
|
move_parameters
|
||||||
@@ -66,13 +67,13 @@ impl MoveParameters {
|
|||||||
self.mv = Some(mv);
|
self.mv = Some(mv);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_moved_piece(&mut self, game: &Game, mv: &Move) {
|
fn add_moved_piece(&mut self, mailbox: &Mailbox, mv: &Move) {
|
||||||
self.moved_piece = game.mailbox.find_piece_at(mv.src);
|
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 {
|
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 {
|
impl Default for MoveParameters {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
MoveParameters::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use std::str::Chars;
|
|||||||
use crate::board::{
|
use crate::board::{
|
||||||
board::PieceType,
|
board::PieceType,
|
||||||
game::Game,
|
game::Game,
|
||||||
|
mailbox::Mailbox,
|
||||||
square::{coords_to_square, square_to_algebraic},
|
square::{coords_to_square, square_to_algebraic},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -117,12 +118,17 @@ impl Move {
|
|||||||
None
|
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 {
|
fn build_with_type(
|
||||||
let moving = game.mailbox.find_piece_at(src);
|
mailbox: &Mailbox,
|
||||||
let captured = game.mailbox.find_piece_at(dst);
|
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) {
|
match (captured, promote_into) {
|
||||||
(Some(_), None) => return Self::new_with_type(src, dst, MoveType::Capture),
|
(Some(_), None) => return Self::new_with_type(src, dst, MoveType::Capture),
|
||||||
|
|||||||
@@ -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)
|
// Rows: Aggressors (P, N, B, R, Q, K)
|
||||||
// Columns: Victims (K, Q, R, B, N, P)
|
// Columns: Victims (K, Q, R, B, N, P)
|
||||||
@@ -12,11 +12,8 @@ const MVV_LVA: [[usize; 6]; 6] = [
|
|||||||
[5, 4, 3, 2, 1, 0],
|
[5, 4, 3, 2, 1, 0],
|
||||||
];
|
];
|
||||||
|
|
||||||
pub fn score_by_mvv_lva(game: &Game, mv: Move) -> usize {
|
pub const fn score_by_mvv_lva(mailbox: &Mailbox, mv: Move) -> usize {
|
||||||
match (
|
match (mailbox.find_piece_at(mv.src), mailbox.find_piece_at(mv.dst)) {
|
||||||
game.mailbox.find_piece_at(mv.src),
|
|
||||||
game.mailbox.find_piece_at(mv.dst),
|
|
||||||
) {
|
|
||||||
(Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()],
|
(Some(aggressor), Some(victim)) => MVV_LVA[aggressor.idx()][victim.idx()],
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
@@ -36,7 +33,7 @@ mod tests {
|
|||||||
fn test_score_by_mvv_lva() -> Result<(), String> {
|
fn test_score_by_mvv_lva() -> Result<(), String> {
|
||||||
let game = from_fen(FEN)?;
|
let game = from_fen(FEN)?;
|
||||||
let f3f5 = Move::new_with_type(Square::F3, Square::F5, MoveType::Capture);
|
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()];
|
let expected = MVV_LVA[PieceType::Queen.idx()][PieceType::Pawn.idx()];
|
||||||
|
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ pub fn negamax(
|
|||||||
let mut legal_moves = 0;
|
let mut legal_moves = 0;
|
||||||
|
|
||||||
let mut pseudo_legal_moves = game.board.pseudo_moves_all();
|
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 {
|
for mv in pseudo_legal_moves {
|
||||||
game.make_move(&mv);
|
game.make_move(&mv);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option<Move>,
|
|||||||
})
|
})
|
||||||
.collect();
|
.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 {
|
for mv in captures {
|
||||||
game.make_move(&mv);
|
game.make_move(&mv);
|
||||||
|
|||||||
Reference in New Issue
Block a user