chessfriend/board/src/en_passant.rs

48 lines
1.4 KiB
Rust

// Eryn Wells <eryn@erynwells.me>
use chessfriend_core::{Rank, Square};
/// En passant information.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
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_board::en_passant::EnPassant;
/// use chessfriend_core::Square;
/// 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> {
Self::_capture_square(target).map(|capture| Self { target, capture })
}
/// The square the capturing piece will move to.
pub fn target_square(self) -> Square {
self.target
}
/// The square on which the captured pawn sits.
pub fn capture_square(self) -> Square {
self.capture
}
}