From e61d2474117a32f22001b513e46b4a925b36b085 Mon Sep 17 00:00:00 2001 From: stefiosif Date: Wed, 28 Aug 2024 23:27:40 +0300 Subject: [PATCH] Make more readability refactorings --- src/board/board.rs | 46 +++++++++++++------------------------------ src/board/fen.rs | 6 +++--- src/board/history.rs | 14 ++++++------- src/board/state.rs | 16 +++++++-------- src/movegen/attack.rs | 4 ++-- src/movegen/move.rs | 16 +++++++-------- 6 files changed, 42 insertions(+), 60 deletions(-) diff --git a/src/board/board.rs b/src/board/board.rs index 059492e..4fa96e1 100644 --- a/src/board/board.rs +++ b/src/board/board.rs @@ -83,25 +83,19 @@ impl Board { Color::White => (&self.white_pieces, Color::Black), }; - have_common_bit( - opponent[PieceType::Pawn].bitboard, - fetch_pawn_attacks(square, own_color), - ) || have_common_bit( - opponent[PieceType::Knight].bitboard, - fetch_knight_attacks(square), - ) || have_common_bit( - opponent[PieceType::Bishop].bitboard, - fetch_bishop_attacks(all_occupancies, square), - ) || have_common_bit( - opponent[PieceType::Rook].bitboard, - fetch_rook_attacks(all_occupancies, square), - ) || have_common_bit( - opponent[PieceType::Queen].bitboard, - fetch_queen_attacks(all_occupancies, square), - ) || have_common_bit( - opponent[PieceType::King].bitboard, - fetch_king_attacks(square), - ) + let pawns = opponent[PieceType::Pawn].bitboard; + let knights = opponent[PieceType::Knight].bitboard; + let bishops = opponent[PieceType::Bishop].bitboard; + let rooks = opponent[PieceType::Rook].bitboard; + let queens = opponent[PieceType::Queen].bitboard; + let king = opponent[PieceType::King].bitboard; + + have_common_bit(pawns, fetch_pawn_attacks(square, own_color)) + || have_common_bit(knights, fetch_knight_attacks(square)) + || have_common_bit(bishops, fetch_bishop_attacks(all_occupancies, square)) + || have_common_bit(rooks, fetch_rook_attacks(all_occupancies, square)) + || have_common_bit(queens, fetch_queen_attacks(all_occupancies, square)) + || have_common_bit(king, fetch_king_attacks(square)) } pub fn king_under_check(&self, color: Color) -> bool { @@ -144,7 +138,7 @@ impl Board { pieces, all_occupancies, opponent_occupancies, - self.state.en_passant_target_square(), + self.state.en_passant_square(), color, ), PieceType::Knight => knight_pseudo_moves(pieces, all_occupancies, own_occupancies), @@ -284,16 +278,4 @@ mod tests { Ok(()) } - - #[test] - fn test_fifty_move_draw() -> Result<(), String> { - //TODO: make a MoveHistory/BoardHistory/GameHistory struct - Ok(()) - } - - #[test] - fn test_threefold_repetition() -> Result<(), String> { - //TODO: make a MoveHistory/BoardHistory/GameHistory struct - Ok(()) - } } diff --git a/src/board/fen.rs b/src/board/fen.rs index 3d0b2be..90e5e04 100644 --- a/src/board/fen.rs +++ b/src/board/fen.rs @@ -12,7 +12,7 @@ pub fn from_fen(fen: &str) -> Result { let castling_ability = castling_ability(fen_parts[2])?; - let en_passant_target_square = en_passant_target_square(fen_parts[3])?; + let en_passant_square = en_passant_square(fen_parts[3])?; let halfmove_clock = halfmove_clock(fen_parts[4])?; @@ -21,7 +21,7 @@ pub fn from_fen(fen: &str) -> Result { board.set_state(State::load_state( side_to_move, castling_ability, - en_passant_target_square, + en_passant_square, halfmove_clock, fullmove_counter, )); @@ -123,7 +123,7 @@ fn castling_ability(castling: &str) -> Result<[Castle; 2], FenError> { use std::collections::HashMap; -fn en_passant_target_square(square: &str) -> Result, FenError> { +fn en_passant_square(square: &str) -> Result, FenError> { let mut sqr = square.chars(); let mut files: HashMap = HashMap::new(); diff --git a/src/board/history.rs b/src/board/history.rs index 24de7b3..529b9ce 100644 --- a/src/board/history.rs +++ b/src/board/history.rs @@ -30,7 +30,7 @@ pub struct MoveParameters { pub captured_piece: Option, pub promoted_piece: Option, pub castling_ability: Option<[Castle; 2]>, - pub en_passant_target_square: Option, + pub en_passant_square: Option, pub halfmove_clock: Option, } @@ -41,7 +41,7 @@ impl MoveParameters { captured_piece: None, promoted_piece: None, castling_ability: None, - en_passant_target_square: None, + en_passant_square: None, halfmove_clock: None, } } @@ -54,25 +54,25 @@ impl MoveParameters { self.captured_piece = board.piece_type_at(dst, color); } - pub fn add_promoted_piece(&mut self, piece_type: Promote) { - self.promoted_piece = Some(piece_type.into_piece_type()) + pub fn add_promoted_piece(&mut self, promote: Promote) { + self.promoted_piece = Some(promote.into_piece_type()) } pub fn add_irreversible_parameters(&mut self, state: State) { self.castling_ability = Some(state.castling_ability); - self.en_passant_target_square = state.en_passant_target_square; + 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_color(color)) + self.add_captured_piece(board, mv.dst, Color::opponent_color(color)) } 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_color(color)); + self.add_captured_piece(board, mv.dst, Color::opponent_color(color)); } _ => (), } diff --git a/src/board/state.rs b/src/board/state.rs index 41f22df..d86cfaf 100644 --- a/src/board/state.rs +++ b/src/board/state.rs @@ -4,7 +4,7 @@ use crate::{board::board::Color, movegen::r#move::MoveType}; pub struct State { side_to_move: Color, pub castling_ability: [Castle; 2], - pub en_passant_target_square: Option, + pub en_passant_square: Option, pub halfmove_clock: u8, pub fullmove_counter: u8, } @@ -14,7 +14,7 @@ impl State { Self { side_to_move: Color::White, castling_ability: [Castle::Both, Castle::Both], - en_passant_target_square: None, + en_passant_square: None, halfmove_clock: 0, fullmove_counter: 1, } @@ -23,25 +23,25 @@ impl State { pub const fn load_state( side_to_move: Color, castling_ability: [Castle; 2], - en_passant_target_square: Option, + en_passant_square: Option, halfmove_clock: u8, fullmove_counter: u8, ) -> Self { Self { side_to_move, castling_ability, - en_passant_target_square, + en_passant_square, halfmove_clock, fullmove_counter, } } - pub const fn en_passant_target_square(&self) -> Option { - self.en_passant_target_square + pub const fn en_passant_square(&self) -> Option { + self.en_passant_square } - pub fn set_en_passant_target_square(&mut self, square: Option) { - self.en_passant_target_square = square; + pub fn set_en_passant_square(&mut self, square: Option) { + self.en_passant_square = square; } pub const fn castling_ability(&self, color: Color) -> Castle { diff --git a/src/movegen/attack.rs b/src/movegen/attack.rs index 465a7b4..98c166e 100644 --- a/src/movegen/attack.rs +++ b/src/movegen/attack.rs @@ -1,7 +1,7 @@ use crate::board::{ bitboard::{ - have_common_bit, lsb, square_to_bitboard, square_to_bitboard_wrapping, EMPTY, NOT_FILE_A, - NOT_FILE_AB, NOT_FILE_GH, NOT_FILE_H, bitboard_to_coords + bitboard_to_coords, have_common_bit, lsb, square_to_bitboard, square_to_bitboard_wrapping, + EMPTY, NOT_FILE_A, NOT_FILE_AB, NOT_FILE_GH, NOT_FILE_H, }, board::Color, square::coords_to_square, diff --git a/src/movegen/move.rs b/src/movegen/move.rs index 6b1a982..b8efb30 100644 --- a/src/movegen/move.rs +++ b/src/movegen/move.rs @@ -17,12 +17,12 @@ pub enum Promote { } impl Promote { - pub fn into_piece_type(&self) -> PieceType { + pub const fn into_piece_type(self) -> PieceType { match self { - Promote::Knight => PieceType::Knight, - Promote::Rook => PieceType::Rook, - Promote::Bishop => PieceType::Bishop, - Promote::Queen => PieceType::Queen, + Self::Knight => PieceType::Knight, + Self::Rook => PieceType::Rook, + Self::Bishop => PieceType::Bishop, + Self::Queen => PieceType::Queen, } } } @@ -152,7 +152,7 @@ impl Board { } MoveType::DoublePush => { Self::move_piece(mv.src, mv.dst, own_pieces); - self.state.set_en_passant_target_square(en_passant_square); + self.state.set_en_passant_square(en_passant_square); } MoveType::Promotion(promote) => { Self::remove_piece(mv.src, own_pieces); @@ -174,7 +174,7 @@ impl Board { pub fn unmake_move(&mut self, move_parameters: MoveParameters) { let color_before_move = self.state.change_side(); self.state.revert_full_move(color_before_move); - self.state.en_passant_target_square = move_parameters.en_passant_target_square; + self.state.en_passant_square = move_parameters.en_passant_square; if let Some(new_castling_ability) = move_parameters.castling_ability { self.state.castling_ability = new_castling_ability; @@ -232,7 +232,7 @@ impl Board { } fn update_game_state(state: &mut State, mv: &Move, color: Color, pawn_move: bool) { - state.set_en_passant_target_square(None); + state.set_en_passant_square(None); state.update_castling_state_quiet(mv.src, color); state.update_castling_state_capture(mv.dst, Color::opponent_color(color)); state.update_half_move(mv.move_type, pawn_move);