[board] Use Castle as the interface type for methods related to castling
Use BoardSide as an internal type for looking up generated bitboards, target squares, etc.
This commit is contained in:
parent
7071f6a742
commit
918b68f300
4 changed files with 71 additions and 37 deletions
|
@ -5,6 +5,7 @@ use crate::{
|
|||
move_generator::Moves,
|
||||
piece::{Color, Piece, PlacedPiece, Shape},
|
||||
position::DiagramFormatter,
|
||||
r#move::Castle,
|
||||
sight::Sight,
|
||||
BitBoard, Move, Square,
|
||||
};
|
||||
|
@ -14,8 +15,17 @@ use std::{cell::OnceCell, fmt};
|
|||
/// player's king starts on. Queenside is the side of the board the player's
|
||||
/// queen starts on.
|
||||
pub(crate) enum BoardSide {
|
||||
King,
|
||||
Queen,
|
||||
King = 0,
|
||||
Queen = 1,
|
||||
}
|
||||
|
||||
impl From<Castle> for BoardSide {
|
||||
fn from(value: Castle) -> Self {
|
||||
match value {
|
||||
Castle::KingSide => BoardSide::King,
|
||||
Castle::QueenSide => BoardSide::Queen,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
@ -77,15 +87,26 @@ impl Position {
|
|||
/// The right to castle on a particular side of the board is retained as
|
||||
/// long as the player has not moved their king, or the rook on that side of
|
||||
/// the board.
|
||||
pub(crate) fn player_has_right_to_castle(&self, color: Color, castle: Castle) -> bool {
|
||||
self.flags.player_has_right_to_castle(color, castle)
|
||||
}
|
||||
|
||||
/// Returns `true` if the player is able to castle on the given side of the board.
|
||||
///
|
||||
/// The following requirements must also be met:
|
||||
/// The following requirements must be met:
|
||||
///
|
||||
/// 1. The player must still have the right to castle on that side of the
|
||||
/// board. The king and rook involved in the castle must not have moved.
|
||||
/// 1. The spaces between the king and rook must be clear
|
||||
/// 2. The king must not be in check
|
||||
/// 3. In the course of castling on that side, the king must not pass
|
||||
/// through a square that an enemy piece can see
|
||||
pub(crate) fn player_has_right_to_castle(&self, color: Color, side: BoardSide) -> bool {
|
||||
self.flags.player_has_right_to_castle(color, side)
|
||||
pub(crate) fn player_can_castle(&self, player: Color, castle: Castle) -> bool {
|
||||
if !self.player_has_right_to_castle(player, castle.into()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn moves(&self) -> Moves {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue