[board, moves] Reorganize castling_right API on Board

Board::color_has_castling_right takes an Option<Color> which it unwraps. With that
unwrapped Color, it can call…

Board::color_has_castling_right_unwrapped, which performs the evaluation with a
non-Option Color.

Clean up some imports.
This commit is contained in:
Eryn Wells 2025-06-17 08:28:39 -07:00
parent 8dc1e859e0
commit 37cb9bcaa0
5 changed files with 28 additions and 19 deletions

View file

@ -1,11 +1,10 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
castle,
PieceSet, castle,
display::DiagramFormatter,
piece_sets::{PlacePieceError, PlacePieceStrategy},
zobrist::{ZobristHash, ZobristState},
PieceSet,
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, Shape, Square, Wing};
@ -109,11 +108,16 @@ impl Board {
#[must_use]
pub fn active_color_has_castling_right(&self, wing: Wing) -> bool {
self.color_has_castling_right(self.active_color, wing)
self.color_has_castling_right_unwrapped(self.active_color, wing)
}
#[must_use]
pub fn color_has_castling_right(&self, color: Color, wing: Wing) -> bool {
pub fn color_has_castling_right(&self, color: Option<Color>, wing: Wing) -> bool {
self.color_has_castling_right_unwrapped(self.unwrap_color(color), wing)
}
#[must_use]
pub fn color_has_castling_right_unwrapped(&self, color: Color, wing: Wing) -> bool {
self.castling_rights.color_has_right(color, wing)
}
@ -129,7 +133,12 @@ impl Board {
self.update_zobrist_hash_castling_rights(old_rights);
}
pub fn revoke_castling_right(&mut self, color: Color, wing: Wing) {
pub fn revoke_castling_right(&mut self, color: Option<Color>, wing: Wing) {
let color = self.unwrap_color(color);
self.revoke_castling_right_unwrapped(color, wing);
}
pub fn revoke_castling_right_unwrapped(&mut self, color: Color, wing: Wing) {
let old_rights = self.castling_rights;
self.castling_rights.revoke(color, wing);
self.update_zobrist_hash_castling_rights(old_rights);

View file

@ -46,7 +46,7 @@ impl Board {
let color = self.unwrap_color(color);
if !self.color_has_castling_right(color, wing) {
if !self.color_has_castling_right_unwrapped(color, wing) {
return Err(CastleEvaluationError::NoRights { color, wing });
}
@ -94,7 +94,7 @@ impl Board {
mod tests {
use super::*;
use crate::test_board;
use chessfriend_core::{piece, Color, Wing};
use chessfriend_core::{Color, Wing, piece};
#[test]
fn king_on_starting_square_can_castle() {
@ -104,8 +104,8 @@ mod tests {
White Rook on H1
);
assert!(board.color_has_castling_right(Color::White, Wing::KingSide));
assert!(board.color_has_castling_right(Color::White, Wing::QueenSide));
assert!(board.color_has_castling_right_unwrapped(Color::White, Wing::KingSide));
assert!(board.color_has_castling_right_unwrapped(Color::White, Wing::QueenSide));
}
#[test]

View file

@ -1,8 +1,8 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{piece_sets::PlacePieceStrategy, Board};
use crate::{Board, piece_sets::PlacePieceStrategy};
use chessfriend_core::{
coordinates::ParseSquareError, piece, Color, File, Piece, Rank, Square, Wing,
Color, File, Piece, Rank, Square, Wing, coordinates::ParseSquareError, piece,
};
use std::fmt::Write;
use thiserror::Error;
@ -123,7 +123,7 @@ impl ToFenStr for Board {
(Color::Black, Wing::QueenSide),
]
.map(|(color, castle)| {
if !self.color_has_castling_right(color, castle) {
if !self.color_has_castling_right_unwrapped(color, castle) {
return "";
}

View file

@ -244,7 +244,7 @@ impl<T: BoardProvider> MakeMoveInternal for T {
// original board state is preserved.
let record = MoveRecord::new(board, ply, None);
board.revoke_castling_right(active_color, wing);
board.revoke_castling_right_unwrapped(active_color, wing);
self.advance_board_state(None, HalfMoveClock::Advance);
@ -378,7 +378,7 @@ mod tests {
use super::*;
use crate::{Move, PromotionShape};
use chessfriend_board::test_board;
use chessfriend_core::{piece, Color, Square};
use chessfriend_core::{Color, Square, piece};
type TestResult = Result<(), MakeMoveError>;
@ -528,7 +528,7 @@ mod tests {
assert_eq!(board.get_piece(Square::H1), None);
assert_eq!(board.get_piece(Square::G1), Some(piece!(White King)));
assert_eq!(board.get_piece(Square::F1), Some(piece!(White Rook)));
assert!(!board.color_has_castling_right(Color::White, Wing::KingSide));
assert!(!board.color_has_castling_right_unwrapped(Color::White, Wing::KingSide));
Ok(())
}
@ -548,7 +548,7 @@ mod tests {
assert_eq!(board.get_piece(Square::A1), None);
assert_eq!(board.get_piece(Square::C1), Some(piece!(White King)));
assert_eq!(board.get_piece(Square::D1), Some(piece!(White Rook)));
assert!(!board.color_has_castling_right(Color::White, Wing::QueenSide));
assert!(!board.color_has_castling_right_unwrapped(Color::White, Wing::QueenSide));
Ok(())
}

View file

@ -193,7 +193,7 @@ mod tests {
use super::*;
use crate::{MakeMove, Move, PromotionShape, ValidateMove};
use chessfriend_board::test_board;
use chessfriend_core::{piece, Color, Square, Wing};
use chessfriend_core::{Color, Square, Wing, piece};
type TestResult = Result<(), Box<dyn std::error::Error>>;
@ -406,7 +406,7 @@ mod tests {
assert_eq!(board.get_piece(Square::H1), None);
assert_eq!(board.get_piece(Square::G1), Some(piece!(White King)));
assert_eq!(board.get_piece(Square::F1), Some(piece!(White Rook)));
assert!(!board.color_has_castling_right(Color::White, Wing::KingSide));
assert!(!board.color_has_castling_right_unwrapped(Color::White, Wing::KingSide));
board.unmake_move(&record)?;
@ -438,7 +438,7 @@ mod tests {
assert_eq!(board.get_piece(Square::A1), None);
assert_eq!(board.get_piece(Square::C1), Some(piece!(White King)));
assert_eq!(board.get_piece(Square::D1), Some(piece!(White Rook)));
assert!(!board.color_has_castling_right(Color::White, Wing::QueenSide));
assert!(!board.color_has_castling_right_unwrapped(Color::White, Wing::QueenSide));
board.unmake_move(&record)?;