1
0
Fork 0

remove redundant methods for positions

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-03 16:28:19 +01:00
parent 263bb0ddb6
commit bcd5a3182a
2 changed files with 39 additions and 72 deletions

View file

@ -76,34 +76,6 @@ impl Position {
})
}
/// Tries to read a valid position from a text record.
///
/// This is a shortcut for parsing and validating a [`Setup`]:
/// ```
/// # use eschac::setup::Setup;
/// # |s: &str| -> Option<eschac::position::Position> {
/// Setup::from_text_record(s).ok().and_then(|pos| pos.into_position().ok())
/// # };
/// ```
#[inline]
pub fn from_text_record(s: &str) -> Option<Self> {
Setup::from_text_record(s)
.ok()
.and_then(|pos| pos.into_position().ok())
}
/// Returns the text record of the position.
///
/// This is a shortcut for:
/// ```
/// # |position: eschac::position::Position| {
/// position.as_setup().to_text_record()
/// # };
#[inline]
pub fn to_text_record(&self) -> String {
self.as_setup().to_text_record()
}
/// Returns all the legal moves on the position.
#[inline]
pub fn legal_moves<'l>(&'l self) -> Moves<'l> {
@ -171,33 +143,6 @@ impl Position {
self.0.en_passant = OptionSquare::None;
}
/// Returns the occupancy of a square.
#[inline]
pub fn get(&self, square: Square) -> Option<Piece> {
self.0.get(square)
}
/// Returns the color whose turn it is to play.
#[inline]
pub fn turn(&self) -> Color {
self.0.turn()
}
/// Returns `true` if castling is available for the given color and side.
#[inline]
pub fn castling_rights(&self, color: Color, side: CastlingSide) -> bool {
self.0.castling_rights(color, side)
}
/// Returns the en passant target square if it exists.
///
/// Note that if an en passant target square exists, it does not mean that taking en passant is
/// legal or even pseudo-legal.
#[inline]
pub fn en_passant_target_square(&self) -> Option<Square> {
self.0.en_passant_target_square()
}
/// Discards the castling rights for the given color and side.
#[inline]
pub fn remove_castling_rights(&mut self, color: Color, side: CastlingSide) {
@ -420,14 +365,14 @@ impl Position {
SanInner::Castle(CastlingSide::Short) => (
Role::King,
Role::King,
Square::from_coords(File::E, self.turn().home_rank()).bitboard(),
Square::from_coords(File::G, self.turn().home_rank()).bitboard(),
Square::from_coords(File::E, self.0.turn().home_rank()).bitboard(),
Square::from_coords(File::G, self.0.turn().home_rank()).bitboard(),
),
SanInner::Castle(CastlingSide::Long) => (
Role::King,
Role::King,
Square::from_coords(File::E, self.turn().home_rank()).bitboard(),
Square::from_coords(File::C, self.turn().home_rank()).bitboard(),
Square::from_coords(File::E, self.0.turn().home_rank()).bitboard(),
Square::from_coords(File::C, self.0.turn().home_rank()).bitboard(),
),
SanInner::Normal {
role,

View file

@ -13,7 +13,10 @@ static P6: &'static str = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QP
fn recursive_check_aux(position: Position, depth: usize) {
assert_eq!(
position,
Position::from_text_record(&position.as_setup().to_text_record()).unwrap(),
Setup::from_text_record(&position.as_setup().to_text_record())
.unwrap()
.into_position()
.unwrap(),
);
if let Some(passed) = position.pass() {
@ -27,20 +30,25 @@ fn recursive_check_aux(position: Position, depth: usize) {
for square in Square::all() {
setup.set(
square.mirror(),
position.get(square).map(|piece| Piece {
position.as_setup().get(square).map(|piece| Piece {
role: piece.role,
color: !piece.color,
}),
);
}
setup.set_turn(!position.turn());
setup.set_turn(!position.as_setup().turn());
for color in Color::all() {
for side in CastlingSide::all() {
setup.set_castling_rights(!color, side, position.castling_rights(color, side));
setup.set_castling_rights(
!color,
side,
position.as_setup().castling_rights(color, side),
);
}
}
setup.set_en_passant_target_square(
position
.as_setup()
.en_passant_target_square()
.map(|square| square.mirror()),
);
@ -69,7 +77,13 @@ fn recursive_check_aux(position: Position, depth: usize) {
}
}
fn recursive_check(record: &str) {
recursive_check_aux(Position::from_text_record(record).unwrap(), 4);
recursive_check_aux(
Setup::from_text_record(record)
.unwrap()
.into_position()
.unwrap(),
4,
);
}
#[test]
fn recursive_check_1() {
@ -218,17 +232,22 @@ fn setup() {
#[test]
fn mirror() {
assert_eq!(Position::new().pass(), Some(Position::new().mirror()));
let position =
Position::from_text_record("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b Kq e3")
let position = Setup::from_text_record("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b Kq e3")
.unwrap()
.into_position()
.unwrap();
let mirror =
Position::from_text_record("rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6")
let mirror = Setup::from_text_record("rnbqkbnr/pppp1ppp/8/4p3/8/8/PPPPPPPP/RNBQKBNR w Qk e6")
.unwrap()
.into_position()
.unwrap();
assert_eq!(mirror, position.mirror());
}
fn perft_aux(record: &str, tests: &[u128]) {
let position = Position::from_text_record(record).unwrap();
let position = Setup::from_text_record(record)
.unwrap()
.into_position()
.unwrap();
for (depth, value) in tests.iter().copied().enumerate() {
assert_eq!(
position.perft(depth),
@ -267,7 +286,10 @@ fn perft_6() {
#[test]
fn san() {
let position = Position::from_text_record("8/2KN1p2/5p2/3N1B1k/5PNp/7P/7P/8 w - -").unwrap();
let position = Setup::from_text_record("8/2KN1p2/5p2/3N1B1k/5PNp/7P/7P/8 w - -")
.unwrap()
.into_position()
.unwrap();
let san1 = "N7xf6#".parse::<San>().unwrap();
let m1 = san1.to_move(&position).unwrap();
let san2 = "N5xf6#".parse::<San>().unwrap();