Exemplo n.º 1
0
def get_board_state(board: chess.Board) -> np.ndarray:
    """Function adapted from chess.Board.__str__() to generate a numpy array representing the board
    state"""
    # 6 pieces * 2 colors + 2 for en passant and castling. 8*8 fields of the board.
    builder = np.zeros([12 + 2, 8 * 8])
    for square in SQUARES_180:

        piece = board.piece_at(square)

        if piece:
            piece_int = PIECE_INT_LOOKUP[piece.symbol()]
            builder[piece_int, square] = 1

    builder = builder.reshape([14, 8, 8])

    if board.has_legal_en_passant():
        set_en_passant(board=board, builder=builder)

    # As the SQUARES_180 is flipped I need to flip it back
    builder = np.flip(builder, 1)

    if board.has_castling_rights(color=True) or board.has_castling_rights(color=False):
        castling_ints = get_castling_ints(castling_fen=board.castling_shredder_fen())
        builder[13, 0, castling_ints] = 1

    return builder