Closed oscardssmith closed 5 years ago
Agreed, that sounds like a useful feature.
Some groundwork is laid already: There's an undocumented board.transform(f)
that takes a function that changes bitboards. For example flipped_vertically = board.transform(chess.bswap)
. It remains to implement functions that flip a bitboard horizontally or on diagonals, and make these discoverable.
These aren't especially efficient but they work
def flip_horiz():
piece_map = board.piece_map()
new_map = {i^7: p for i, p in piece_map.items()}
board.set_piece_map(new_map)
def flip_vert():
piece_map = board.piece_map()
new_map = {i^56: p for i, p in piece_map.items()}
board.set_piece_map(new_map)
def flip_diag()
piece_map = board.piece_map()
new_map = {8*(i%8) + i//8 : p for i, p in piece_map.items()}
board.set_piece_map(new_map)
Thanks. I added bitboard based implementations. Your functions are now equivalent to:
def flip_horiz(board):
board.apply_transform(chess.flip_horizontal)
def flip_vert(board):
board.apply_transform(chess.flip_vertical)
def flip_diag(board):
board.apply_transform(chess.flip_diag)
def flip_anti_diag(board):
board.apply_transform(chess.flip_anti_diag)
Example:
>>> import chess
>>> s = chess.SquareSet(0x1e2222120e0a1222)
>>> print(s)
. 1 1 1 1 . . .
. 1 . . . 1 . .
. 1 . . . 1 . .
. 1 . . 1 . . .
. 1 1 1 . . . .
. 1 . 1 . . . .
. 1 . . 1 . . .
. 1 . . . 1 . .
>>> print(chess.flip_horizontal(s))
. . . 1 1 1 1 .
. . 1 . . . 1 .
. . 1 . . . 1 .
. . . 1 . . 1 .
. . . . 1 1 1 .
. . . . 1 . 1 .
. . . 1 . . 1 .
. . 1 . . . 1 .
>>> print(chess.flip_vertical(s))
. 1 . . . 1 . .
. 1 . . 1 . . .
. 1 . 1 . . . .
. 1 1 1 . . . .
. 1 . . 1 . . .
. 1 . . . 1 . .
. 1 . . . 1 . .
. 1 1 1 1 . . .
>>> print(chess.flip_diag(s))
. . . . . . . .
. . . . . . . .
1 . . . . 1 1 .
. 1 . . 1 . . 1
. . 1 1 . . . 1
. . . 1 . . . 1
1 1 1 1 1 1 1 1
. . . . . . . .
>>> print(chess.flip_anti_diag(s))
. . . . . . . .
1 1 1 1 1 1 1 1
1 . . . 1 . . .
1 . . . 1 1 . .
1 . . 1 . . 1 .
. 1 1 . . . . 1
. . . . . . . .
. . . . . . . .
thanks so much for all your help so far
It would be really nice to have functions to flip a board vertically, horizontally, or along a diagonal. These are fairly simple conceptual operations, but a pain to implement when you want them.