# how to evaluate the position inside the search function

using PVS Algorithm with Quiet search and zero window search what should the range of the score be (-infinity, infinity ) could it be as small as (-16000,16000) and what should the search return if it's a stalemate or checkmate for me or for the other side ? I tried reading the source code of different engines but there is too much addition(heuristics and such) that make it too difficult for me to understand anything.

the question in another for given this PVS algorithm how and where should I place checking for chaeck/stale mate and what score should I return ?

``````int PVS(alfa,beta,depthleft) {
if( depthleft <= 0 ) return qsearch(alpha, beta);

// using fail soft with negamax:
make first move
bestscore = -PVS(-beta, -alpha, depthleft-1);
unmake first move
if( bestscore > alpha ) {
if( bestscore >= beta )
return bestscore;
alpha = bestscore;
}

for( all remaining moves ) {
make move
score = -PVS(-alpha-1, -alpha, depthleft-1); // alphaBeta or zwSearch
if( score > alpha && score < beta ) {
// research with window [alpha;beta]
score = -PVS(-beta, -alpha, depthleft-1);
if( score > alpha )
alpha = score;
}
unmake move
if( score > bestscore ) {
if( score >= beta )
return score;
bestscore = score;
}
}
return bestscore;
}
``````

I wasn't sure where to post this question here or on stack overflow so please migrate it as you see fit

## 2 Answers

where should I place checking for chaeck/stale mate

Right before `make first move`: there is no first move!

• +1. Don't know why this got downvoted; it's exactly right. If there are no moves available, the position is a draw or a mate. You can't `make first move` if there is none. Commented May 27, 2015 at 17:44

You're supposed to return a negative value based on the ply to the mate. You should place it after the loop for scanning moves. If there's no move, it must be a stalemate or checkmate. Refer to the code in Stockfish as listed below:

``````   // Step 20. Check for mate and stalemate
// All legal moves have been searched and if there are no legal moves, it
// must be mate or stalemate. If we are in a singular extension search then
// return a fail low score.
if (!moveCount)
bestValue = excludedMove ? alpha
:     inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
``````

and

``````inline Value mated_in(int ply) {
return -VALUE_MATE + ply;
}
``````
• What is the purpose of using that return value? Is this a generally recognized convention (e.g., to let a GUI or something recognize and announce "Mate in N moves"), or is it just something that Stockfish does for it's own reasons? Commented Apr 27, 2015 at 16:45
• Or (thinking about it a bit more...), is this so that the search will find a quicker mate if one is available? Commented Apr 27, 2015 at 16:55
• Sorry. I don't really understand your question... Do you mean VALUE_MATE? Commented May 3, 2015 at 12:25
• You're right. VALUE_MATE is just a constant for anything that a not-mate position wouldn't give you. The ply is indeed to prefer shorter mates. :-) Commented May 4, 2015 at 0:12
• @patbarron - the reason for basing the value on the number of moves to mate is so that the engine picks the shortest mate when there's more than one. It doesn't affect the outcome of the game, it just keeps the engine from looking stupid. It's also a complication that's better ignored when you're just getting started. Commented Aug 25, 2015 at 12:07