diff --git a/board/src/bitboard/bitboard.rs b/board/src/bitboard/bitboard.rs index f777d64..dbb1c8c 100644 --- a/board/src/bitboard/bitboard.rs +++ b/board/src/bitboard/bitboard.rs @@ -6,7 +6,7 @@ use std::fmt; use std::ops::{BitAnd, BitOr, Not}; #[derive(Clone, Copy, Eq, Hash, PartialEq)] -pub(crate) struct BitBoard(u64); +pub(crate) struct BitBoard(pub(super) u64); impl BitBoard { pub fn empty() -> BitBoard { @@ -38,61 +38,6 @@ impl BitBoard { } } -impl BitBoard { - const NOT_A_FILE: u64 = 0xfefefefefefefefe; - const NOT_H_FILE: u64 = 0x7f7f7f7f7f7f7f7f; - - #[inline] - pub fn shift_north(&self, n: u8) -> BitBoard { - BitBoard(self.0 << (8 * n)) - } - - #[inline] - pub fn shift_north_one(&self) -> BitBoard { - BitBoard(self.0 << 8) - } - - #[inline] - pub fn shift_north_east_one(&self) -> BitBoard { - BitBoard(self.0 << 9 & BitBoard::NOT_A_FILE) - } - - #[inline] - pub fn shift_east_one(&self) -> BitBoard { - BitBoard(self.0 << 1 & BitBoard::NOT_A_FILE) - } - - #[inline] - pub fn shift_south_east_one(&self) -> BitBoard { - BitBoard(self.0 >> 7 & BitBoard::NOT_A_FILE) - } - - #[inline] - pub fn shift_south(&self, n: u8) -> BitBoard { - BitBoard(self.0 >> (8 * n)) - } - - #[inline] - pub fn shift_south_one(&self) -> BitBoard { - BitBoard(self.0 >> 8) - } - - #[inline] - pub fn shift_south_west_one(&self) -> BitBoard { - BitBoard(self.0 >> 9 & BitBoard::NOT_H_FILE) - } - - #[inline] - pub fn shift_west_one(&self) -> BitBoard { - BitBoard(self.0 >> 1 & BitBoard::NOT_H_FILE) - } - - #[inline] - pub fn shift_north_west_one(&self) -> BitBoard { - BitBoard(self.0 << 7 & BitBoard::NOT_H_FILE) - } -} - impl BitBoard { pub(crate) fn occupied_squares(&self) -> impl Iterator { BitScanner::new(self.0) @@ -200,55 +145,6 @@ mod tests { assert!(!bb.has_piece_at(&sq)); } - #[test] - fn cardinal_direction_shifts() { - let bb = BitBoard(0x1000_0000); // e4 - - assert_eq!(bb.shift_north_one(), BitBoard(0x0010_0000_0000), "North"); - assert_eq!(bb.shift_east_one(), BitBoard(0x2000_0000), "East"); - assert_eq!(bb.shift_south_one(), BitBoard(0x0010_0000), "South"); - assert_eq!(bb.shift_west_one(), BitBoard(0x0800_0000), "West"); - } - - #[test] - fn intercardinal_direction_shifts() { - let bb = BitBoard(0x1000_0000); // e4 - - assert_eq!( - bb.shift_north_east_one(), - BitBoard(0x0020_0000_0000), - "North East" - ); - assert_eq!( - bb.shift_south_east_one(), - BitBoard(0x0020_0000), - "South East" - ); - assert_eq!( - bb.shift_south_west_one(), - BitBoard(0x0008_0000), - "South West" - ); - assert_eq!( - bb.shift_north_west_one(), - BitBoard(0x0008_0000_0000), - "North West" - ); - } - - #[test] - fn shift_n() { - assert_eq!( - BitBoard(0x0008_0000_0000).shift_north(2), - BitBoard(0x0008_0000_0000_0000) - ); - - assert_eq!( - BitBoard(0x0008_0000_0000).shift_south(2), - BitBoard(0x0008_0000) - ); - } - #[test] fn pieces() { let bb = BitBoard(0x1001_1010); // e4 diff --git a/board/src/bitboard/mod.rs b/board/src/bitboard/mod.rs index 34d2ce4..9bf08d1 100644 --- a/board/src/bitboard/mod.rs +++ b/board/src/bitboard/mod.rs @@ -1,5 +1,6 @@ mod bit_scanner; mod bitboard; +mod shifts; pub(crate) use self::bit_scanner::BitScanner; pub(crate) use self::bitboard::BitBoard; diff --git a/board/src/bitboard/shifts.rs b/board/src/bitboard/shifts.rs new file mode 100644 index 0000000..3b1da02 --- /dev/null +++ b/board/src/bitboard/shifts.rs @@ -0,0 +1,113 @@ +// Eryn Wells + +use super::BitBoard; + +impl BitBoard { + const NOT_A_FILE: u64 = 0xfefefefefefefefe; + const NOT_H_FILE: u64 = 0x7f7f7f7f7f7f7f7f; + + #[inline] + pub fn shift_north(&self, n: u8) -> BitBoard { + BitBoard(self.0 << (8 * n)) + } + + #[inline] + pub fn shift_north_one(&self) -> BitBoard { + BitBoard(self.0 << 8) + } + + #[inline] + pub fn shift_north_east_one(&self) -> BitBoard { + BitBoard(self.0 << 9 & BitBoard::NOT_A_FILE) + } + + #[inline] + pub fn shift_east_one(&self) -> BitBoard { + BitBoard(self.0 << 1 & BitBoard::NOT_A_FILE) + } + + #[inline] + pub fn shift_south_east_one(&self) -> BitBoard { + BitBoard(self.0 >> 7 & BitBoard::NOT_A_FILE) + } + + #[inline] + pub fn shift_south(&self, n: u8) -> BitBoard { + BitBoard(self.0 >> (8 * n)) + } + + #[inline] + pub fn shift_south_one(&self) -> BitBoard { + BitBoard(self.0 >> 8) + } + + #[inline] + pub fn shift_south_west_one(&self) -> BitBoard { + BitBoard(self.0 >> 9 & BitBoard::NOT_H_FILE) + } + + #[inline] + pub fn shift_west_one(&self) -> BitBoard { + BitBoard(self.0 >> 1 & BitBoard::NOT_H_FILE) + } + + #[inline] + pub fn shift_north_west_one(&self) -> BitBoard { + BitBoard(self.0 << 7 & BitBoard::NOT_H_FILE) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Square; + + #[test] + fn cardinal_direction_shifts() { + let bb = BitBoard(0x1000_0000); // e4 + + assert_eq!(bb.shift_north_one(), BitBoard(0x0010_0000_0000), "North"); + assert_eq!(bb.shift_east_one(), BitBoard(0x2000_0000), "East"); + assert_eq!(bb.shift_south_one(), BitBoard(0x0010_0000), "South"); + assert_eq!(bb.shift_west_one(), BitBoard(0x0800_0000), "West"); + } + + #[test] + fn intercardinal_direction_shifts() { + let bb = BitBoard(0x1000_0000); // e4 + + assert_eq!( + bb.shift_north_east_one(), + BitBoard(0x0020_0000_0000), + "North East" + ); + assert_eq!( + bb.shift_south_east_one(), + BitBoard(0x0020_0000), + "South East" + ); + assert_eq!( + bb.shift_south_west_one(), + BitBoard(0x0008_0000), + "South West" + ); + assert_eq!( + bb.shift_north_west_one(), + BitBoard(0x0008_0000_0000), + "North West" + ); + } + + #[test] + fn shift_n() { + assert_eq!( + BitBoard(0x0008_0000_0000).shift_north(2), + BitBoard(0x0008_0000_0000_0000) + ); + + assert_eq!( + BitBoard(0x0008_0000_0000).shift_south(2), + BitBoard(0x0008_0000) + ); + } +}