Skip to content

Commit

Permalink
add Setup::mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Sep 1, 2024
1 parent f8373c5 commit 55a217a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,18 @@ impl Board {
self.transform(Bitboard::rotate_270);
}

/// Makes black pieces white and vice versa.
/// Swap piece colors, making black pieces white and vice versa.
pub fn swap_colors(&mut self) {
self.by_color.swap();
}

/// Mirror the board vertically and swap piece colors, so that the resulting
/// board is equivalent modulo color.
pub fn mirror(&mut self) {
self.flip_vertical();
self.swap_colors();
}

pub fn pop_front(&mut self) -> Option<(Square, Piece)> {
self.occupied
.first()
Expand Down
3 changes: 2 additions & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ pub trait Position {
}
}

/// Swap turns. This is sometimes called "playing a null move".
/// Swap turns and discard en passant rights. This is sometimes called
/// "playing a null move".
///
/// # Errors
///
Expand Down
20 changes: 20 additions & 0 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,31 @@ impl Setup {
}
}

/// Swap turns and discard en passant rights. This is sometimes called
/// "playing a null move".
pub fn swap_turn(&mut self) {
self.turn = !self.turn;
self.ep_square = None;
}

/// Mirror vertically and swap turns and all piece colors, so that the
/// resulting setup is equivalent modulo color.
///
/// Move counters remain unchanged.
pub fn mirror(&mut self) {
self.board.mirror();
self.promoted = self.promoted.flip_vertical();
if let Some(pockets) = &mut self.pockets {
pockets.swap();
}
self.turn = !self.turn;
self.castling_rights = self.castling_rights.flip_vertical();
self.ep_square = self.ep_square.map(Square::flip_vertical);
if let Some(remaining_checks) = &mut self.remaining_checks {
remaining_checks.swap();
}
}

pub fn position<P: FromSetup>(self, mode: CastlingMode) -> Result<P, PositionError<P>> {
P::from_setup(self, mode)
}
Expand Down

0 comments on commit 55a217a

Please sign in to comment.