From 6badbcd776f78b1fcc88e8d534bfcc1f6811f7ca Mon Sep 17 00:00:00 2001 From: stefiosif Date: Sun, 15 Sep 2024 11:25:22 +0300 Subject: [PATCH] Rename movegen module files and make readability improvements --- src/board/board.rs | 10 ++--- src/interface/uci.rs | 2 +- src/main.rs | 2 +- .../{attack.rs => attack_generator.rs} | 2 +- src/movegen/{magic.rs => magic_bitboards.rs} | 42 ++++++++++--------- src/movegen/mod.rs | 6 +-- src/movegen/move.rs | 6 +-- src/movegen/{movegen.rs => move_generator.rs} | 4 +- src/search/move_ordering.rs | 13 +++--- src/search/negamax.rs | 2 +- src/search/perft.rs | 2 +- src/search/quiescence.rs | 17 +++++--- 12 files changed, 57 insertions(+), 51 deletions(-) rename src/movegen/{attack.rs => attack_generator.rs} (99%) rename src/movegen/{magic.rs => magic_bitboards.rs} (84%) rename src/movegen/{movegen.rs => move_generator.rs} (99%) diff --git a/src/board/board.rs b/src/board/board.rs index 3119a9e..b7ec29d 100644 --- a/src/board/board.rs +++ b/src/board/board.rs @@ -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::*; diff --git a/src/interface/uci.rs b/src/interface/uci.rs index 1446f4a..fa3252a 100644 --- a/src/interface/uci.rs +++ b/src/interface/uci.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index c3dd3a6..e316e36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(), diff --git a/src/movegen/attack.rs b/src/movegen/attack_generator.rs similarity index 99% rename from src/movegen/attack.rs rename to src/movegen/attack_generator.rs index 5d96a75..e07739c 100644 --- a/src/movegen/attack.rs +++ b/src/movegen/attack_generator.rs @@ -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 { diff --git a/src/movegen/magic.rs b/src/movegen/magic_bitboards.rs similarity index 84% rename from src/movegen/magic.rs rename to src/movegen/magic_bitboards.rs index f6ea00b..901b6cd 100644 --- a/src/movegen/magic.rs +++ b/src/movegen/magic_bitboards.rs @@ -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( +fn _find_magic_numbers( 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]); } } diff --git a/src/movegen/mod.rs b/src/movegen/mod.rs index c6a70ac..58f9787 100644 --- a/src/movegen/mod.rs +++ b/src/movegen/mod.rs @@ -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; diff --git a/src/movegen/move.rs b/src/movegen/move.rs index c94d6ac..c48070e 100644 --- a/src/movegen/move.rs +++ b/src/movegen/move.rs @@ -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) diff --git a/src/movegen/movegen.rs b/src/movegen/move_generator.rs similarity index 99% rename from src/movegen/movegen.rs rename to src/movegen/move_generator.rs index 86d7f87..9ae6b26 100644 --- a/src/movegen/movegen.rs +++ b/src/movegen/move_generator.rs @@ -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; diff --git a/src/search/move_ordering.rs b/src/search/move_ordering.rs index 1f8974c..5b8b113 100644 --- a/src/search/move_ordering.rs +++ b/src/search/move_ordering.rs @@ -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 { diff --git a/src/search/negamax.rs b/src/search/negamax.rs index 5e2f894..2ca60ae 100644 --- a/src/search/negamax.rs +++ b/src/search/negamax.rs @@ -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; diff --git a/src/search/perft.rs b/src/search/perft.rs index c5ab95e..c7b76af 100644 --- a/src/search/perft.rs +++ b/src/search/perft.rs @@ -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; diff --git a/src/search/quiescence.rs b/src/search/quiescence.rs index c186342..4ab13be 100644 --- a/src/search/quiescence.rs +++ b/src/search/quiescence.rs @@ -18,12 +18,17 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option, 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));