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

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
chessfriend_core = { path = "../core" }
chessfriend_bitboard = { path = "../bitboard" }
chessfriend_moves = { path = "../moves" }

View file

@ -95,11 +95,8 @@ impl ToFen for Position {
write!(
fen_string,
" {}",
if let Some(en_passant_square) = self.en_passant_target_square() {
en_passant_square.to_string()
} else {
"-".to_string()
}
self.en_passant()
.map_or("-".to_string(), |ep| ep.target_square().to_string())
)
.map_err(|err| ToFenError::FmtError(err))?;

View file

@ -1,10 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use crate::{
position::flags::Flags,
r#move::{AlgebraicMoveFormatter, Castle},
MakeMoveError, Move, Position,
};
use crate::{position::flags::Flags, r#move::Castle, MakeMoveError, Move, Position};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Direction, Piece, PlacedPiece, Shape, Square};
@ -292,7 +288,10 @@ mod tests {
new_position.piece_on_square(Square::E4),
Some(piece!(White Pawn on E4))
);
assert_eq!(new_position.en_passant_target_square(), Some(Square::E3));
assert_eq!(
new_position.en_passant().map(|ep| ep.target_square()),
Some(Square::E3)
);
Ok(())
}

View file

@ -52,7 +52,7 @@ impl Builder {
flags: position.flags(),
pieces,
kings: [Some(white_king), Some(black_king)],
en_passant_square: position.en_passant_target_square(),
en_passant_square: position.en_passant().map(|ep| ep.target_square()),
ply_counter: position.ply_counter(),
move_number: position.move_number(),
}

View file

@ -10,6 +10,7 @@ use crate::{
};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece, Rank, Shape, Square};
use chessfriend_moves::EnPassant;
use std::{cell::OnceCell, fmt};
#[derive(Clone, Debug, Eq)]
@ -182,23 +183,12 @@ impl Position {
Pieces::new(&self, color)
}
/// If en passant is available in this position, the square a pawn will move
/// to if it captures en passant.
pub fn en_passant_target_square(&self) -> Option<Square> {
self.en_passant_square
pub fn has_en_passant_square(&self) -> bool {
self.en_passant_square.is_some()
}
/// If en passant is available in this position, the square on which the
/// captured pawn is sitting.
pub fn en_passant_capture_square(&self) -> Option<Square> {
let target_square = self.en_passant_square?;
let file = target_square.file();
Some(match target_square.rank() {
Rank::THREE => Square::from_file_rank(file, Rank::FOUR),
Rank::SIX => Square::from_file_rank(file, Rank::SEVEN),
_ => unreachable!(),
})
pub fn en_passant(&self) -> Option<EnPassant> {
EnPassant::from_target_square(self.en_passant_square?)
}
fn _sight_of_player(&self, player: Color, pieces: &PieceBitBoards) -> BitBoard {