[board] Implement a BitBoard::rank constructor

Also implement BitBoard::shift_north() to support rank().
This commit is contained in:
Eryn Wells 2023-12-27 10:04:02 -07:00
parent dda4cd8a5a
commit 61448d437f

View file

@ -17,6 +17,10 @@ impl BitBoard {
BitBoard(bits)
}
pub fn rank(rank: u8) -> BitBoard {
BitBoard(0xFF).shift_north(rank)
}
pub fn is_empty(&self) -> bool {
self.0 == 0
}
@ -38,6 +42,11 @@ 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)
@ -139,6 +148,18 @@ mod tests {
use super::*;
use crate::Square;
#[test]
fn rank() {
assert_eq!(BitBoard::rank(0).0, 0xFF, "Rank 1");
assert_eq!(BitBoard::rank(1).0, 0xFF00, "Rank 2");
assert_eq!(BitBoard::rank(2).0, 0xFF0000, "Rank 3");
assert_eq!(BitBoard::rank(3).0, 0xFF000000, "Rank 4");
assert_eq!(BitBoard::rank(4).0, 0xFF00000000, "Rank 5");
assert_eq!(BitBoard::rank(5).0, 0xFF0000000000, "Rank 6");
assert_eq!(BitBoard::rank(6).0, 0xFF000000000000, "Rank 7");
assert_eq!(BitBoard::rank(7).0, 0xFF00000000000000, "Rank 8");
}
#[test]
fn is_empty() {
assert!(BitBoard(0).is_empty());
@ -210,6 +231,14 @@ mod tests {
);
}
#[test]
fn shift_n() {
assert_eq!(
BitBoard(0x0008_0000_0000).shift_north(2),
BitBoard(0x0008_0000_0000_0000)
);
}
#[test]
fn pieces() {
let bb = BitBoard(0x1001_1010); // e4