[board, explorer, moves] Clean up the castling rights API

Reorganize castling rights API on Board into methods named according to
conventions applied to other API.

Board::has_castling_right
Board::has_castling_right_active
Board::has_castling_right_unwrapped

    These all check if a color has the right to castle on a particular side
    (wing) of the board. The first takes an Option<Color>, the latter two
    operate on bare Colors: the active color, or an explicit Color.

Board::grant_castling_right
Board::grant_castling_right_active
Board::grant_castling_right_unwrapped

    Grant castling rights to a color. Color arguments follow the pattern above.

Board::revoke_castling_right
Board::revoke_castling_right_active
Board::revoke_castling_right_unwrapped

    Revoke castling rights from a color. Color arguments follow the pattern
    above.

The latter two groups of methods take a new CastleRightsOption type that
specifies either a single Wing or All.

Rework the implementation of CastleRights to take a CastleRightsOption. Update
the unit tests and make sure everything builds.
This commit is contained in:
Eryn Wells 2025-06-18 23:44:40 +00:00
parent 933924d37a
commit 4ce7e89cdb
8 changed files with 177 additions and 124 deletions

View file

@ -4,10 +4,10 @@ mod parameters;
mod rights;
pub use parameters::Parameters;
pub use rights::Rights;
pub use rights::{CastleRightsOption, Rights};
use crate::{Board, CastleParameters};
use chessfriend_core::{Color, Piece, Square, Wing};
use chessfriend_core::{Color, Wing};
use thiserror::Error;
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
@ -46,7 +46,7 @@ impl Board {
let color = self.unwrap_color(color);
if !self.color_has_castling_right_unwrapped(color, wing) {
if !self.has_castling_right_unwrapped(color, wing.into()) {
return Err(CastleEvaluationError::NoRights { color, wing });
}
@ -76,17 +76,60 @@ impl Board {
Ok(parameters)
}
}
pub(crate) fn castling_king(&self, square: Square) -> Option<Piece> {
let active_color = self.active_color();
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_king())
impl Board {
#[must_use]
pub fn has_castling_right(&self, color: Option<Color>, wing: Wing) -> bool {
self.has_castling_right_unwrapped(self.unwrap_color(color), wing)
}
pub(crate) fn castling_rook(&self, square: Square) -> Option<Piece> {
let active_color = self.active_color();
self.get_piece(square)
.filter(|piece| piece.color == active_color && piece.is_rook())
#[must_use]
pub fn has_castling_right_active(&self, wing: Wing) -> bool {
self.has_castling_right_unwrapped(self.active_color(), wing)
}
#[must_use]
pub fn has_castling_right_unwrapped(&self, color: Color, wing: Wing) -> bool {
self.castling_rights().get(color, wing.into())
}
}
impl Board {
pub fn grant_castling_rights(&mut self, color: Option<Color>, rights: CastleRightsOption) {
let color = self.unwrap_color(color);
self.grant_castling_rights_unwrapped(color, rights);
}
pub fn grant_castling_rights_active(&mut self, rights: CastleRightsOption) {
self.grant_castling_rights_unwrapped(self.active_color(), rights);
}
pub fn grant_castling_rights_unwrapped(&mut self, color: Color, rights: CastleRightsOption) {
let old_rights = *self.castling_rights();
self.castling_rights_mut().grant(color, rights);
self.update_zobrist_hash_castling_rights(old_rights);
}
}
impl Board {
pub fn revoke_all_castling_rights(&mut self) {
self.castling_rights_mut().revoke_all();
}
pub fn revoke_castling_rights(&mut self, color: Option<Color>, rights: CastleRightsOption) {
let color = self.unwrap_color(color);
self.revoke_castling_rights_unwrapped(color, rights);
}
pub fn revoke_castling_rights_active(&mut self, rights: CastleRightsOption) {
self.revoke_castling_rights_unwrapped(self.active_color(), rights);
}
pub fn revoke_castling_rights_unwrapped(&mut self, color: Color, rights: CastleRightsOption) {
let old_rights = *self.castling_rights();
self.castling_rights_mut().revoke(color, rights);
self.update_zobrist_hash_castling_rights(old_rights);
}
}
@ -104,8 +147,8 @@ mod tests {
White Rook on H1
);
assert!(board.color_has_castling_right_unwrapped(Color::White, Wing::KingSide));
assert!(board.color_has_castling_right_unwrapped(Color::White, Wing::QueenSide));
assert!(board.has_castling_right_unwrapped(Color::White, Wing::KingSide));
assert!(board.has_castling_right_unwrapped(Color::White, Wing::QueenSide));
}
#[test]