From d74605ba5c34b1dbc2424cebc077b99d30378c3b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 3 Apr 2024 21:14:31 +0100 Subject: [PATCH] Use 'NUM_VARIANTS' where appropriate --- src/board/bitboard/mod.rs | 6 +++--- src/board/chess_board/builder.rs | 6 +++--- src/board/chess_board/mod.rs | 4 ++-- src/fen.rs | 6 +++--- src/movegen/moves.rs | 22 ++++++++++++---------- src/movegen/wizardry/mod.rs | 6 +++--- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index b0ec90a..9059235 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -1,4 +1,4 @@ -use super::Square; +use super::{File, Rank, Square}; use crate::utils::static_assert; mod error; @@ -21,7 +21,7 @@ impl Bitboard { pub const ALL: Bitboard = Bitboard(u64::MAX); /// Array of bitboards representing the eight ranks, in order from rank 1 to rank 8. - pub const RANKS: [Self; 8] = [ + pub const RANKS: [Self; Rank::NUM_VARIANTS] = [ Bitboard(0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001), Bitboard(0b00000010_00000010_00000010_00000010_00000010_00000010_00000010_00000010), Bitboard(0b00000100_00000100_00000100_00000100_00000100_00000100_00000100_00000100), @@ -33,7 +33,7 @@ impl Bitboard { ]; /// Array of bitboards representing the eight files, in order from file A to file H. - pub const FILES: [Self; 8] = [ + pub const FILES: [Self; File::NUM_VARIANTS] = [ Bitboard(0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_11111111), Bitboard(0b00000000_00000000_00000000_00000000_00000000_00000000_11111111_00000000), Bitboard(0b00000000_00000000_00000000_00000000_00000000_11111111_00000000_00000000), diff --git a/src/board/chess_board/builder.rs b/src/board/chess_board/builder.rs index 0304af5..679b3b7 100644 --- a/src/board/chess_board/builder.rs +++ b/src/board/chess_board/builder.rs @@ -4,7 +4,7 @@ use crate::board::{Bitboard, CastleRights, ChessBoard, Color, Piece, Square, Val #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct ChessBoardBuilder { /// The list of [Piece] on the board. Indexed by [Square::index]. - pieces: [Option<(Piece, Color)>; 64], + pieces: [Option<(Piece, Color)>; Square::NUM_VARIANTS], // Same fields as [ChessBoard]. castle_rights: [CastleRights; Color::NUM_VARIANTS], en_passant: Option, @@ -17,8 +17,8 @@ pub struct ChessBoardBuilder { impl ChessBoardBuilder { pub fn new() -> Self { Self { - pieces: [None; 64], - castle_rights: [CastleRights::NoSide; 2], + pieces: [None; Square::NUM_VARIANTS], + castle_rights: [CastleRights::NoSide; Color::NUM_VARIANTS], en_passant: Default::default(), half_move_clock: Default::default(), side: Color::White, diff --git a/src/board/chess_board/mod.rs b/src/board/chess_board/mod.rs index edf92a2..63dbe51 100644 --- a/src/board/chess_board/mod.rs +++ b/src/board/chess_board/mod.rs @@ -411,7 +411,7 @@ impl Default for ChessBoard { | Rank::Second.into_bitboard() | Rank::Seventh.into_bitboard() | Rank::Eighth.into_bitboard(), - castle_rights: [CastleRights::BothSides; 2], + castle_rights: [CastleRights::BothSides; Color::NUM_VARIANTS], en_passant: None, half_move_clock: 0, total_plies: 0, @@ -699,7 +699,7 @@ mod test { | Square::F3 | Square::G1 | Square::H2, - castle_rights: [CastleRights::NoSide; 2], + castle_rights: [CastleRights::NoSide; Color::NUM_VARIANTS], en_passant: None, half_move_clock: 0, total_plies: 0, diff --git a/src/fen.rs b/src/fen.rs index 41a0696..03c60c1 100644 --- a/src/fen.rs +++ b/src/fen.rs @@ -37,7 +37,7 @@ impl From for FenError { } /// Convert the castling rights segment of a FEN string to an array of [CastleRights]. -impl FromFen for [CastleRights; 2] { +impl FromFen for [CastleRights; Color::NUM_VARIANTS] { type Err = FenError; fn from_fen(s: &str) -> Result { @@ -45,7 +45,7 @@ impl FromFen for [CastleRights; 2] { return Err(FenError::InvalidFen); } - let mut res = [CastleRights::NoSide; 2]; + let mut res = [CastleRights::NoSide; Color::NUM_VARIANTS]; if s == "-" { return Ok(res); @@ -134,7 +134,7 @@ impl FromFen for ChessBoard { let mut builder = ChessBoardBuilder::new(); - let castle_rights = <[CastleRights; 2]>::from_fen(castling_rights)?; + let castle_rights = <[CastleRights; Color::NUM_VARIANTS]>::from_fen(castling_rights)?; for color in Color::iter() { builder.with_castle_rights(castle_rights[color.index()], color); } diff --git a/src/movegen/moves.rs b/src/movegen/moves.rs index 9840083..d46a733 100644 --- a/src/movegen/moves.rs +++ b/src/movegen/moves.rs @@ -13,12 +13,12 @@ use crate::{ // A pre-rolled RNG for magic bitboard generation, using pre-determined values. struct PreRolledRng { - numbers: [u64; 64], + numbers: [u64; Square::NUM_VARIANTS], current_index: usize, } impl PreRolledRng { - pub fn new(numbers: [u64; 64]) -> Self { + pub fn new(numbers: [u64; Square::NUM_VARIANTS]) -> Self { Self { numbers, current_index: 0, @@ -39,7 +39,8 @@ impl RandGen for PreRolledRng { /// Compute the set of possible non-attack moves for a pawn on a [Square], given its [Color] and /// set of blockers. pub fn pawn_quiet_moves(color: Color, square: Square, blockers: Bitboard) -> Bitboard { - static PAWN_MOVES: OnceLock<[[Bitboard; 64]; 2]> = OnceLock::new(); + static PAWN_MOVES: OnceLock<[[Bitboard; Square::NUM_VARIANTS]; Color::NUM_VARIANTS]> = + OnceLock::new(); // If there is a piece in front of the pawn, it can't advance if !(color.backward_direction().move_board(blockers) & square).is_empty() { @@ -47,7 +48,7 @@ pub fn pawn_quiet_moves(color: Color, square: Square, blockers: Bitboard) -> Bit } PAWN_MOVES.get_or_init(|| { - let mut res = [[Bitboard::EMPTY; 64]; 2]; + let mut res = [[Bitboard::EMPTY; Square::NUM_VARIANTS]; Color::NUM_VARIANTS]; for color in Color::iter() { for square in Square::iter() { res[color.index()][square.index()] = @@ -60,10 +61,11 @@ pub fn pawn_quiet_moves(color: Color, square: Square, blockers: Bitboard) -> Bit /// Compute the set of possible attacks for a pawn on a [Square], given its [Color]. pub fn pawn_attacks(color: Color, square: Square) -> Bitboard { - static PAWN_ATTACKS: OnceLock<[[Bitboard; 64]; 2]> = OnceLock::new(); + static PAWN_ATTACKS: OnceLock<[[Bitboard; Square::NUM_VARIANTS]; Color::NUM_VARIANTS]> = + OnceLock::new(); PAWN_ATTACKS.get_or_init(|| { - let mut res = [[Bitboard::EMPTY; 64]; 2]; + let mut res = [[Bitboard::EMPTY; Square::NUM_VARIANTS]; Color::NUM_VARIANTS]; for color in Color::iter() { for square in Square::iter() { res[color.index()][square.index()] = naive::pawn_captures(color, square); @@ -81,9 +83,9 @@ pub fn pawn_moves(color: Color, square: Square, blockers: Bitboard) -> Bitboard /// Compute the set of possible moves for a knight on a [Square]. pub fn knight_moves(square: Square) -> Bitboard { - static KNIGHT_MOVES: OnceLock<[Bitboard; 64]> = OnceLock::new(); + static KNIGHT_MOVES: OnceLock<[Bitboard; Square::NUM_VARIANTS]> = OnceLock::new(); KNIGHT_MOVES.get_or_init(|| { - let mut res = [Bitboard::EMPTY; 64]; + let mut res = [Bitboard::EMPTY; Square::NUM_VARIANTS]; for square in Square::iter() { res[square.index()] = naive::knight_moves(square) } @@ -122,9 +124,9 @@ pub fn queen_moves(square: Square, blockers: Bitboard) -> Bitboard { /// Compute the set of possible moves for a king on a [Square]. pub fn king_moves(square: Square) -> Bitboard { - static KING_MOVES: OnceLock<[Bitboard; 64]> = OnceLock::new(); + static KING_MOVES: OnceLock<[Bitboard; Square::NUM_VARIANTS]> = OnceLock::new(); KING_MOVES.get_or_init(|| { - let mut res = [Bitboard::EMPTY; 64]; + let mut res = [Bitboard::EMPTY; Square::NUM_VARIANTS]; for square in Square::iter() { res[square.index()] = naive::king_moves(square) } diff --git a/src/movegen/wizardry/mod.rs b/src/movegen/wizardry/mod.rs index f2794aa..6918d09 100644 --- a/src/movegen/wizardry/mod.rs +++ b/src/movegen/wizardry/mod.rs @@ -59,7 +59,7 @@ impl MagicMoves { // region:sourcegen /// A set of magic numbers for bishop move generation. -pub(crate) const BISHOP_SEED: [u64; 64] = [ +pub(crate) const BISHOP_SEED: [u64; Square::NUM_VARIANTS] = [ 4908958787341189172, 1157496606860279808, 289395876198088778, @@ -127,7 +127,7 @@ pub(crate) const BISHOP_SEED: [u64; 64] = [ ]; /// A set of magic numbers for rook move generation. -pub(crate) const ROOK_SEED: [u64; 64] = [ +pub(crate) const ROOK_SEED: [u64; Square::NUM_VARIANTS] = [ 2341871943948451840, 18015635528220736, 72066665545773824, @@ -250,7 +250,7 @@ mod test { )?; writeln!( &mut res, - "pub(crate) const {}_SEED: [u64; 64] = [", + "pub(crate) const {}_SEED: [u64; Square::NUM_VARIANTS] = [", piece_type.to_uppercase() )?; for magic in values {