2023-12-31 09:26:20 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
|
2024-01-24 09:16:21 -08:00
|
|
|
use crate::BitBoard;
|
2024-01-28 09:08:57 -08:00
|
|
|
use chessfriend_core::{Color, Direction, Square};
|
2024-02-05 13:59:26 -08:00
|
|
|
use std::sync::OnceLock;
|
2023-12-31 09:26:20 -08:00
|
|
|
|
2024-02-11 08:44:11 -07:00
|
|
|
pub(crate) const RANKS: [BitBoard; 8] = [
|
2023-12-31 09:26:20 -08:00
|
|
|
BitBoard(0xFF << 0 * 8),
|
|
|
|
|
BitBoard(0xFF << 1 * 8),
|
|
|
|
|
BitBoard(0xFF << 2 * 8),
|
|
|
|
|
BitBoard(0xFF << 3 * 8),
|
|
|
|
|
BitBoard(0xFF << 4 * 8),
|
|
|
|
|
BitBoard(0xFF << 5 * 8),
|
|
|
|
|
BitBoard(0xFF << 6 * 8),
|
|
|
|
|
BitBoard(0xFF << 7 * 8),
|
|
|
|
|
];
|
|
|
|
|
|
2024-02-11 08:44:11 -07:00
|
|
|
pub(crate) const FILES: [BitBoard; 8] = [
|
2023-12-31 09:26:20 -08:00
|
|
|
BitBoard(0x0101010101010101 << 0),
|
|
|
|
|
BitBoard(0x0101010101010101 << 1),
|
|
|
|
|
BitBoard(0x0101010101010101 << 2),
|
|
|
|
|
BitBoard(0x0101010101010101 << 3),
|
|
|
|
|
BitBoard(0x0101010101010101 << 4),
|
|
|
|
|
BitBoard(0x0101010101010101 << 5),
|
|
|
|
|
BitBoard(0x0101010101010101 << 6),
|
|
|
|
|
BitBoard(0x0101010101010101 << 7),
|
|
|
|
|
];
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-29 15:00:53 -08:00
|
|
|
/// Bitboards representing the kingside of the board, per color.
|
|
|
|
|
pub(crate) const KINGSIDES: [BitBoard; 2] =
|
|
|
|
|
[BitBoard(0xF0F0F0F0F0F0F0F0), BitBoard(0x0F0F0F0F0F0F0F0F)];
|
|
|
|
|
|
|
|
|
|
/// Bitboards representing the queenside of the board, per color.
|
|
|
|
|
pub(crate) const QUEENSIDES: [BitBoard; 2] =
|
|
|
|
|
[BitBoard(0x0F0F0F0F0F0F0F0F), BitBoard(0xF0F0F0F0F0F0F0F0)];
|
|
|
|
|
|
|
|
|
|
/// A bitboard representing the light squares.
|
|
|
|
|
pub(crate) const LIGHT_SQUARES: BitBoard =
|
2024-01-06 19:47:55 -08:00
|
|
|
BitBoard(0x5555 | 0x5555 << 16 | 0x5555 << 32 | 0x5555 << 48);
|
2024-01-29 15:00:53 -08:00
|
|
|
|
|
|
|
|
/// A bitboad representing the dark squares
|
|
|
|
|
pub(crate) const DARK_SQUARES: BitBoard = BitBoard(!LIGHT_SQUARES.0);
|
2024-01-01 09:25:31 -08:00
|
|
|
|
|
|
|
|
pub(super) fn library() -> &'static MoveLibrary {
|
2024-02-05 13:59:26 -08:00
|
|
|
static mut MOVE_LIBRARY: OnceLock<MoveLibrary> = OnceLock::new();
|
2024-01-02 08:42:47 -08:00
|
|
|
|
2024-01-01 09:25:31 -08:00
|
|
|
unsafe {
|
2024-02-05 13:59:26 -08:00
|
|
|
MOVE_LIBRARY.get_or_init(|| {
|
|
|
|
|
let mut library = MoveLibrary::new();
|
|
|
|
|
library.init();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-02-05 13:59:26 -08:00
|
|
|
library
|
|
|
|
|
})
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
macro_rules! library_getter {
|
|
|
|
|
($name:ident) => {
|
|
|
|
|
pub(super) fn $name(&self, sq: Square) -> BitBoard {
|
2024-01-06 16:22:22 -08:00
|
|
|
self.$name[sq as usize]
|
2024-01-02 08:42:47 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-01-01 09:25:31 -08:00
|
|
|
pub(super) struct MoveLibrary {
|
2024-01-02 08:42:47 -08:00
|
|
|
// Rays
|
2024-01-06 19:47:55 -08:00
|
|
|
rays: [[BitBoard; 8]; Square::NUM],
|
2024-01-01 09:25:31 -08:00
|
|
|
|
|
|
|
|
// Piecewise move tables
|
2024-01-28 09:08:57 -08:00
|
|
|
pawn_attacks: [[BitBoard; 64]; 2],
|
2024-01-28 10:25:01 -08:00
|
|
|
pawn_pushes: [[BitBoard; 64]; 2],
|
2024-01-01 09:25:31 -08:00
|
|
|
knight_moves: [BitBoard; 64],
|
2024-01-02 08:42:47 -08:00
|
|
|
bishop_moves: [BitBoard; 64],
|
|
|
|
|
rook_moves: [BitBoard; 64],
|
|
|
|
|
queen_moves: [BitBoard; 64],
|
|
|
|
|
king_moves: [BitBoard; 64],
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MoveLibrary {
|
|
|
|
|
const fn new() -> MoveLibrary {
|
|
|
|
|
MoveLibrary {
|
2024-01-06 19:47:55 -08:00
|
|
|
rays: [[BitBoard::empty(); 8]; Square::NUM],
|
2024-01-28 09:08:57 -08:00
|
|
|
pawn_attacks: [[BitBoard::empty(); 64]; 2],
|
2024-01-28 10:25:01 -08:00
|
|
|
pawn_pushes: [[BitBoard::empty(); 64]; 2],
|
2024-01-01 09:25:31 -08:00
|
|
|
knight_moves: [BitBoard::empty(); 64],
|
2024-01-02 08:42:47 -08:00
|
|
|
bishop_moves: [BitBoard::empty(); 64],
|
|
|
|
|
rook_moves: [BitBoard::empty(); 64],
|
|
|
|
|
queen_moves: [BitBoard::empty(); 64],
|
|
|
|
|
king_moves: [BitBoard::empty(); 64],
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
fn init(&mut self) {
|
2024-01-06 16:22:22 -08:00
|
|
|
for sq in Square::ALL {
|
2024-01-28 09:08:57 -08:00
|
|
|
self.init_pawn_moves(sq);
|
2024-01-06 16:22:22 -08:00
|
|
|
self.init_orthogonal_rays(sq);
|
|
|
|
|
self.init_diagonal_rays(sq);
|
|
|
|
|
self.init_knight_moves(sq as usize);
|
2024-01-06 19:47:55 -08:00
|
|
|
self.init_bishop_moves(sq);
|
|
|
|
|
self.init_rook_moves(sq);
|
|
|
|
|
self.init_queen_moves(sq);
|
2024-01-06 16:22:22 -08:00
|
|
|
self.init_king_moves(sq as usize);
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
2024-01-02 08:42:47 -08:00
|
|
|
}
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-06 16:22:22 -08:00
|
|
|
fn init_orthogonal_rays(&mut self, sq: Square) {
|
|
|
|
|
let sq_bb: BitBoard = sq.into();
|
2024-01-06 19:47:55 -08:00
|
|
|
let rays = &mut self.rays[sq as usize];
|
|
|
|
|
rays[Direction::North as usize] = Self::generate_ray(sq_bb, BitBoard::shift_north_one);
|
|
|
|
|
rays[Direction::South as usize] = Self::generate_ray(sq_bb, BitBoard::shift_south_one);
|
|
|
|
|
rays[Direction::East as usize] = Self::generate_ray(sq_bb, BitBoard::shift_east_one);
|
|
|
|
|
rays[Direction::West as usize] = Self::generate_ray(sq_bb, BitBoard::shift_west_one);
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-06 16:22:22 -08:00
|
|
|
fn init_diagonal_rays(&mut self, sq: Square) {
|
|
|
|
|
let sq_bb: BitBoard = sq.into();
|
2024-01-06 19:47:55 -08:00
|
|
|
let rays = &mut self.rays[sq as usize];
|
|
|
|
|
rays[Direction::NorthEast as usize] =
|
|
|
|
|
Self::generate_ray(sq_bb, BitBoard::shift_north_east_one);
|
|
|
|
|
rays[Direction::NorthWest as usize] =
|
|
|
|
|
Self::generate_ray(sq_bb, BitBoard::shift_north_west_one);
|
|
|
|
|
rays[Direction::SouthWest as usize] =
|
|
|
|
|
Self::generate_ray(sq_bb, BitBoard::shift_south_west_one);
|
|
|
|
|
rays[Direction::SouthEast as usize] =
|
|
|
|
|
Self::generate_ray(sq_bb, BitBoard::shift_south_east_one);
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
fn init_king_moves(&mut self, idx: usize) {
|
|
|
|
|
let king = BitBoard::new(1 << idx);
|
|
|
|
|
let mut attacks = king.shift_east_one() | king.shift_west_one();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
let king = king | attacks;
|
|
|
|
|
attacks |= king.shift_north_one() | king.shift_south_one();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
self.king_moves[idx] = attacks;
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Calculate bitboards representing knight moves from each square on the
|
|
|
|
|
/// board. The algorithm is described on the [Chess Programming Wiki][cpw].
|
|
|
|
|
///
|
|
|
|
|
/// [cpw]: https://www.chessprogramming.org/Knight_Pattern
|
2024-01-02 08:42:47 -08:00
|
|
|
fn init_knight_moves(&mut self, idx: usize) {
|
|
|
|
|
let knight = BitBoard::new(1 << idx);
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
let east = knight.shift_east_one();
|
|
|
|
|
let west = knight.shift_west_one();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
let mut attacks = (east | west).shift_north(2);
|
|
|
|
|
attacks |= (east | west).shift_south(2);
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
let east = east.shift_east_one();
|
|
|
|
|
let west = west.shift_west_one();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
attacks |= (east | west).shift_north_one();
|
|
|
|
|
attacks |= (east | west).shift_south_one();
|
2024-01-01 09:25:31 -08:00
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
self.knight_moves[idx] = attacks;
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-06 19:47:55 -08:00
|
|
|
fn init_bishop_moves(&mut self, sq: Square) {
|
|
|
|
|
let rays = self.rays[sq as usize];
|
|
|
|
|
self.bishop_moves[sq as usize] = rays[Direction::NorthWest as usize]
|
|
|
|
|
| rays[Direction::NorthEast as usize]
|
|
|
|
|
| rays[Direction::SouthEast as usize]
|
|
|
|
|
| rays[Direction::SouthWest as usize];
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|
2024-01-02 08:42:47 -08:00
|
|
|
|
2024-01-06 19:47:55 -08:00
|
|
|
fn init_rook_moves(&mut self, sq: Square) {
|
|
|
|
|
let rays = self.rays[sq as usize];
|
|
|
|
|
self.rook_moves[sq as usize] = rays[Direction::North as usize]
|
|
|
|
|
| rays[Direction::East as usize]
|
|
|
|
|
| rays[Direction::South as usize]
|
|
|
|
|
| rays[Direction::West as usize];
|
2024-01-02 08:42:47 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-06 19:47:55 -08:00
|
|
|
fn init_queen_moves(&mut self, sq: Square) {
|
|
|
|
|
let rook_moves = self.rook_moves[sq as usize];
|
|
|
|
|
let bishop_moves = self.bishop_moves[sq as usize];
|
|
|
|
|
self.queen_moves[sq as usize] = rook_moves | bishop_moves;
|
2024-01-02 08:42:47 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-28 09:08:57 -08:00
|
|
|
fn init_pawn_moves(&mut self, sq: Square) {
|
|
|
|
|
let bitboard: BitBoard = sq.into();
|
2024-01-28 10:25:01 -08:00
|
|
|
|
2024-01-28 09:08:57 -08:00
|
|
|
self.pawn_attacks[Color::White as usize][sq as usize] =
|
|
|
|
|
bitboard.shift_north_west_one() | bitboard.shift_north_east_one();
|
|
|
|
|
self.pawn_attacks[Color::Black as usize][sq as usize] =
|
|
|
|
|
bitboard.shift_south_west_one() | bitboard.shift_south_east_one();
|
2024-01-28 10:25:01 -08:00
|
|
|
|
|
|
|
|
self.pawn_pushes[Color::White as usize][sq as usize] = {
|
|
|
|
|
let mut push = bitboard.shift_north_one();
|
|
|
|
|
if !(bitboard & RANKS[1]).is_empty() {
|
|
|
|
|
push |= push.shift_north_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
push
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.pawn_pushes[Color::Black as usize][sq as usize] = {
|
|
|
|
|
let mut push = bitboard.shift_south_one();
|
|
|
|
|
if !(bitboard & RANKS[6]).is_empty() {
|
|
|
|
|
push |= push.shift_south_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
push
|
|
|
|
|
};
|
2024-01-28 09:08:57 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
#[inline]
|
2024-01-19 18:08:41 -08:00
|
|
|
fn generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard {
|
2024-01-02 08:42:47 -08:00
|
|
|
let mut ray = BitBoard::empty();
|
|
|
|
|
|
2024-01-19 18:08:41 -08:00
|
|
|
let mut iter = shift(&sq);
|
2024-01-02 08:42:47 -08:00
|
|
|
while !iter.is_empty() {
|
|
|
|
|
ray |= iter;
|
2024-01-19 18:08:41 -08:00
|
|
|
iter = shift(&iter);
|
2024-01-02 08:42:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ray
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 08:42:19 -08:00
|
|
|
pub(super) fn ray(&self, sq: Square, dir: Direction) -> &BitBoard {
|
|
|
|
|
&self.rays[sq as usize][dir as usize]
|
2024-01-06 19:47:55 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-28 10:25:01 -08:00
|
|
|
pub(super) fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard {
|
|
|
|
|
self.pawn_pushes[color as usize][sq as usize]
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-28 09:08:57 -08:00
|
|
|
pub(super) fn pawn_attacks(&self, sq: Square, color: Color) -> BitBoard {
|
|
|
|
|
self.pawn_attacks[color as usize][sq as usize]
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 08:42:47 -08:00
|
|
|
library_getter!(knight_moves);
|
|
|
|
|
library_getter!(bishop_moves);
|
|
|
|
|
library_getter!(rook_moves);
|
|
|
|
|
library_getter!(queen_moves);
|
|
|
|
|
library_getter!(king_moves);
|
2024-01-01 09:25:31 -08:00
|
|
|
}
|