Rename names for better readability
This commit is contained in:
125
src/board.rs
125
src/board.rs
@@ -1,8 +1,8 @@
|
||||
use u64 as Bitboard;
|
||||
|
||||
use crate::attack::{
|
||||
get_bishop_attacks, get_king_attacks, get_knight_attacks, get_pawn_attacks, get_queen_attacks,
|
||||
get_rook_attacks,
|
||||
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
|
||||
fetch_queen_attacks, fetch_rook_attacks,
|
||||
};
|
||||
use crate::bitboard::{have_common_bit, lsb, square_to_bitboard};
|
||||
use crate::movegen::{
|
||||
@@ -76,42 +76,44 @@ impl Board {
|
||||
self.state = state;
|
||||
}
|
||||
|
||||
pub fn get_white_occupancies(&self) -> Bitboard {
|
||||
pub fn white_occupancies(&self) -> Bitboard {
|
||||
self.white_pieces.iter().fold(0, |acc, p| p.bitboard | acc)
|
||||
}
|
||||
|
||||
pub fn get_black_occupancies(&self) -> Bitboard {
|
||||
pub fn black_occupancies(&self) -> Bitboard {
|
||||
self.black_pieces.iter().fold(0, |acc, p| p.bitboard | acc)
|
||||
}
|
||||
|
||||
pub fn get_all_occupancies(&self) -> Bitboard {
|
||||
self.get_white_occupancies() | self.get_black_occupancies()
|
||||
pub fn all_occupancies(&self) -> Bitboard {
|
||||
self.white_occupancies() | self.black_occupancies()
|
||||
}
|
||||
|
||||
pub fn is_attacked(&self, sq: usize, opponent_color: Color) -> bool {
|
||||
let all_occupancies = self.get_all_occupancies();
|
||||
let (enemy, own_color) = match opponent_color {
|
||||
pub fn is_attacked(&self, square: usize, opponent_color: Color) -> bool {
|
||||
let all_occupancies = self.all_occupancies();
|
||||
let (opponent, own_color) = match opponent_color {
|
||||
Color::Black => (&self.black_pieces, Color::White),
|
||||
Color::White => (&self.white_pieces, Color::Black),
|
||||
};
|
||||
|
||||
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))
|
||||
opponent[Kind::Pawn.idx()].bitboard,
|
||||
fetch_pawn_attacks(square, own_color),
|
||||
) || have_common_bit(
|
||||
opponent[Kind::Knight.idx()].bitboard,
|
||||
fetch_knight_attacks(square),
|
||||
) || have_common_bit(
|
||||
opponent[Kind::Bishop.idx()].bitboard,
|
||||
fetch_bishop_attacks(all_occupancies, square),
|
||||
) || have_common_bit(
|
||||
opponent[Kind::Rook.idx()].bitboard,
|
||||
fetch_rook_attacks(all_occupancies, square),
|
||||
) || have_common_bit(
|
||||
opponent[Kind::Queen.idx()].bitboard,
|
||||
fetch_queen_attacks(all_occupancies, square),
|
||||
) || have_common_bit(
|
||||
opponent[Kind::King.idx()].bitboard,
|
||||
fetch_king_attacks(square),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn is_move_legit(&self, square: usize, opponent_color: Color) -> bool {
|
||||
@@ -129,17 +131,17 @@ impl Board {
|
||||
moves
|
||||
}
|
||||
pub fn pseudo_moves(&self, color: Color, kind: Kind) -> Vec<Move> {
|
||||
let all_occupancies = self.get_all_occupancies();
|
||||
let (pieces, enemy_occupancies, own_occupancies) = match color {
|
||||
let all_occupancies = self.all_occupancies();
|
||||
let (pieces, opponent_occupancies, own_occupancies) = match color {
|
||||
Color::White => (
|
||||
self.white_pieces[kind.idx()].bitboard,
|
||||
self.get_black_occupancies(),
|
||||
self.get_white_occupancies(),
|
||||
self.black_occupancies(),
|
||||
self.white_occupancies(),
|
||||
),
|
||||
Color::Black => (
|
||||
self.black_pieces[kind.idx()].bitboard,
|
||||
self.get_white_occupancies(),
|
||||
self.get_black_occupancies(),
|
||||
self.white_occupancies(),
|
||||
self.black_occupancies(),
|
||||
),
|
||||
};
|
||||
|
||||
@@ -147,8 +149,8 @@ impl Board {
|
||||
Kind::Pawn => pawn_pseudo_moves(
|
||||
pieces,
|
||||
all_occupancies,
|
||||
enemy_occupancies,
|
||||
self.state.get_en_passant_target_square(),
|
||||
opponent_occupancies,
|
||||
self.state.en_passant_target_square(),
|
||||
color,
|
||||
),
|
||||
Kind::Knight => knight_pseudo_moves(pieces, all_occupancies, own_occupancies),
|
||||
@@ -175,9 +177,9 @@ impl Board {
|
||||
Color::Black => &self.black_pieces,
|
||||
};
|
||||
|
||||
self.state.update_castling_state_quiet(mv.source, color);
|
||||
self.state.update_castling_state_quiet(mv.src, color);
|
||||
self.state
|
||||
.update_castling_state_capture(mv.target, Color::opponent_color(color));
|
||||
.update_castling_state_capture(mv.dst, Color::opponent_color(color));
|
||||
self.state.change_side();
|
||||
|
||||
let own_king_square = lsb(pieces[Kind::King.idx()].bitboard);
|
||||
@@ -192,52 +194,52 @@ impl Board {
|
||||
|
||||
match &mv.move_type {
|
||||
MoveType::Quiet => {
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::move_piece(mv.src, mv.dst, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Capture => {
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::remove_piece(mv.target, opponent_pieces);
|
||||
Self::move_piece(mv.src, mv.dst, own_pieces);
|
||||
Self::remove_piece(mv.dst, opponent_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::EnPassant => {
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::remove_pawn_enpassant(mv.target, opponent_pieces, color);
|
||||
Self::move_piece(mv.src, mv.dst, own_pieces);
|
||||
Self::remove_pawn_enpassant(mv.dst, opponent_pieces, color);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::DoublePush => {
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::move_piece(mv.src, mv.dst, own_pieces);
|
||||
let en_passant = match color {
|
||||
Color::White => Some(mv.source + 8),
|
||||
Color::Black => Some(mv.source - 8),
|
||||
Color::White => Some(mv.src + 8),
|
||||
Color::Black => Some(mv.src - 8),
|
||||
};
|
||||
self.state.set_en_passant_target_square(en_passant);
|
||||
}
|
||||
MoveType::Promotion(promote) => {
|
||||
Self::remove_piece(mv.source, own_pieces);
|
||||
Self::promote_piece(mv.target, own_pieces, *promote);
|
||||
Self::remove_piece(mv.src, own_pieces);
|
||||
Self::promote_piece(mv.dst, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::PromotionCapture(promote) => {
|
||||
Self::remove_piece(mv.source, own_pieces);
|
||||
Self::remove_piece(mv.target, opponent_pieces);
|
||||
Self::promote_piece(mv.target, own_pieces, *promote);
|
||||
Self::remove_piece(mv.src, own_pieces);
|
||||
Self::remove_piece(mv.dst, opponent_pieces);
|
||||
Self::promote_piece(mv.dst, own_pieces, *promote);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
}
|
||||
MoveType::Castle => {
|
||||
Self::move_piece(mv.source, mv.target, own_pieces);
|
||||
Self::move_rook_castle(mv.target, own_pieces);
|
||||
Self::move_piece(mv.src, mv.dst, own_pieces);
|
||||
Self::move_rook_castle(mv.dst, own_pieces);
|
||||
self.state.set_en_passant_target_square(None);
|
||||
self.state.set_castling_ability(color, Castle::None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn move_piece(source: usize, target: usize, pieces: &mut [Piece; 6]) {
|
||||
fn move_piece(src: usize, dst: usize, pieces: &mut [Piece; 6]) {
|
||||
for p in pieces.iter_mut() {
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(source)) {
|
||||
p.bitboard &= !square_to_bitboard(source);
|
||||
p.bitboard |= square_to_bitboard(target);
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(src)) {
|
||||
p.bitboard &= !square_to_bitboard(src);
|
||||
p.bitboard |= square_to_bitboard(dst);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -277,12 +279,7 @@ impl Board {
|
||||
Color::Black => square + 8,
|
||||
};
|
||||
|
||||
for p in pieces.iter_mut() {
|
||||
if have_common_bit(p.bitboard, square_to_bitboard(piece_to_remove)) {
|
||||
p.bitboard &= !(square_to_bitboard(piece_to_remove));
|
||||
break;
|
||||
}
|
||||
}
|
||||
Self::remove_piece(piece_to_remove, pieces);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,11 +346,11 @@ mod tests {
|
||||
const FEN_EXAMPLE: [&str; 1] = ["8/6P1/4n2b/1p6/1Kp5/6n1/4b3/1k6 w - - 0 1"];
|
||||
|
||||
#[test]
|
||||
fn test_get_occupancies() -> Result<(), String> {
|
||||
fn test_occupancies() -> Result<(), String> {
|
||||
let new_game = from_fen(FEN_EXAMPLE[0])?;
|
||||
assert_eq!(new_game.board.get_white_occupancies(), 0x40000002000000);
|
||||
assert_eq!(new_game.board.get_black_occupancies(), 0x900204401002);
|
||||
assert_eq!(new_game.board.get_all_occupancies(), 0x40900206401002);
|
||||
assert_eq!(new_game.board.white_occupancies(), 0x40000002000000);
|
||||
assert_eq!(new_game.board.black_occupancies(), 0x900204401002);
|
||||
assert_eq!(new_game.board.all_occupancies(), 0x40900206401002);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user