Use lsb and have_common_bit fn's for readability
This commit is contained in:
93
src/board.rs
93
src/board.rs
@@ -4,6 +4,7 @@ use crate::attack::{
|
||||
get_bishop_attacks, get_king_attacks, get_knight_attacks, get_pawn_attacks, get_queen_attacks,
|
||||
get_rook_attacks,
|
||||
};
|
||||
use crate::bitboard::{have_common_bit, lsb, square_to_bitboard};
|
||||
use crate::game::{Castle, State};
|
||||
use crate::movegen::{
|
||||
bishop_pseudo_moves, king_pseudo_moves, knight_pseudo_moves, pawn_pseudo_moves,
|
||||
@@ -93,12 +94,23 @@ impl Board {
|
||||
Color::White => (&self.white_pieces, Color::Black),
|
||||
};
|
||||
|
||||
enemy[Kind::Pawn.idx()].bitboard & get_pawn_attacks(sq, own_color) != 0
|
||||
|| enemy[Kind::Knight.idx()].bitboard & get_knight_attacks(sq) != 0
|
||||
|| enemy[Kind::Bishop.idx()].bitboard & get_bishop_attacks(all_occupancies, sq) != 0
|
||||
|| enemy[Kind::Rook.idx()].bitboard & get_rook_attacks(all_occupancies, sq) != 0
|
||||
|| enemy[Kind::Queen.idx()].bitboard & get_queen_attacks(all_occupancies, sq) != 0
|
||||
|| enemy[Kind::King.idx()].bitboard & get_king_attacks(sq) != 0
|
||||
have_common_bit(
|
||||
enemy[Kind::Pawn.idx()].bitboard,
|
||||
get_pawn_attacks(sq, own_color),
|
||||
) || have_common_bit(enemy[Kind::Knight.idx()].bitboard, get_knight_attacks(sq))
|
||||
|| have_common_bit(
|
||||
enemy[Kind::Bishop.idx()].bitboard,
|
||||
get_bishop_attacks(all_occupancies, sq),
|
||||
)
|
||||
|| have_common_bit(
|
||||
enemy[Kind::Rook.idx()].bitboard,
|
||||
get_rook_attacks(all_occupancies, sq),
|
||||
)
|
||||
|| have_common_bit(
|
||||
enemy[Kind::Queen.idx()].bitboard,
|
||||
get_queen_attacks(all_occupancies, sq),
|
||||
)
|
||||
|| have_common_bit(enemy[Kind::King.idx()].bitboard, get_king_attacks(sq))
|
||||
}
|
||||
|
||||
pub fn is_move_legit(&self, square: usize, opponent_color: Color) -> bool {
|
||||
@@ -162,13 +174,12 @@ impl Board {
|
||||
Color::Black => &self.black_pieces,
|
||||
};
|
||||
|
||||
self.state.update_castling_state_quiet(mv.source, color);
|
||||
self.state
|
||||
.update_castling_state_quiet(mv.source as u8, color);
|
||||
self.state
|
||||
.update_castling_state_capture(mv.target as u8, Color::opponent_color(color));
|
||||
.update_castling_state_capture(mv.target, Color::opponent_color(color));
|
||||
self.state.change_side();
|
||||
|
||||
let own_king_square = pieces[Kind::King.idx()].bitboard.trailing_zeros() as usize;
|
||||
let own_king_square = lsb(pieces[Kind::King.idx()].bitboard);
|
||||
self.is_move_legit(own_king_square, Color::opponent_color(color))
|
||||
}
|
||||
|
||||
@@ -180,58 +191,58 @@ impl Board {
|
||||
|
||||
match &mv.move_type {
|
||||
MoveType::Quiet => {
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Capture => {
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::remove_piece(mv.target as u8, opponent_pieces);
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::remove_piece(mv.target, opponent_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::EnPassant => {
|
||||
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::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::remove_pawn_enpassant(mv.target, opponent_pieces, color);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::DoublePush => {
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
let en_passant = match color {
|
||||
Color::White => Some((mv.source + 8) as u8),
|
||||
Color::Black => Some((mv.source - 8) as u8),
|
||||
Color::White => Some(mv.source + 8),
|
||||
Color::Black => Some(mv.source - 8),
|
||||
};
|
||||
self.state.set_en_passant_target_square(en_passant);
|
||||
}
|
||||
MoveType::Promotion(promote) => {
|
||||
Self::remove_piece(mv.source as u8, own_pieces);
|
||||
Self::promote_piece(mv.target as u8, own_pieces, *promote);
|
||||
Self::remove_piece(mv.source, own_pieces);
|
||||
Self::promote_piece(mv.target, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::PromotionCapture(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::remove_piece(mv.source, own_pieces);
|
||||
Self::remove_piece(mv.target, opponent_pieces);
|
||||
Self::promote_piece(mv.target, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Castle => {
|
||||
Self::move_piece(mv.source as u8, mv.target as u8, own_pieces);
|
||||
Self::move_rook_castle(mv.target as u8, own_pieces);
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::move_rook_castle(mv.target, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
self.state.set_castling_ability(color, Castle::None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_piece(source: u8, target: u8, pieces: &mut [Piece; 6]) {
|
||||
fn move_piece(source: usize, target: usize, pieces: &mut [Piece; 6]) {
|
||||
for p in pieces.iter_mut() {
|
||||
if p.bitboard & (1_u64 << source) != 0 {
|
||||
p.bitboard &= !(1_u64 << source);
|
||||
p.bitboard |= 1_u64 << target;
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(source)) {
|
||||
p.bitboard &= !square_to_bitboard(source);
|
||||
p.bitboard |= square_to_bitboard(target);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_rook_castle(square: u8, pieces: &mut [Piece; 6]) {
|
||||
fn move_rook_castle(square: usize, pieces: &mut [Piece; 6]) {
|
||||
let (rook_source, rook_target) = match square {
|
||||
2 | 58 => (square - 2, square + 1),
|
||||
6 | 62 => (square + 1, square - 1),
|
||||
@@ -241,33 +252,33 @@ impl Board {
|
||||
Self::move_piece(rook_source, rook_target, pieces);
|
||||
}
|
||||
|
||||
fn promote_piece(square: u8, pieces: &mut [Piece; 6], promote: Promote) {
|
||||
fn promote_piece(square: usize, pieces: &mut [Piece; 6], promote: Promote) {
|
||||
match promote {
|
||||
Promote::Knight => pieces[Kind::Knight.idx()].bitboard |= 1_u64 << square,
|
||||
Promote::Bishop => pieces[Kind::Bishop.idx()].bitboard |= 1_u64 << square,
|
||||
Promote::Rook => pieces[Kind::Rook.idx()].bitboard |= 1_u64 << square,
|
||||
Promote::Queen => pieces[Kind::Queen.idx()].bitboard |= 1_u64 << square,
|
||||
Promote::Knight => pieces[Kind::Knight.idx()].bitboard |= square_to_bitboard(square),
|
||||
Promote::Bishop => pieces[Kind::Bishop.idx()].bitboard |= square_to_bitboard(square),
|
||||
Promote::Rook => pieces[Kind::Rook.idx()].bitboard |= square_to_bitboard(square),
|
||||
Promote::Queen => pieces[Kind::Queen.idx()].bitboard |= square_to_bitboard(square),
|
||||
};
|
||||
}
|
||||
|
||||
fn remove_piece(square: u8, pieces: &mut [Piece; 6]) {
|
||||
fn remove_piece(square: usize, pieces: &mut [Piece; 6]) {
|
||||
for p in pieces.iter_mut() {
|
||||
if p.bitboard & (1_u64 << square) != 0 {
|
||||
p.bitboard &= !(1_u64 << square);
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(square)) {
|
||||
p.bitboard &= !(square_to_bitboard(square));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_pawn_enpassant(square: u8, pieces: &mut [Piece; 6], color: Color) {
|
||||
fn remove_pawn_enpassant(square: usize, pieces: &mut [Piece; 6], color: Color) {
|
||||
let piece_to_remove = match color {
|
||||
Color::White => square - 8,
|
||||
Color::Black => square + 8,
|
||||
};
|
||||
|
||||
for p in pieces.iter_mut() {
|
||||
if p.bitboard & (1_u64 << piece_to_remove) != 0 {
|
||||
p.bitboard &= !(1_u64 << piece_to_remove);
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(piece_to_remove)) {
|
||||
p.bitboard &= !(square_to_bitboard(piece_to_remove));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user