Answers.Body: <blockquote>
    <p>where should I place checking for chaeck/stale mate</p>
  </blockquote>
  
  <p>Right before <code>make first move</code>: there is no first move!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-27T17:44:32.637Z
Answers.Text: +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.
Answers.UserDisplayName: Pete Becker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-26T00:38:43.870Z
Answers.LastActivityDate: 2015-02-26T00:38:43.870Z
Answers.UserDisplayName: user58697
Answers.Body: <p>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:</p>
  
  <pre><code>   // 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-&gt;ply) : DrawValue[pos.side_to_move()];
  </code></pre>
  
  <p>and</p>
  
  <pre><code>inline Value mated_in(int ply) {
    return -VALUE_MATE + ply;
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-27T16:45:04.190Z
Answers.Text: 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?
Answers.UserDisplayName: patbarron
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-27T16:55:59.783Z
Answers.Text: Or (thinking about it a bit more...), is this so that the search will find a quicker mate if one is available?
Answers.UserDisplayName: patbarron
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-03T12:25:08.650Z
Answers.Text: Sorry. I don't really understand your question... Do you mean VALUE_MATE?
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-03T18:08:50.647Z
Answers.Text: Yes - I was trying to understand why any position that leads to mate returns any evaluation other than VALUE_MATE, and whether adding/subtracting the ply was to make search prefer shorter mates.  In the interim, I found https://chessprogramming.wikispaces.com/Score, which seems to indicate that this is indeed the case.
Answers.UserDisplayName: patbarron
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-04T00:12:15.457Z
Answers.Text: 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. :-)
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T12:07:42.447Z
Answers.Text: @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.
Answers.UserDisplayName: Pete Becker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-28T05:55:59.010Z
Answers.LastActivityDate: 2015-03-28T05:55:59.010Z
Answers.UserDisplayName: SmallChess
Body: <p>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.</p>
  
  <p>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 ?</p>
  
  <pre><code>int PVS(alfa,beta,depthleft) {
     if( depthleft &lt;= 0 ) return qsearch(alpha, beta);
  
  // using fail soft with negamax:
  make first move
  bestscore = -PVS(-beta, -alpha, depthleft-1);
  unmake first move
  if( bestscore &gt; alpha ) {
     if( bestscore &gt;= beta )
        return bestscore;
    alpha = bestscore;
  }
  
  for( all remaining moves ) {
    make move
    score = -PVS(-alpha-1, -alpha, depthleft-1); // alphaBeta or zwSearch
    if( score &gt; alpha &amp;&amp; score &lt; beta ) {
       // research with window [alpha;beta]
       score = -PVS(-beta, -alpha, depthleft-1);
       if( score &gt; alpha )
         alpha = score;
    }
    unmake move
    if( score &gt; bestscore ) {
       if( score &gt;= beta )
          return score;
       bestscore = score;
    }
  }
  return bestscore;
  }
  </code></pre>
  
  <p>I wasn't sure where to post this question here or on stack overflow so please migrate it as you see fit </p>
  
Tags: <engines><checkmate><evaluation><programming>
Title: how to evaluate the position inside the search function
ThreadText: # how to evaluate the position inside the search function
  
  (Asked by: username_0 on 2015-02-25)
  
  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
  
  **Tags:** engines, checkmate, evaluation, programming
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-02-26. Score: 1
  
  > where should I place checking for chaeck/stale mate
  
  Right before `make first move`: there is no first move!
  
  > **Comments:**
  > **username_2**: +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.  
  
  ---
  
  ### Answer by username_3 on 2015-03-28. Score: 0
  
  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;
  }
  
  > **Comments:**
  > **username_4**: 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?  
  > **username_4**: Or (thinking about it a bit more...), is this so that the search will find a quicker mate if one is available?  
  > **username_3**: Sorry. I don't really understand your question... Do you mean VALUE_MATE?  
  > **username_4**: Yes - I was trying to understand why any position that leads to mate returns any evaluation other than VALUE_MATE, and whether adding/subtracting the ply was to make search prefer shorter mates.  In the interim, I found https://chessprogramming.wikispaces.com/Score, which seems to indicate that this is indeed the case.  
  > **username_3**: 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. :-)  
  > **username_2**: @username_4 - 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.  
  
  ---
  
  