Exemplo n.º 1
0
def generate_pawn_blocker_masks():
    """Mask to get the square directly in front of a pawn"""
    def create_blocker(square, direction):
        x, y = get_coordinate(square)
        blockerSquare = get_square(x, y + direction)
        return Bitboard(blockerSquare) if 0 <= blockerSquare < 64 else 0

    whiteMasks, blackMasks = {}, {}
    for square in range(64):
        squareBitboard = Bitboard(square)
        whiteMasks[squareBitboard] = create_blocker(square, -1)
        blackMasks[squareBitboard] = create_blocker(square, 1)

    return whiteMasks, blackMasks
Exemplo n.º 2
0
def generate_ray_masks(reverse=False):
    """Generates file, rank, diagonal, and antidiagonal masks

  Example Diagonal Mask       Example Antidiagonal Mask

    00000010                        10000000
    00000100                        01000000
    00001000                        00100000
    00100000                        00010000
    01000000                        00001000
    10000000                        00000100
    00000000                        00000010
    00000000                        00000001

  Example File Mask               Example Rank Mask

    00000010                        00000000
    00000010                        11111111
    00000010                        00000000
    00000010                        00000000
    00000010                        00000000
    00000010                        00000000
    00000010                        00000000
    00000010                        00000000
  """
    # create a mask for each rank
    rankMaskList = []
    for rank in range(8):
        rankStartIndex = rank * 8
        rankSquares = range(rankStartIndex, rankStartIndex + 8)
        rankMask = Bitboard(*rankSquares)
        rankMaskList.append(rankMask)

    # create a mask for each file
    fileMaskList = []
    for file in range(8):
        fileSquares = range(file, 64, 8)
        fileMask = Bitboard(*fileSquares)
        fileMaskList.append(fileMask)

    maskLists = [
        rankMaskList, fileMaskList, DIAGONAL_MASK_LIST, ANTI_DIAGONAL_MASK_LIST
    ]

    # iterate through each square on the board and add the rank, file,
    # diagonal, and antidiagonal masks that the square is a part of. This
    # way, a square's mask index does not need to be computed for retreival
    maskDicts = (rankMasks, fileMasks, diagonalMasks,
                 antiDiagonalMasks) = {}, {}, {}, {}

    for square in range(64):
        fileIndex, rankIndex = get_coordinate(square)
        diagonalIndex = (square // 8) + (square % 8)
        antiDiagonalIndex = (square // 8) + 7 - (square % 8)
        maskIndecies = [rankIndex, fileIndex, diagonalIndex, antiDiagonalIndex]

        for dict, list, index in zip(maskDicts, maskLists, maskIndecies):
            mask = list[index]
            if reverse:
                dict[Bitboard(square)] = reverse_bitboard(mask)
            else:
                dict[Bitboard(square)] = mask

    labels = ['ranks', 'files', 'diagonals', 'antidiagonals']
    if reverse: labels = ['reversed' + label.title() for label in labels]

    return {label: masks for label, masks in zip(labels, maskDicts)}
Exemplo n.º 3
0
def create_mask(*squareNotationList):
    return Bitboard(*map(parse_notation, squareNotationList))
Exemplo n.º 4
0
def generate_center_file_masks():
    fileMasks = generate_ray_masks()['files']
    return fileMasks[Bitboard(3)], fileMasks[Bitboard(4)]
Exemplo n.º 5
0
 def create_blocker(square, direction):
     x, y = get_coordinate(square)
     blockerSquare = get_square(x, y + direction)
     return Bitboard(blockerSquare) if 0 <= blockerSquare < 64 else 0
Exemplo n.º 6
0
def generate_reversed_square_masks():
    reversedSquares = {}
    for square in range(64):
        squareBitboard = Bitboard(square)
        reversedSquares[squareBitboard] = reverse_bitboard(squareBitboard)
    return reversedSquares
Exemplo n.º 7
0
 def movesets(self):
   bitboards = [Bitboard(*squares) for squares in self.endSquareSets]
   return bitboards[0] if len(self.endSquareSets) == 1 else bitboards
Exemplo n.º 8
0
 def moves(self):
   allSquares = sum(self.endSquareSets, [])
   return [Bitboard(square) for square in allSquares]
Exemplo n.º 9
0
 def key(self):
   return Bitboard(self.startSquare)