Implement redundant mailbox representation
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use crate::movegen::r#move::{Move, MoveType, Promote};
|
||||
use crate::movegen::r#move::{Move, MoveType};
|
||||
|
||||
use super::{
|
||||
board::{Board, Color, PieceType},
|
||||
board::PieceType,
|
||||
game::Game,
|
||||
state::{Castle, State},
|
||||
};
|
||||
|
||||
@@ -29,6 +30,7 @@ impl History {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct MoveParameters {
|
||||
pub mv: Option<Move>,
|
||||
pub moved_piece: Option<PieceType>,
|
||||
pub captured_piece: Option<PieceType>,
|
||||
pub promoted_piece: Option<PieceType>,
|
||||
pub castling_ability: Option<[Castle; 2]>,
|
||||
@@ -40,6 +42,7 @@ impl MoveParameters {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
mv: None,
|
||||
moved_piece: None,
|
||||
captured_piece: None,
|
||||
promoted_piece: None,
|
||||
castling_ability: None,
|
||||
@@ -48,11 +51,13 @@ impl MoveParameters {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(board: &Board, mv: &Move) -> Self {
|
||||
pub fn build(game: &Game, mv: &Move) -> Self {
|
||||
let mut move_parameters = Self::new();
|
||||
move_parameters.add_move(*mv);
|
||||
move_parameters.add_irreversible_parameters(board.state);
|
||||
move_parameters.add_capture_and_promotion_piece(board, *mv, board.state.current_player());
|
||||
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_promoted_piece(mv);
|
||||
|
||||
move_parameters
|
||||
}
|
||||
@@ -61,31 +66,27 @@ impl MoveParameters {
|
||||
self.mv = Some(mv);
|
||||
}
|
||||
|
||||
fn add_captured_piece(&mut self, board: &Board, dst: usize, color: Color) {
|
||||
self.captured_piece = board.piece_type_at_color(dst, color);
|
||||
fn add_moved_piece(&mut self, game: &Game, mv: &Move) {
|
||||
self.moved_piece = game.mailbox.find_piece_at(mv.src);
|
||||
}
|
||||
|
||||
fn add_promoted_piece(&mut self, promote: Promote) {
|
||||
self.promoted_piece = Some(promote.into_piece_type());
|
||||
fn add_captured_piece(&mut self, game: &Game, mv: &Move) {
|
||||
if let MoveType::Capture | MoveType::PromotionCapture(_) = mv.move_type {
|
||||
self.captured_piece = game.mailbox.find_piece_at(mv.dst)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_irreversible_parameters(&mut self, state: State) {
|
||||
fn add_promoted_piece(&mut self, mv: &Move) {
|
||||
if let MoveType::Promotion(promote) | MoveType::PromotionCapture(promote) = mv.move_type {
|
||||
self.promoted_piece = Some(promote.into_piece_type())
|
||||
}
|
||||
}
|
||||
|
||||
fn add_irreversible_parameters(&mut self, state: State) {
|
||||
self.castling_ability = Some(state.castling_ability);
|
||||
self.en_passant_square = state.en_passant_square;
|
||||
self.halfmove_clock = Some(state.halfmove_clock);
|
||||
}
|
||||
|
||||
pub fn add_capture_and_promotion_piece(&mut self, board: &Board, mv: Move, color: Color) {
|
||||
match mv.move_type {
|
||||
MoveType::Capture => self.add_captured_piece(board, mv.dst, color.opponent()),
|
||||
MoveType::Promotion(piece) => self.add_promoted_piece(piece),
|
||||
MoveType::PromotionCapture(piece) => {
|
||||
self.add_promoted_piece(piece);
|
||||
self.add_captured_piece(board, mv.dst, color.opponent());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MoveParameters {
|
||||
@@ -107,18 +108,18 @@ mod tests {
|
||||
#[test]
|
||||
fn test_unmake_quiet_and_double_push() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let board_before_make = game.board.clone();
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::B2, Square::B3, MoveType::Quiet);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(Square::B2, Square::B4, MoveType::DoublePush);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -126,18 +127,18 @@ mod tests {
|
||||
#[test]
|
||||
fn test_unmake_capture_and_promotion() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let board_before_make = game.board.clone();
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::D3, Square::B5, MoveType::Capture);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(Square::C7, Square::C8, MoveType::Promotion(Promote::Queen));
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
let mv = Move::new_with_type(
|
||||
Square::C7,
|
||||
@@ -147,7 +148,7 @@ mod tests {
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -155,12 +156,12 @@ mod tests {
|
||||
#[test]
|
||||
fn test_unmake_en_passant() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN_2)?;
|
||||
let board_before_make = game.board.clone();
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::A4, Square::B3, MoveType::EnPassant);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -168,12 +169,12 @@ mod tests {
|
||||
#[test]
|
||||
fn test_unmake_castle() -> Result<(), String> {
|
||||
let mut game = from_fen(FEN)?;
|
||||
let board_before_make = game.board.clone();
|
||||
let game_before_make = game.clone();
|
||||
let mv = Move::new_with_type(Square::E1, Square::C1, MoveType::Castle);
|
||||
game.make_move(&mv);
|
||||
game.unmake_move();
|
||||
|
||||
assert_eq!(board_before_make, game.board);
|
||||
assert_eq!(game_before_make, game);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user