Rename movegen module files and make readability improvements

This commit is contained in:
stefiosif
2024-09-15 11:25:22 +03:00
parent 062a2fe903
commit 6badbcd776
12 changed files with 57 additions and 51 deletions

View File

@@ -2,11 +2,11 @@ use u64 as Bitboard;
use crate::board::bitboard::{have_common_bit, lsb};
use crate::board::state::State;
use crate::movegen::attack::{
use crate::movegen::attack_generator::{
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
fetch_queen_attacks, fetch_rook_attacks,
};
use crate::movegen::movegen::{
use crate::movegen::move_generator::{
bishop_pseudo_moves, king_pseudo_moves, knight_pseudo_moves, pawn_pseudo_moves,
queen_pseudo_moves, rook_pseudo_moves,
};
@@ -185,7 +185,7 @@ impl Board {
return Some(at_black);
}
return None;
None
}
pub fn make_move(&mut self, mv: &Move) {
@@ -256,7 +256,7 @@ impl Board {
move_parameters.promoted_piece,
) {
let (own_pieces, opponent_pieces) = self.all_pieces();
opponent_pieces[captured_piece_type].bitboard |= square_to_bitboard(mv.dst);
own_pieces[promoted_piece_type].bitboard &= !square_to_bitboard(mv.dst);
own_pieces[PieceType::Pawn].bitboard |= square_to_bitboard(mv.src);
@@ -474,7 +474,7 @@ impl Color {
mod tests {
use crate::{
board::fen::from_fen,
movegen::{attack::init_attacks, r#move::MoveType},
movegen::{attack_generator::init_attacks, r#move::MoveType},
};
use super::*;

View File

@@ -194,7 +194,7 @@ mod tests {
use crate::{
board::{fen::from_fen, square::Square},
interface::uci::{parse_command, Command},
movegen::{attack::init_attacks, r#move::Move},
movegen::{attack_generator::init_attacks, r#move::Move},
};
use super::uci_go;

View File

@@ -23,7 +23,7 @@ pub enum Mode {
}
fn main() {
movegen::attack::init_attacks();
movegen::attack_generator::init_attacks();
match Args::parse().mode {
Mode::Perft => search::perft::perftree_script(),

View File

@@ -224,7 +224,7 @@ pub fn fetch_king_attacks(square: usize) -> Bitboard {
unsafe { KING_ATTACKS[square] }
}
use crate::movegen::magic::{BISHOP_MAGIC, ROOK_MAGIC};
use crate::movegen::magic_bitboards::{BISHOP_MAGIC, ROOK_MAGIC};
pub fn fetch_bishop_attacks(mut occupancy: Bitboard, square: usize) -> Bitboard {
unsafe {

View File

@@ -1,6 +1,6 @@
use crate::board::bitboard::{bit_count, square_to_bitboard, EMPTY, RANK_8};
use crate::board::square::Square;
use crate::movegen::attack::{
use crate::movegen::attack_generator::{
bishop_attacks_on_the_fly, mask_bishop_attacks, mask_rook_attacks, rook_attacks_on_the_fly,
set_occupancy, BISHOP_RELEVANT_BITS, ROOK_RELEVANT_BITS,
};
@@ -8,8 +8,8 @@ use rand::rngs::SmallRng;
use rand::{RngCore, SeedableRng};
use u64 as Bitboard;
static mut BISHOP_MAGIC_INIT: [Bitboard; 64] = [0; 64];
static mut ROOK_MAGIC_INIT: [Bitboard; 64] = [0; 64];
static mut _BISHOP_MAGIC_INIT: [Bitboard; 64] = [0; 64];
static mut _ROOK_MAGIC_INIT: [Bitboard; 64] = [0; 64];
#[rustfmt::skip]
pub const ROOK_MAGIC: [u64; 64] = [
@@ -51,7 +51,7 @@ pub const BISHOP_MAGIC: [u64; 64] = [
0x4000000004208200, 0x804a10011602, 0x200a24c410041500, 0x8408080088061020,
];
fn random_uint64(state: &mut SmallRng) -> u64 {
fn _random_uint64(state: &mut SmallRng) -> u64 {
let n1 = state.next_u32() as u64 & 0xffff;
let n2 = state.next_u32() as u64 & 0xffff;
let n3 = state.next_u32() as u64 & 0xffff;
@@ -60,15 +60,15 @@ fn random_uint64(state: &mut SmallRng) -> u64 {
n1 | (n2 << (16)) | (n3 << (32)) | (n4 << (48))
}
fn generate_magic_number_candidate(state: &mut SmallRng) -> u64 {
random_uint64(state) & random_uint64(state) & random_uint64(state)
fn __generate_magic_number_candidate(state: &mut SmallRng) -> u64 {
_random_uint64(state) & _random_uint64(state) & _random_uint64(state)
}
fn init_magic_arrays() {
fn _init_magic_arrays() {
let mut state = SmallRng::seed_from_u64(1804289383);
for square in Square::A1..=Square::H8 {
let rook_magic_number = find_magic_numbers(
let rook_magic_number = _find_magic_numbers(
ROOK_RELEVANT_BITS[square],
square_to_bitboard(square),
&mut state,
@@ -76,11 +76,11 @@ fn init_magic_arrays() {
rook_attacks_on_the_fly,
);
unsafe {
ROOK_MAGIC_INIT[square] = rook_magic_number;
_ROOK_MAGIC_INIT[square] = rook_magic_number;
}
}
for square in Square::A1..=Square::H8 {
let bishop_magic_number = find_magic_numbers(
let bishop_magic_number = _find_magic_numbers(
BISHOP_RELEVANT_BITS[square],
square_to_bitboard(square),
&mut state,
@@ -88,12 +88,12 @@ fn init_magic_arrays() {
bishop_attacks_on_the_fly,
);
unsafe {
BISHOP_MAGIC_INIT[square] = bishop_magic_number;
_BISHOP_MAGIC_INIT[square] = bishop_magic_number;
}
}
}
fn find_magic_numbers<F1, F2>(
fn _find_magic_numbers<F1, F2>(
relevant_bits: usize,
bitboard: Bitboard,
state: &mut SmallRng,
@@ -115,7 +115,7 @@ where
attacks[index as usize] = attacks_on_the_fly(bitboard, occupancies[index as usize]);
}
generate_magic_number(
_generate_magic_number(
relevant_bits,
state,
attack_mask,
@@ -125,7 +125,7 @@ where
)
}
fn generate_magic_number(
fn _generate_magic_number(
relevant_bits: usize,
state: &mut SmallRng,
attack_mask: u64,
@@ -134,7 +134,7 @@ fn generate_magic_number(
attacks: [u64; 4096],
) -> u64 {
for _ in 0..100000000 {
let magic_number = generate_magic_number_candidate(state);
let magic_number = __generate_magic_number_candidate(state);
if 6 > bit_count(attack_mask.wrapping_mul(magic_number) & RANK_8) {
continue;
}
@@ -163,20 +163,22 @@ fn generate_magic_number(
#[cfg(test)]
mod tests {
use super::init_magic_arrays;
use super::_init_magic_arrays;
use crate::{
board::square::Square,
movegen::magic::{BISHOP_MAGIC, BISHOP_MAGIC_INIT, ROOK_MAGIC, ROOK_MAGIC_INIT},
movegen::magic_bitboards::{
BISHOP_MAGIC, ROOK_MAGIC, _BISHOP_MAGIC_INIT, _ROOK_MAGIC_INIT,
},
};
#[test]
fn test_init_magic_arrays() -> Result<(), String> {
init_magic_arrays();
_init_magic_arrays();
for index in Square::A1..=Square::H8 {
unsafe {
assert_eq!(ROOK_MAGIC[index], ROOK_MAGIC_INIT[index]);
assert_eq!(BISHOP_MAGIC[index], BISHOP_MAGIC_INIT[index]);
assert_eq!(ROOK_MAGIC[index], _ROOK_MAGIC_INIT[index]);
assert_eq!(BISHOP_MAGIC[index], _BISHOP_MAGIC_INIT[index]);
}
}

View File

@@ -1,4 +1,4 @@
pub mod attack;
pub mod magic;
pub mod attack_generator;
pub mod magic_bitboards;
pub mod r#move;
pub mod movegen;
pub mod move_generator;

View File

@@ -146,10 +146,8 @@ impl Move {
}
}
if moving == Some(PieceType::King) {
if src.abs_diff(dst) == 2 {
return Self::new_with_type(src, dst, MoveType::Castle);
}
if moving == Some(PieceType::King) && src.abs_diff(dst) == 2 {
return Self::new_with_type(src, dst, MoveType::Castle);
}
Self::new(src, dst)

View File

@@ -4,7 +4,7 @@ use crate::board::bitboard::{
};
use crate::board::board::{Board, Color};
use crate::board::state::Castle;
use crate::movegen::attack::{
use crate::movegen::attack_generator::{
fetch_bishop_attacks, fetch_king_attacks, fetch_knight_attacks, fetch_pawn_attacks,
fetch_queen_attacks, fetch_rook_attacks,
};
@@ -395,7 +395,7 @@ fn king_and_adj_square_safety(board: &Board, color: Color) -> Castle {
#[cfg(test)]
mod tests {
use crate::{board::fen::from_fen, movegen::attack::init_attacks};
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
use super::*;
use crate::board::board::PieceType;

View File

@@ -2,13 +2,14 @@ use crate::{board::board::Board, movegen::r#move::Move};
// Rows: Aggressors (P, N, B, R, Q, K)
// Columns: Victims (K, Q, R, B, N, P)
#[rustfmt::skip]
const MVV_LVA: [[usize; 6]; 6] = [
[30, 29, 28, 27, 26, 00],
[25, 24, 23, 22, 21, 00],
[20, 19, 18, 17, 16, 00],
[15, 14, 13, 12, 11, 00],
[10, 09, 08, 07, 06, 00],
[05, 04, 03, 02, 01, 00],
[30, 29, 28, 27, 26, 0],
[25, 24, 23, 22, 21, 0],
[20, 19, 18, 17, 16, 0],
[15, 14, 13, 12, 11, 0],
[10, 9, 8, 7, 6, 0],
[5, 4, 3, 2, 1, 0],
];
pub fn score_by_mvv_lva(board: &Board, mv: Move) -> usize {

View File

@@ -63,7 +63,7 @@ pub fn negamax(
mod tests {
use crate::board::{fen::from_fen, square::Square};
use crate::evaluation::{MAX_SCORE, MIN_SCORE};
use crate::movegen::attack::init_attacks;
use crate::movegen::attack_generator::init_attacks;
use crate::movegen::r#move::Move;
use crate::search::negamax::negamax;

View File

@@ -58,7 +58,7 @@ pub fn _square_to_notation(square: u8) -> &'static str {
#[cfg(test)]
mod tests {
use crate::{board::fen::from_fen, movegen::attack::init_attacks};
use crate::{board::fen::from_fen, movegen::attack_generator::init_attacks};
use super::driver;

View File

@@ -18,12 +18,17 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option<Move>,
alpha = stand_pat;
}
let mut captures: Vec<_> = game.board.pseudo_moves_all().into_iter().filter(|m| {
matches!(
m.move_type,
MoveType::Capture | MoveType::PromotionCapture(_)
)
}).collect();
let mut captures: Vec<_> = game
.board
.pseudo_moves_all()
.into_iter()
.filter(|m| {
matches!(
m.move_type,
MoveType::Capture | MoveType::PromotionCapture(_)
)
})
.collect();
captures.sort_unstable_by_key(|mv| score_by_mvv_lva(&game.board, *mv));