From 37cb9bcaa024c8cbfab3424b5eb5670537bccec1 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 17 Jun 2025 08:28:39 -0700 Subject: [PATCH] [board, moves] Reorganize castling_right API on Board MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Board::color_has_castling_right takes an Option 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. --- board/src/board.rs | 19 ++++++++++++++----- board/src/castle.rs | 8 ++++---- board/src/fen.rs | 6 +++--- moves/src/make_move.rs | 8 ++++---- moves/src/unmake_move.rs | 6 +++--- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/board/src/board.rs b/board/src/board.rs index 6a93eef..666488e 100644 --- a/board/src/board.rs +++ b/board/src/board.rs @@ -1,11 +1,10 @@ // Eryn Wells 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, 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, 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); diff --git a/board/src/castle.rs b/board/src/castle.rs index c5c3423..f8f9c24 100644 --- a/board/src/castle.rs +++ b/board/src/castle.rs @@ -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] diff --git a/board/src/fen.rs b/board/src/fen.rs index 12f59ed..0da38dc 100644 --- a/board/src/fen.rs +++ b/board/src/fen.rs @@ -1,8 +1,8 @@ // Eryn Wells -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 ""; } diff --git a/moves/src/make_move.rs b/moves/src/make_move.rs index bfb7d46..38b03cf 100644 --- a/moves/src/make_move.rs +++ b/moves/src/make_move.rs @@ -244,7 +244,7 @@ impl 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(()) } diff --git a/moves/src/unmake_move.rs b/moves/src/unmake_move.rs index 2c6ebed..8a477c6 100644 --- a/moves/src/unmake_move.rs +++ b/moves/src/unmake_move.rs @@ -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>; @@ -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)?;