4

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.

Brian Towers
  • 92,895
  • 10
  • 227
  • 372
Arman
  • 171
  • 1
  • 5

2 Answers2

6

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.

AndrewGrant
  • 449
  • 3
  • 9
6

To convert a single bitboard to a numpy array:

def bitboard_to_array(bb: int) -> np.ndarray:
    s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
    b = (bb >> s).astype(np.uint8)
    b = np.unpackbits(b, bitorder="little")
    return b.reshape(8, 8)

To convert multiple bitboards to a numpy array:

def bitboards_to_array(bb: np.ndarray) -> np.ndarray:
    bb = np.asarray(bb, dtype=np.uint64)[:, np.newaxis]
    s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
    b = (bb >> s).astype(np.uint8)
    b = np.unpackbits(b, bitorder="little")
    return b.reshape(-1, 8, 8)

Example usage:

import chess
import numpy as np

board = chess.Board()

black, white = board.occupied_co

bitboards = np.array([
    black & board.pawns,
    black & board.knights,
    black & board.bishops,
    black & board.rooks,
    black & board.queens,
    black & board.kings,
    white & board.pawns,
    white & board.knights,
    white & board.bishops,
    white & board.rooks,
    white & board.queens,
    white & board.kings,
], dtype=np.uint64)

board_array = bitboards_to_array(bitboards)

Which gives:

>>> board_array.shape
(12, 8, 8)

>>> black_pawns = board_array[0]
... print(black_pawns)
... 
[[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]]

>>> black_kings = board_array[5]
... print(black_kings)
... 
[[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]]
Mateen Ulhaq
  • 1,244
  • 10
  • 18