diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-02 20:51:25 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-02 20:51:53 +0300 |
commit | cae14cd6f19c40be2c30b638a51a02c4da0959ea (patch) | |
tree | 5e3cc85e5d779beeaaaa837eefed97835b89dca5 | |
parent | ec05e8bfa3f61b86fb83d939ff6fa3ddbc51ce6d (diff) | |
download | chessnost-cae14cd6f19c40be2c30b638a51a02c4da0959ea.tar.gz |
refactor: use functions for file and rank
-rw-r--r-- | src/attacks.rs | 6 | ||||
-rw-r--r-- | src/board/mod.rs | 5 | ||||
-rw-r--r-- | src/moves.rs | 13 | ||||
-rw-r--r-- | src/square.rs | 4 |
4 files changed, 8 insertions, 20 deletions
diff --git a/src/attacks.rs b/src/attacks.rs index 1074037..eafaf01 100644 --- a/src/attacks.rs +++ b/src/attacks.rs @@ -253,7 +253,7 @@ impl Attacks { /// Given a square and occupancy masked for rank, diagonal or anti-diagonal (note: not a file!) /// return an attack bitboard that considers blocking pieces fn kindergarten_attacks_base(&self, occupancy: Bitboard, mask: Bitboard, square: Square) -> Bitboard { - let file = square as u8 % 8; + let file = square.file(); let masked_occupancy = occupancy & mask; let occupancy_rank = ((masked_occupancy as u128 * B_FILE as u128) >> 58 & 0b111111) << 1; let rank_attacks = self.first_rank_attacks[occupancy_rank as usize][file as usize] as Bitboard; @@ -267,8 +267,8 @@ impl Attacks { /// https://www.chessprogramming.org/Kindergarten_Bitboards fn kindergarten_attacks_file(&self, occupancy: Bitboard, mask: Bitboard, square: Square) -> Bitboard { - let file = square as u8 % 8; - let rank = square as u8 / 8; + let file = square.file(); + let rank = square.rank(); let masked_occupancy = (occupancy & mask) >> file; // Shift occupancy to A file let occupancy_rank = ((masked_occupancy as u128 * DIAG_C2_H7 as u128) >> 58 & 0b111111) << 1; diff --git a/src/board/mod.rs b/src/board/mod.rs index 2bb1147..a1166d6 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -255,7 +255,7 @@ impl Board { } pub fn generate_pseudolegal_moves(&self, color: Color) -> Vec<Move> { - let mut moves = Vec::with_capacity(1024); + let mut moves = Vec::with_capacity(256); let capture_targets = self.color_occupancy(color.flip()) ^ match color { // Exclude opponent king because we can't capture it Color::White => self.pieces[PieceType::KingBlack as usize], @@ -555,10 +555,11 @@ impl Board { } fn piece_by_square(&self, square: Square) -> Option<PieceType> { + let square_bb = square.to_bitboard(); self.pieces .iter() .enumerate() - .find(|(_, bitboard)| *bitboard & square.to_bitboard() > 0) + .find(|(_, bitboard)| *bitboard & square_bb > 0) .and_then(|(pt, _)| Some(PieceType::from(pt))) } diff --git a/src/moves.rs b/src/moves.rs index 34f8370..57103d0 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -32,16 +32,3 @@ impl Move { } } } - - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn mock() { - let mov = Move { source: Square::E2, target: Square::E4, kind: MoveKind::Quiet }; - println!("{:?}", mov); - } -} diff --git a/src/square.rs b/src/square.rs index 2e47bea..8a268ea 100644 --- a/src/square.rs +++ b/src/square.rs @@ -27,11 +27,11 @@ impl Square { } /// 0-based rank pub fn rank(&self) -> u8 { - *self as u8 / 8 + *self as u8 >> 3 } /// 0-based file pub fn file(&self) -> u8 { - *self as u8 % 8 + *self as u8 & 7 } pub fn nort_one(&self) -> Self { |