[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:
		
							parent
							
								
									8dc1e859e0
								
							
						
					
					
						commit
						37cb9bcaa0
					
				
					 5 changed files with 28 additions and 19 deletions
				
			
		| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 "";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue