[board, explorer, moves] Make Board::active_color private

Make the struct attribute private, and export two new methods. A getter, active_color(),
and a setter, set_active_color().

Update all references to the attribute to use the methods.
This commit is contained in:
Eryn Wells 2025-06-02 17:29:52 -07:00
parent cae93cb090
commit eaab34587c
10 changed files with 55 additions and 40 deletions

View file

@ -14,7 +14,7 @@ pub type FullMoveClock = u32;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Board {
pub active_color: Color,
active_color: Color,
pieces: PieceSet,
pub castling_rights: castle::Rights,
pub en_passant_target: Option<Square>,
@ -58,6 +58,19 @@ impl Board {
}
impl Board {
#[must_use]
pub fn active_color(&self) -> Color {
self.active_color
}
pub fn set_active_color(&mut self, color: Color) {
if color == self.active_color {
return;
}
self.active_color = color;
}
#[must_use]
pub fn get_piece(&self, square: Square) -> Option<Piece> {
self.pieces.get(square)

View file

@ -78,13 +78,13 @@ impl Board {
}
pub(crate) fn castling_king(&self, square: Square) -> Option<Piece> {
let active_color = self.active_color;
let active_color = self.active_color();
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_king())
}
pub(crate) fn castling_rook(&self, square: Square) -> Option<Piece> {
let active_color = self.active_color;
let active_color = self.active_color();
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_rook())
}

View file

@ -7,7 +7,7 @@ use chessfriend_core::{Color, Piece};
impl Board {
#[must_use]
pub fn active_color_is_in_check(&self) -> bool {
self.unwrapped_color_is_in_check(self.active_color)
self.unwrapped_color_is_in_check(self.active_color())
}
#[must_use]

View file

@ -113,7 +113,7 @@ impl ToFenStr for Board {
}
}
write!(fen_string, " {}", self.active_color.to_fen_str()?)
write!(fen_string, " {}", self.active_color().to_fen_str()?)
.map_err(ToFenStrError::FmtError)?;
let castling = [
@ -224,7 +224,7 @@ impl FromFenStr for Board {
.next()
.ok_or(FromFenStrError::MissingField(Field::PlayerToMove))?,
)?;
board.active_color = active_color;
board.set_active_color(active_color);
let castling_rights = fields
.next()

View file

@ -13,7 +13,7 @@ macro_rules! test_board {
chessfriend_core::Square::$square,
$crate::PlacePieceStrategy::default());
)*
board.active_color = chessfriend_core::Color::$to_move;
board.set_active_color(chessfriend_core::Color::$to_move);
board.en_passant_target = Some(chessfriend_core::Square::$en_passant);
println!("{}", board.display());

View file

@ -32,7 +32,7 @@ impl Board {
}
pub fn active_sight(&self) -> BitBoard {
self.friendly_sight(self.active_color)
self.friendly_sight(self.active_color())
}
/// A [`BitBoard`] of all squares the given color can see.
@ -45,7 +45,7 @@ impl Board {
}
pub fn active_color_opposing_sight(&self) -> BitBoard {
self.opposing_sight(self.active_color)
self.opposing_sight(self.active_color())
}
/// A [`BitBoard`] of all squares visible by colors that oppose the given color.