mcostalba wrote:Here is the code in SF:
Hi Marco,
I think the change you propose is not really what Lucas means with "loose pieces" the usual term, I don't use it myself much though, is as Gerd says "hanging pieces" The definition on Wikipedia for hanging pieces, hanging pawns are another matter, from
http://en.wikipedia.org/wiki/Glossary_of_chess
Hanging
Unprotected and exposed to capture. It is not the same as en prise since a piece en prise may be protected. To "hang a piece" is to lose it by failing to move or protect it
So if a pice is not defended by a pawn and there is already a bonus given for attacking it, the extra effort to see if there are non-pawn material defenders is not so great anymore, and in that case the piece really is "hanging". My own version of that code in Stockfish is a bit experimental and not very streamlined code (if the indents are wrong; I tried to fix them for about forty minutes, it probably has to do with original Stockfish code made in Unix and modified under Windows and some conflicting line endings still there):
Code: Select all
template<Color Us>
Score evaluate_threats(const Position& pos, EvalInfo& ei) {
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b;
Score score = SCORE_ZERO;
// Enemy pieces not defended by a pawn and under our attack
Bitboard weakEnemies = pos.pieces(Them)
& ~ei.attackedBy[Them][PAWN]
& ei.attackedBy[Us][0];
if (!weakEnemies)
return SCORE_ZERO;
// Add bonus according to type of attacked enemy piece [pt2] and to the
// type of attacking piece [pt1], from knights to queens. Kings are not
// considered under pt2 because that is already handled in king evaluation.
for (PieceType pt1 = KNIGHT; pt1 <= KING; pt1++)
{
b = ei.attackedBy[Us][pt1] & weakEnemies;
if (b)
for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
{
Bitboard bb = b & pos.pieces(pt2);
if (bb)
{
score += ThreatBonus[pt1][pt2];
do { // Is the piece undefended?
Square s = pop_1st_bit(&bb);
if (!(ei.attackedBy[Them][0] & s))
{
score += ThreatBonus[pt1][pt2];
if (pt2 == PAWN && (!pos.square_is_empty(s + pawn_push(Them)) || (ei.attackedBy[Us][0] & s + pawn_push(Them))))
{
score += make_score(5, 10);
// Nearing a pawn ending and the pawn may become a candidate pawn or a passed pawn?
File f = file_of(s);
if (ei.pi->file_is_half_open(Us, f) && !pos.non_pawn_material(Them))
score += make_score(0, 2*relative_rank(Them, s));
}
}
// Does the piece attack our king?
if ((ei.kingAttackersBB[Us] & s))
score += make_score(25, 0);
} while (bb);
}
}
}
return score;
}
Some of the extra bonuses introduced are just small and may not make much of a difference.
Eelco