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 {