From 6af64171a2954e7522d1e323befcb338c1e227f6 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 22 Dec 2023 08:50:03 -0800 Subject: [PATCH] [board] Implement BitAnd and BitOr on BitBoard and make it's u64 private --- board/src/bitboard.rs | 43 +++++++++++++++++++++++++++++++++- board/src/position.rs | 54 +++++++++++++++++++++---------------------- 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/board/src/bitboard.rs b/board/src/bitboard.rs index 5b17050..d797d9b 100644 --- a/board/src/bitboard.rs +++ b/board/src/bitboard.rs @@ -2,11 +2,20 @@ use crate::square::Square; use std::arch::asm; +use std::ops::{BitAnd, BitOr}; #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct BitBoard(pub u64); +pub struct BitBoard(u64); impl BitBoard { + pub fn empty() -> BitBoard { + BitBoard(0) + } + + pub fn from_bit_field(bits: u64) -> BitBoard { + BitBoard(bits) + } + fn is_empty(&self) -> bool { self.0 == 0 } @@ -38,6 +47,38 @@ impl BitBoard { } } +impl BitAnd for BitBoard { + type Output = BitBoard; + + fn bitand(self, rhs: Self) -> Self { + BitBoard(self.0 & rhs.0) + } +} + +impl BitAnd<&BitBoard> for BitBoard { + type Output = BitBoard; + + fn bitand(self, rhs: &Self) -> Self { + BitBoard(self.0 & rhs.0) + } +} + +impl BitOr for BitBoard { + type Output = BitBoard; + + fn bitor(self, rhs: Self) -> Self { + BitBoard(self.0 | rhs.0) + } +} + +impl BitOr<&BitBoard> for BitBoard { + type Output = BitBoard; + + fn bitor(self, rhs: &Self) -> Self { + BitBoard(self.0 | rhs.0) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/board/src/position.rs b/board/src/position.rs index 7033a5f..6279c16 100644 --- a/board/src/position.rs +++ b/board/src/position.rs @@ -29,23 +29,23 @@ pub struct Position { impl Position { fn empty() -> Position { Position { - pieces_per_color: [BitBoard(0), BitBoard(0)], + pieces_per_color: [BitBoard::empty(), BitBoard::empty()], pieces_per_type: [ [ - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), ], [ - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), - BitBoard(0), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), + BitBoard::empty(), ], ], } @@ -54,27 +54,27 @@ impl Position { /// Return a starting position. fn starting() -> Position { let white_pieces = [ - BitBoard(0x00FF000000000000), - BitBoard(0x4200000000000000), - BitBoard(0x2400000000000000), - BitBoard(0x8100000000000000), - BitBoard(0x1000000000000000), - BitBoard(0x8000000000000000), + BitBoard::from_bit_field(0x00FF000000000000), + BitBoard::from_bit_field(0x4200000000000000), + BitBoard::from_bit_field(0x2400000000000000), + BitBoard::from_bit_field(0x8100000000000000), + BitBoard::from_bit_field(0x1000000000000000), + BitBoard::from_bit_field(0x8000000000000000), ]; let black_pieces = [ - BitBoard(0xFF00), - BitBoard(0x0042), - BitBoard(0x0024), - BitBoard(0x0081), - BitBoard(0x0010), - BitBoard(0x0080), + BitBoard::from_bit_field(0xFF00), + BitBoard::from_bit_field(0x0042), + BitBoard::from_bit_field(0x0024), + BitBoard::from_bit_field(0x0081), + BitBoard::from_bit_field(0x0010), + BitBoard::from_bit_field(0x0080), ]; Position { pieces_per_color: [ - BitBoard(white_pieces.iter().map(|bb| bb.0).fold(0, |a, b| a | b)), - BitBoard(black_pieces.iter().map(|bb| bb.0).fold(0, |a, b| a | b)), + white_pieces.iter().fold(BitBoard::empty(), |a, b| a | b), + black_pieces.iter().fold(BitBoard::empty(), |a, b| a | b), ], pieces_per_type: [white_pieces, black_pieces], }