Refactor duplicate code and make it more readable
This commit is contained in:
34
src/board.rs
34
src/board.rs
@@ -180,21 +180,21 @@ impl Board {
|
||||
|
||||
match &mv.move_type {
|
||||
MoveType::Quiet => {
|
||||
Board::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Capture => {
|
||||
Board::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Board::remove_piece(mv.target as u8, opponent_pieces);
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::remove_piece(mv.target as u8, opponent_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::EnPassant => {
|
||||
Board::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Board::remove_pawn_enpassant(mv.target as u8, opponent_pieces, color);
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::remove_pawn_enpassant(mv.target as u8, opponent_pieces, color);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::DoublePush => {
|
||||
Board::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
let en_passant = match color {
|
||||
Color::White => Some((mv.source + 8) as u8),
|
||||
Color::Black => Some((mv.source - 8) as u8),
|
||||
@@ -202,19 +202,19 @@ impl Board {
|
||||
self.state.set_en_passant_target_square(en_passant);
|
||||
}
|
||||
MoveType::Promotion(promote) => {
|
||||
Board::remove_piece(mv.source as u8, own_pieces);
|
||||
Board::promote_piece(mv.target as u8, own_pieces, *promote);
|
||||
Self::remove_piece(mv.source as u8, own_pieces);
|
||||
Self::promote_piece(mv.target as u8, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::PromotionCapture(promote) => {
|
||||
Board::remove_piece(mv.source as u8, own_pieces);
|
||||
Board::remove_piece(mv.target as u8, opponent_pieces);
|
||||
Board::promote_piece(mv.target as u8, own_pieces, *promote);
|
||||
Self::remove_piece(mv.source as u8, own_pieces);
|
||||
Self::remove_piece(mv.target as u8, opponent_pieces);
|
||||
Self::promote_piece(mv.target as u8, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Castle => {
|
||||
Board::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Board::move_rook_castle(mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_rook_castle(mv.target as u8, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
self.state.set_castling_ability(color, Castle::None)
|
||||
}
|
||||
@@ -238,7 +238,7 @@ impl Board {
|
||||
_ => return,
|
||||
};
|
||||
|
||||
Board::move_piece(rook_source, rook_target, pieces);
|
||||
Self::move_piece(rook_source, rook_target, pieces);
|
||||
}
|
||||
|
||||
fn promote_piece(square: u8, pieces: &mut [Piece; 6], promote: Promote) {
|
||||
@@ -320,10 +320,10 @@ pub enum Color {
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub const fn opponent_color(color: Color) -> Color {
|
||||
pub const fn opponent_color(color: Self) -> Self {
|
||||
match color {
|
||||
Color::White => Color::Black,
|
||||
Color::Black => Color::White,
|
||||
Self::White => Self::Black,
|
||||
Self::Black => Self::White,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user