Skip to content

Commit

Permalink
Merge f1480fc into 45c823d
Browse files Browse the repository at this point in the history
  • Loading branch information
kraktus committed Aug 31, 2020
2 parents 45c823d + f1480fc commit c023825
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 28 additions & 7 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,21 +1227,28 @@ def transform(self: BaseBoardT, f: Callable[[Bitboard], Bitboard]) -> BaseBoardT
:func:`chess.shift_right()`.
Alternatively, :func:`~chess.BaseBoard.apply_transform()` can be used
to apply the transformation in place.
to apply the transformation on the board.
"""
board = self.copy()
board.apply_transform(f)
return board

def apply_mirror(self: BaseBoardT) -> None:
self.apply_transform(flip_vertical)
self.occupied_co[WHITE], self.occupied_co[BLACK] = self.occupied_co[BLACK], self.occupied_co[WHITE]

def mirror(self: BaseBoardT) -> BaseBoardT:
"""
Returns a mirrored copy of the board.
The board is mirrored vertically and piece colors are swapped, so that
the position is equivalent modulo color.
Alternatively, :func:`~chess.BaseBoard.apply_mirror()` can be used
to mirror the board.
"""
board = self.transform(flip_vertical)
board.occupied_co[WHITE], board.occupied_co[BLACK] = board.occupied_co[BLACK], board.occupied_co[WHITE]
board = self.copy()
board.apply_mirror()
return board

def copy(self: BaseBoardT) -> BaseBoardT:
Expand Down Expand Up @@ -3437,17 +3444,31 @@ def __eq__(self, board: object) -> bool:
def apply_transform(self, f: Callable[[Bitboard], Bitboard]) -> None:
super().apply_transform(f)
self.clear_stack()
self.ep_square = None if self.ep_square is None else msb(f(BB_SQUARES[self.ep_square]))
self.castling_rights = f(self.castling_rights)

def transform(self: BoardT, f: Callable[[Bitboard], Bitboard]) -> BoardT:
board = self.copy(stack=False)
board.apply_transform(f)
board.ep_square = None if self.ep_square is None else msb(f(BB_SQUARES[self.ep_square]))
board.castling_rights = f(self.castling_rights)
return board

def apply_mirror(self: BoardT) -> None:
super().apply_mirror()
self.turn = not self.turn

def mirror(self: BoardT) -> BoardT:
board = super().mirror()
board.turn = not self.turn
"""
Returns a mirrored copy of the board.
The board is mirrored vertically and piece colors are swapped, so that
the position is equivalent modulo color. Also swap the "en passant"
square, castling rights and turn.
Alternatively, :func:`~chess.Board.apply_mirror()` can be used
to mirror the board.
"""
board = self.copy()
board.apply_mirror()
return board

def copy(self: BoardT, *, stack: Union[bool, int] = True) -> BoardT:
Expand Down
2 changes: 2 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,8 @@ def test_mirror(self):
board = chess.Board("r1bq1r2/pp2n3/4N2k/3pPppP/1b1n2Q1/2N5/PP3PP1/R1B1K2R w KQ g6 0 15")
mirrored = chess.Board("r1b1k2r/pp3pp1/2n5/1B1N2q1/3PpPPp/4n2K/PP2N3/R1BQ1R2 b kq g3 0 15")
self.assertEqual(board.mirror(), mirrored)
board.apply_mirror()
self.assertEqual(board, mirrored)

def test_chess960_pos(self):
board = chess.Board()
Expand Down

0 comments on commit c023825

Please sign in to comment.