Use 'NUM_VARIANTS' where appropriate
This commit is contained in:
parent
8e688a0cac
commit
d74605ba5c
|
@ -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),
|
||||
|
|
|
@ -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<Square>,
|
||||
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -37,7 +37,7 @@ impl From<ValidationError> 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<Self, Self::Err> {
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue