Return a chessfriend_moves::EnPassant from a new Position::en_passant()

Replace Position's en_passant_target_square() and en_passant_capture_square()
with a single en_passant() method that returns a new EnPassant struct that has
the target and capture squares for the en passant move.
This commit is contained in:
Eryn Wells 2024-02-10 18:30:11 -07:00
parent cc23ee2d90
commit e6a9b7f8c4
8 changed files with 66 additions and 27 deletions

49
moves/src/en_passant.rs Normal file
View file

@ -0,0 +1,49 @@
// Eryn Wells <eryn@erynwells.me>
use chessfriend_core::{Rank, Square};
/// En passant information.
#[derive(Clone, Copy, Debug)]
pub struct EnPassant {
target: Square,
capture: Square,
}
impl EnPassant {
fn _capture_square(target: Square) -> Option<Square> {
let (file, rank) = target.file_rank();
match rank {
Rank::THREE => Some(Square::from_file_rank(file, Rank::FOUR)),
Rank::SIX => Some(Square::from_file_rank(file, Rank::FIVE)),
_ => None,
}
}
/// Return en passant information for a particular target square. The target
/// square is the square a pawn capturing en passant will move to.
///
/// Return `None` if the square is not eligible for en passant.
///
/// ## Examples
///
/// ```
/// use chessfriend_core::Square;
/// use chessfriend_moves::EnPassant;
/// assert!(EnPassant::from_target_square(Square::E3).is_some());
/// assert!(EnPassant::from_target_square(Square::B4).is_none());
/// ```
pub fn from_target_square(target: Square) -> Option<Self> {
match Self::_capture_square(target) {
Some(capture) => Some(Self { target, capture }),
None => None,
}
}
pub fn target_square(&self) -> Square {
self.target
}
pub fn capture_square(&self) -> Square {
self.capture
}
}

View file

@ -3,9 +3,11 @@
mod builder;
mod castle;
mod defs;
mod en_passant;
mod moves;
pub use builder::{Builder, Error as BuilderError};
pub use castle::Castle;
pub use defs::PromotionShape;
pub use en_passant::EnPassant;
pub use moves::Move;