Skip to content

Board

Board

deepcopy(self)

Duplicate the board with the current pieces positions Will be used for board evaluation and traversing tree search engines

Returns:

Type Description
Board

the same duplicated board as the current object

Source code in beth\board.py
def deepcopy(self):
    """Duplicate the board with the current pieces positions
    Will be used for board evaluation and traversing tree search engines 

    Returns:
        Board: the same duplicated board as the current object
    """
    return deepcopy(self)

get_pieces_positions_by_type(self, piece_type, color=None)

Parse the bitboard representation to get pieces positions on the board

Parameters:

Name Type Description Default
piece_type str

the name of the piece in uppercase

required
color str

WHITE or BLACK. Defaults to None.

None

Exceptions:

Type Description
Exception

if the name of the piece is not recognized (practical to debug)

Returns:

Type Description
list

list: The position (between 0 and 63) of the piece type selected

Source code in beth\board.py
def get_pieces_positions_by_type(self, piece_type: str, color: str = None) -> list:
    """Parse the bitboard representation to get pieces positions on the board

    Args:
        piece_type (str): the name of the piece in uppercase
        color (str, optional): WHITE or BLACK. Defaults to None.

    Raises:
        Exception: if the name of the piece is not recognized (practical to debug)

    Returns:
        list: The position (between 0 and 63) of the piece type selected
    """

    # Prepare binary representation of pieces
    piece_type = piece_type.upper()
    if piece_type == "BISHOP":
        pieces = self.bishops
    elif piece_type == "PAWN":
        pieces = self.pawns
    elif piece_type == "ROOK":
        pieces = self.rooks
    elif piece_type == "KNIGHT":
        pieces = self.knights
    elif piece_type == "QUEEN":
        pieces = self.queens
    elif piece_type == "KING":
        pieces = self.kings
    else:
        raise Exception(f"Piece type {piece_type} is not among {PIECES}")

    # Prepare binary color mask
    if color is None:
        mask = self.occupied
    else:
        if isinstance(color, str):
            color = 0 if color.upper() == "WHITE" else 1
        mask = self.occupied_co[1-color]

    return list(scan_forward(pieces & mask))