diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 05:02:30 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 05:10:48 +0300 |
commit | 9a41a379a8a1f33b6a9d28f1d5ff3c1d0826e1f5 (patch) | |
tree | 58a7ec5f6bdbc701436c011e82a5b6817483bb1c /src/attacks.rs | |
parent | c61c22208de60465db7f9cf58cdfa5d204826daf (diff) | |
download | chessnost-9a41a379a8a1f33b6a9d28f1d5ff3c1d0826e1f5.tar.gz |
refactor: use Square as type everywhere
Diffstat (limited to 'src/attacks.rs')
-rw-r--r-- | src/attacks.rs | 106 |
1 files changed, 53 insertions, 53 deletions
diff --git a/src/attacks.rs b/src/attacks.rs index 7fc6b7d..a9947bc 100644 --- a/src/attacks.rs +++ b/src/attacks.rs @@ -1,4 +1,4 @@ -use crate::{bitboard::Bitboard, board::Color}; +use crate::{bitboard::Bitboard, board::Color, square::Square}; static NOT_A_FILE: Bitboard = 0xFEFEFEFEFEFEFEFE; static NOT_B_FILE: Bitboard = 0xFDFDFDFDFDFDFDFD; @@ -248,8 +248,8 @@ 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: u8) -> Bitboard { - let file = square % 8; + fn kindergarten_attacks_base(&self, occupancy: Bitboard, mask: Bitboard, square: Square) -> Bitboard { + let file = square as u8 % 8; 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; @@ -262,9 +262,9 @@ impl Attacks { } /// https://www.chessprogramming.org/Kindergarten_Bitboards - fn kindergarten_attacks_file(&self, occupancy: Bitboard, mask: Bitboard, square: u8) -> Bitboard { - let file = square % 8; - let rank = square / 8; + 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 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; @@ -274,7 +274,7 @@ impl Attacks { ((rank_attacks as u128 * DIAG_A1_H8 as u128) as Bitboard & H_FILE) >> (7 - file) } - pub fn bishop(&self, occupancy: Bitboard, square: u8) -> Bitboard { + pub fn bishop(&self, occupancy: Bitboard, square: Square) -> Bitboard { let diagonal_mask = self.ray_attacks[Direction::NoEa as usize][square as usize] | self.ray_attacks[Direction::SoWe as usize][square as usize]; @@ -285,7 +285,7 @@ impl Attacks { self.kindergarten_attacks_base(occupancy, diagonal_mask, square) | self.kindergarten_attacks_base(occupancy, anti_diagonal_mask, square) } - pub fn rook(&self, occupancy: Bitboard, square: u8) -> Bitboard { + pub fn rook(&self, occupancy: Bitboard, square: Square) -> Bitboard { let vertical = self.ray_attacks[Direction::Nort as usize][square as usize] | self.ray_attacks[Direction::Sout as usize][square as usize]; @@ -296,14 +296,14 @@ impl Attacks { self.kindergarten_attacks_file(occupancy, vertical, square) | self.kindergarten_attacks_base(occupancy, horizontal, square) } - pub fn queen(&self, occupancy: Bitboard, square: u8) -> Bitboard { + pub fn queen(&self, occupancy: Bitboard, square: Square) -> Bitboard { self.rook(occupancy, square) | self.bishop(occupancy, square) } } #[cfg(test)] mod tests { - use crate::{bitboard::{pop_count, print}, board::Square}; + use crate::{bitboard::{pop_count, print}, square::Square}; use super::*; @@ -321,10 +321,10 @@ mod tests { print(white_attacks, "Pawn e4"); - assert_eq!(white_attacks, 1 << Square::D5 as usize | 1 << Square::F5 as usize); + assert_eq!(white_attacks, Square::D5.to_bitboard() | Square::F5.to_bitboard()); - assert_eq!(attacks[Color::White as usize][Square::H4 as usize], 1 << Square::G5 as usize); - assert_eq!(attacks[Color::White as usize][Square::A4 as usize], 1 << Square::B5 as usize); + assert_eq!(attacks[Color::White as usize][Square::H4 as usize], Square::G5.to_bitboard()); + assert_eq!(attacks[Color::White as usize][Square::A4 as usize], Square::B5.to_bitboard()); assert_eq!(pop_count(attacks[Color::White as usize][Square::E8 as usize]), 0); assert_eq!(pop_count(attacks[Color::Black as usize][Square::E1 as usize]), 0); @@ -333,10 +333,10 @@ mod tests { #[test] fn test_pawn_pushes() { let pushes = Attacks::precompute_pawn_pushes(); - assert_eq!(pushes[Color::White as usize][Square::E4 as usize], 1 << Square::E5 as usize); - assert_eq!(pushes[Color::White as usize][Square::A2 as usize], 1 << Square::A3 as usize | 1 << Square::A4 as usize); - assert_eq!(pushes[Color::Black as usize][Square::E4 as usize], 1 << Square::E3 as usize); - assert_eq!(pushes[Color::Black as usize][Square::H6 as usize], 1 << Square::H5 as usize | 1 << Square::H4 as usize); + assert_eq!(pushes[Color::White as usize][Square::E4 as usize], Square::E5.to_bitboard()); + assert_eq!(pushes[Color::White as usize][Square::A2 as usize], Square::A3.to_bitboard() | Square::A4.to_bitboard()); + assert_eq!(pushes[Color::Black as usize][Square::E4 as usize], Square::E3.to_bitboard()); + assert_eq!(pushes[Color::Black as usize][Square::H6 as usize], Square::H5.to_bitboard() | Square::H4.to_bitboard()); } #[test] @@ -346,18 +346,18 @@ mod tests { print(e4_attacks, "Knight e4"); - assert_ne!(e4_attacks & 1 << Square::G5 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::G3 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::C5 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::C3 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::D2 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::F2 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::D6 as usize, 0); - assert_ne!(e4_attacks & 1 << Square::F6 as usize, 0); + assert_ne!(e4_attacks & Square::G5.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::G3.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::C5.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::C3.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::D2.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::F2.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::D6.to_bitboard(), 0); + assert_ne!(e4_attacks & Square::F6.to_bitboard(), 0); - assert_eq!(e4_attacks & 1 << Square::E5 as usize, 0); - assert_eq!(e4_attacks & 1 << Square::D4 as usize, 0); - assert_eq!(e4_attacks & 1 << Square::A1 as usize, 0); + assert_eq!(e4_attacks & Square::E5.to_bitboard(), 0); + assert_eq!(e4_attacks & Square::D4.to_bitboard(), 0); + assert_eq!(e4_attacks & Square::A1.to_bitboard(), 0); assert_eq!(pop_count(attacks[Square::G1 as usize]), 3); assert_eq!(pop_count(attacks[Square::H8 as usize]), 2); @@ -418,52 +418,52 @@ mod tests { #[test] fn test_bishop_attacks() { let attacks = Attacks::new(); - let square = Square::E4 as u8; + let square = Square::E4; let bb = attacks.bishop(DEFAULT_OCCUPANCY, square); print(DEFAULT_OCCUPANCY, "Occupancy"); print(bb, "Bishop e4"); - assert_ne!(bb & 1 << Square::C2 as u8, 0); - assert_eq!(bb & 1 << Square::B1 as u8, 0); - assert_ne!(bb & 1 << Square::F3 as u8, 0); - assert_eq!(bb & 1 << Square::G2 as u8, 0); - assert_ne!(bb & 1 << Square::H7 as u8, 0); - assert_ne!(bb & 1 << Square::B7 as u8, 0); - assert_eq!(bb & 1 << Square::A8 as u8, 0); + assert_ne!(bb & Square::C2.to_bitboard(), 0); + assert_eq!(bb & Square::B1.to_bitboard(), 0); + assert_ne!(bb & Square::F3.to_bitboard(), 0); + assert_eq!(bb & Square::G2.to_bitboard(), 0); + assert_ne!(bb & Square::H7.to_bitboard(), 0); + assert_ne!(bb & Square::B7.to_bitboard(), 0); + assert_eq!(bb & Square::A8.to_bitboard(), 0); } #[test] fn test_rook_attacks() { let attacks = Attacks::new(); - let square = Square::E4 as u8; + let square = Square::E4; let occupancy = - 1 << Square::B7 as usize | - 1 << Square::B1 as usize | - 1 << Square::C2 as usize | - 1 << Square::E3 as usize | - 1 << Square::F3 as usize; + Square::B7.to_bitboard() | + Square::B1.to_bitboard() | + Square::C2.to_bitboard() | + Square::E3.to_bitboard() | + Square::F3.to_bitboard(); let bb = attacks.rook(occupancy, square); print(occupancy, "Occupancy"); print(bb, "Rook e4"); - assert_ne!(bb & 1 << Square::E8 as u8, 0); - assert_ne!(bb & 1 << Square::E7 as u8, 0); - assert_ne!(bb & 1 << Square::E6 as u8, 0); - assert_ne!(bb & 1 << Square::E5 as u8, 0); - assert_ne!(bb & 1 << Square::E3 as u8, 0); - assert_eq!(bb & 1 << Square::E2 as u8, 0); - assert_eq!(bb & 1 << Square::E1 as u8, 0); - assert_ne!(bb & 1 << Square::A4 as u8, 0); - assert_ne!(bb & 1 << Square::H4 as u8, 0); - assert_eq!(bb & 1 << Square::E4 as u8, 0); + assert_ne!(bb & Square::E8.to_bitboard(), 0); + assert_ne!(bb & Square::E7.to_bitboard(), 0); + assert_ne!(bb & Square::E6.to_bitboard(), 0); + assert_ne!(bb & Square::E5.to_bitboard(), 0); + assert_ne!(bb & Square::E3.to_bitboard(), 0); + assert_eq!(bb & Square::E2.to_bitboard(), 0); + assert_eq!(bb & Square::E1.to_bitboard(), 0); + assert_ne!(bb & Square::A4.to_bitboard(), 0); + assert_ne!(bb & Square::H4.to_bitboard(), 0); + assert_eq!(bb & Square::E4.to_bitboard(), 0); } #[test] fn test_queen_attacks() { let attacks = Attacks::new(); - let square = Square::E4 as u8; + let square = Square::E4; let bb = attacks.queen(DEFAULT_OCCUPANCY, square); print(DEFAULT_OCCUPANCY, "Occupancy"); print(bb, "Queen e4"); |