2

I would like to convert a given chess board into its bitboard representation, but my implementation is rather slow. Here is what I'm doing (using the python-chess package):

board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1)

w_pawn = (np.asarray(board.pieces(chess.PAWN, chess.WHITE).tolist())).astype(int)
w_rook = (np.asarray(board.pieces(chess.ROOK, chess.WHITE).tolist())).astype(int)
w_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.WHITE).tolist())).astype(int)
w_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.WHITE).tolist())).astype(int)
w_queen = (np.asarray(board.pieces(chess.QUEEN, chess.WHITE).tolist())).astype(int)
w_king = (np.asarray(board.pieces(chess.KING, chess.WHITE).tolist())).astype(int)

b_pawn = (np.asarray(board.pieces(chess.PAWN, chess.BLACK).tolist())).astype(int)
b_rook = (np.asarray(board.pieces(chess.ROOK, chess.BLACK).tolist())).astype(int)
b_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.BLACK).tolist())).astype(int)
b_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.BLACK).tolist())).astype(int)
b_queen = (np.asarray(board.pieces(chess.QUEEN, chess.BLACK).tolist())).astype(int)
b_king = (np.asarray(board.pieces(chess.KING, chess.BLACK).tolist())).astype(int)

return np.concatenate((w_pawn, w_rook, w_knight, w_bishop, w_queen, w_king,
                       b_pawn, b_rook, b_knight, b_bishop, b_queen, b_king))

For the starting position this function returns:

[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]

Which is what I want. The only issue is the speed of this function.

  • If speed is important, use a fast programming language. – Mike Jones Apr 24 at 7:11
  • @MikeJones I'm trying to find a C++ alternative to the python-chess library. Do you have any suggestions? – Arman Apr 24 at 7:32
  • I've seen many chess libraries written in different languages, but none of them use bitboard representation. – Mike Jones Apr 24 at 11:17
2

python-chess already has the built in utilities to handle conversions. Here is an example:

import chess

board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')

WP = board.pieces(chess.PAWN, chess.WHITE)
BP = board.pieces(chess.PAWN, chess.BLACK)

print (int(WP), int (BP))

print (list(WP), list(BP))

print ("\nWHITE PAWNS:\n" + str(WP))

print ("\nBLACK PAWNS:\n" + str(BP))

Output:

65280 71776119061217280

[8, 9, 10, 11, 12, 13, 14, 15] [48, 49, 50, 51, 52, 53, 54, 55]

WHITE PAWNS:
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
1 1 1 1 1 1 1 1
. . . . . . . .

BLACK PAWNS:
. . . . . . . .
1 1 1 1 1 1 1 1
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

From a list, or from an integer, you can easily construct a Bitboard however you choose to define it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.