From 72dc7b9a4849488f2c2dc15c2256b0ae1ad9dd72 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 11 Mar 2025 15:59:09 -0400 Subject: [PATCH] bitboard: make directional methods take step argument --- src/repr/bitboard.rs | 37 ++++++++++++++++++------------------- src/repr/board.rs | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index 8fed134..57a08e1 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -45,43 +45,42 @@ impl BitBoard { self.0 == 0 } - // Directional shifts with edge masking (prevents wrapping) - pub const fn east(&self) -> Self { - let mask = !Self::col_mask(Board::BOARD_SIZE - 1).0; // Mask to block column 7 bits - Self((self.0 & mask) << 1) + pub const fn east(&self, n: usize) -> Self { + let mask = !Self::col_mask(Board::BOARD_SIZE - 1).0; // Mask to block column BOARD_SIZE-1 bits + Self((self.0 & mask) << n) } - pub const fn west(&self) -> Self { + pub const fn west(&self, n: usize) -> Self { let mask = !Self::col_mask(0).0; - Self((self.0 & mask) >> 1) + Self((self.0 & mask) >> n) } - pub const fn north(&self) -> Self { - Self(self.0 >> Board::BOARD_SIZE) + pub const fn north(&self, n: usize) -> Self { + Self(self.0 >> (Board::BOARD_SIZE as usize * n)) } - pub const fn south(&self) -> Self { - Self(self.0 << Board::BOARD_SIZE) + pub const fn south(&self, n: usize) -> Self { + Self(self.0 << (Board::BOARD_SIZE as usize * n)) } - pub const fn northeast(&self) -> Self { - self.north().east() + pub const fn northeast(&self, n: usize) -> Self { + self.north(n).east(n) } - pub const fn northwest(&self) -> Self { - self.north().west() + pub const fn northwest(&self, n: usize) -> Self { + self.north(n).west(n) } - pub const fn southeast(&self) -> Self { - self.south().east() + pub const fn southeast(&self, n: usize) -> Self { + self.south(n).east(n) } - pub const fn southwest(&self) -> Self { - self.south().west() + pub const fn southwest(&self, n: usize) -> Self { + self.south(n).west(n) } /// All direction methods - pub const DIRECTIONS: [fn(&Self) -> Self; 8] = [ + pub const DIRECTIONS: [fn(&Self, usize) -> Self; 8] = [ BitBoard::east, BitBoard::west, BitBoard::north, diff --git a/src/repr/board.rs b/src/repr/board.rs index 6e9b5c4..f0c2f5b 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -298,7 +298,7 @@ impl Board { // Expand in direction until edge or non-opponent piece loop { - current = dir(¤t); + current = dir(¤t, 1); if current.is_empty() || !current.intersects(*opponent_board) { break; }