Example #1
0
def evaluate(board: chess.Board, turn: bool):

    if board.is_checkmate():
        score = 10000 - board.ply()                                
        if turn != board.turn:        
            score = -score                
        return score                        

    score = 0
    
    # Iterate over all 64 squares of board    
    for square in chess.SQUARES: 

        piece = board.piece_type_at(square)  # 1 .. 6 or None   

        if not piece: continue
            
        # Calculate material and positional score            
        if board.color_at(square) == chess.WHITE:
            score += pieceWeights[piece]
            score += positionWeights[square]                
        else:    
            score -= pieceWeights[piece]
            score -= positionWeights[square]

    if board.turn == chess.BLACK:
        score = -score
                    
    return score                        
Example #2
0
    def evaluate(self, board: chess.Board) -> int:
        """
        Evaluate board via. piece position/value tables in addition
        to material differences

        Args:
            board (chess.Board): board state to evaluate
        Returns:
            (int)
        """
        val = 0
        for square in chess.SQUARES:
            piece = board.piece_type_at(square)
            color = board.color_at(square)

            if piece:
                # Get base piece value, add position based value from
                # piece value table
                piece_value = self.piece_values[PIECE_NAMES[piece]].value
                piece_value += get_piece_value_from_table(
                    PIECE_NAMES[piece], color, square, self.value_tables)
                if not color:
                    piece_value *= -1
                val += piece_value
        self.evaluations[board.fen()] = val

        return val
Example #3
0
 def get_piece_locations(self, curr_board: chess.Board):
     lop = curr_board.piece_map()
     own_lop = {}
     for p in lop:
         if curr_board.color_at(p) == self.color:
             own_lop[p] = lop[p]
     return own_lop
Example #4
0
def get_attacked_pieces(board: chess.Board, defending_color: chess.Color):

    attacked_pieces = { }

    for square, piece in board.piece_map().items():

        if board.color_at(square) != defending_color:
            continue

        attacking_pieces = get_attacking_pieces(board, not defending_color, square)
        if len(attacking_pieces) > 0:

            defending_pieces = get_defending_pieces(board, defending_color, square)
            attacked_pieces[square] = (piece.piece_type, attacking_pieces, defending_pieces)

    return attacked_pieces
Example #5
0
    def evaluate(self, board: chess.Board) -> int:
        """
        Evaluate boardstate via. material difference on board

        Args:
            board (chess.Board): board state to evaluate
        Returns:
            (int)
        """
        val = 0
        for square in chess.SQUARES:
            # For each piece on board, get value of piece on board.
            piece = board.piece_type_at(square)
            color = board.color_at(square)
            if piece:
                piece_value = self.piece_values[PIECE_NAMES[piece]].value
                if not color:
                    # BLACK encoded as False
                    piece_value *= -1
                val += piece_value
        self.evaluations[board.fen()] = val

        # Return difference in piece values between white & black
        return val
Example #6
0
def evaluateFuzzy(board: chess.Board, evaluator: Callable[[float, float],
                                                          float]):
    player = chess.WHITE if board.turn == True else chess.BLACK
    opponent = chess.WHITE if player == chess.BLACK else chess.BLACK

    total = 0
    # being in check usually means we are going the wrong route
    if board.is_check():
        total -= 1000

    if board.has_insufficient_material(player):
        total -= 1000

    if board.has_insufficient_material(opponent):
        total += 1000

    num_moves_by_piece = {}
    for move in board.legal_moves:
        if move.from_square in num_moves_by_piece:
            num_moves_by_piece[move.from_square] += 1
        else:
            num_moves_by_piece[move.from_square] = 1

    for i in range(1, 7):
        piece_type = board.pieces(i, player)
        num_attacking = 0
        for piece in piece_type:
            movable_spaces = num_moves_by_piece[
                piece] if piece in num_moves_by_piece else 0
            for spot in board.attacks(piece):
                attacked_player = board.color_at(spot)
                if attacked_player == opponent:
                    num_attacking += 1
            total += i + evaluator(movable_spaces, num_attacking)

    return total