Make more readability refactorings

This commit is contained in:
stefiosif
2024-08-28 23:27:40 +03:00
parent 46cf0f0d1f
commit e61d247411
6 changed files with 42 additions and 60 deletions

View File

@@ -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(())
}
}

View File

@@ -12,7 +12,7 @@ pub fn from_fen(fen: &str) -> Result<Game, FenError> {
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<Game, FenError> {
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<Option<usize>, FenError> {
fn en_passant_square(square: &str) -> Result<Option<usize>, FenError> {
let mut sqr = square.chars();
let mut files: HashMap<char, usize> = HashMap::new();

View File

@@ -30,7 +30,7 @@ pub struct MoveParameters {
pub captured_piece: Option<PieceType>,
pub promoted_piece: Option<PieceType>,
pub castling_ability: Option<[Castle; 2]>,
pub en_passant_target_square: Option<usize>,
pub en_passant_square: Option<usize>,
pub halfmove_clock: Option<u8>,
}
@@ -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));
}
_ => (),
}

View File

@@ -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<usize>,
pub en_passant_square: Option<usize>,
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<usize>,
en_passant_square: Option<usize>,
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<usize> {
self.en_passant_target_square
pub const fn en_passant_square(&self) -> Option<usize> {
self.en_passant_square
}
pub fn set_en_passant_target_square(&mut self, square: Option<usize>) {
self.en_passant_target_square = square;
pub fn set_en_passant_square(&mut self, square: Option<usize>) {
self.en_passant_square = square;
}
pub const fn castling_ability(&self, color: Color) -> Castle {

View File

@@ -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,

View File

@@ -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);