From cc23ee2d90148d3c32f6fcd84d6091220b0b7cf6 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 10 Feb 2024 11:40:27 -0700 Subject: [PATCH] Rename en passant square method on Position and implement a getter for the capture square MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Position::en_passant_square → en_passant_target_square Position::en_passant_capture_square --- position/src/fen.rs | 2 +- .../src/position/builders/move_builder.rs | 2 +- .../src/position/builders/position_builder.rs | 2 +- position/src/position/position.rs | 19 +++++++++++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/position/src/fen.rs b/position/src/fen.rs index d6c3568..9a8369a 100644 --- a/position/src/fen.rs +++ b/position/src/fen.rs @@ -95,7 +95,7 @@ impl ToFen for Position { write!( fen_string, " {}", - if let Some(en_passant_square) = self.en_passant_square() { + if let Some(en_passant_square) = self.en_passant_target_square() { en_passant_square.to_string() } else { "-".to_string() diff --git a/position/src/position/builders/move_builder.rs b/position/src/position/builders/move_builder.rs index c926c08..2de56d4 100644 --- a/position/src/position/builders/move_builder.rs +++ b/position/src/position/builders/move_builder.rs @@ -292,7 +292,7 @@ mod tests { new_position.piece_on_square(Square::E4), Some(piece!(White Pawn on E4)) ); - assert_eq!(new_position.en_passant_square(), Some(Square::E3)); + assert_eq!(new_position.en_passant_target_square(), Some(Square::E3)); Ok(()) } diff --git a/position/src/position/builders/position_builder.rs b/position/src/position/builders/position_builder.rs index 5011f21..cba432f 100644 --- a/position/src/position/builders/position_builder.rs +++ b/position/src/position/builders/position_builder.rs @@ -52,7 +52,7 @@ impl Builder { flags: position.flags(), pieces, kings: [Some(white_king), Some(black_king)], - en_passant_square: position.en_passant_square(), + en_passant_square: position.en_passant_target_square(), ply_counter: position.ply_counter(), move_number: position.move_number(), } diff --git a/position/src/position/position.rs b/position/src/position/position.rs index 01de793..b8c193a 100644 --- a/position/src/position/position.rs +++ b/position/src/position/position.rs @@ -9,7 +9,7 @@ use crate::{ sight::SightExt, }; use chessfriend_bitboard::BitBoard; -use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square}; +use chessfriend_core::{Color, Piece, PlacedPiece, Rank, Shape, Square}; use std::{cell::OnceCell, fmt}; #[derive(Clone, Debug, Eq)] @@ -182,10 +182,25 @@ impl Position { Pieces::new(&self, color) } - pub fn en_passant_square(&self) -> Option { + /// 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 { self.en_passant_square } + /// 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 { + 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!(), + }) + } + fn _sight_of_player(&self, player: Color, pieces: &PieceBitBoards) -> BitBoard { let en_passant_square = self.en_passant_square;