Answers.Body: <p>The pseudocode you copied from the wiki is just the easiest way to make you understand SEE. It is <strong>NOT</strong> used in a real chess engine, because building a <code>swap list</code> would be more efficient.</p>
  
  <p>You should check Stockfish. I'll give you some hints here.</p>
  
  <ol>
  <li>Goto <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp" rel="nofollow">position.cpp</a>.</li>
  <li>Find the function <code>Value Position::see(Move m) const</code>. This is where SEE is implemented</li>
  <li><p>Check this loop:</p>
  
  <pre><code>do {
    assert(slIndex &lt; 32);
  
    // Add the new entry to the swap list
    swapList[slIndex] = -swapList[slIndex - 1] + PieceValue[MG][captured];
  
    // Locate and remove the next least valuable attacker
    captured = min_attacker&lt;PAWN&gt;(byTypeBB, to, stmAttackers, occupied, attackers);
    stm = ~stm;
    stmAttackers = attackers &amp; pieces(stm);
    ++slIndex;
  
  } while (stmAttackers &amp;&amp; (captured != KING || (--slIndex, false))); // Stop before a king capture
  </code></pre></li>
  </ol>
  
  <p>Stockfish doesn't do exactly what your pseudocode does. It loops for all for possible attackers from the least powerful (<code>min_attacker</code>). For each attacker, the engine checks it's value (<code>PieceValues[MG]</code>) and add it to the swap list. The swap list will then be looped over for nega-max:</p>
  
  <pre><code>  // Having built the swap list, we negamax through it to find the best
    // achievable score from the point of view of the side to move.
    while (--slIndex)
        swapList[slIndex - 1] = std::min(-swapList[slIndex], swapList[slIndex - 1]);
  </code></pre>
  
  <p>Don't be afraid of the while loop, all it does to check if an exchange is good or not. In fact, the wikipedia you stated has the reasoning:</p>
  
  <p><em>This uses a trick, equivalent to negamax in tree search, where the loss for the current side is the gain for the opposite side. This can be seen in the expression piece_just_captured() - see(square); which is the value of the piece captured (piece_just_captured()) minus the gain that the opponent might make after the move by recapturing. If that term becomes negative, one would better choose standing pat rather than to capture, which can be done by a conditional assignment, or by a max function with zero as second argument.</em></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-28T12:52:29.300Z
Answers.LastActivityDate: 2016-03-28T12:52:29.300Z
Answers.UserDisplayName: SmallChess
Body: <p>I <a href="http://mediocrechess.blogspot.ca/2007/03/guide-static-exchange-evaluation-see.html" rel="nofollow">read</a> about a <a href="https://chessprogramming.wikispaces.com/Static+Exchange+Evaluation" rel="nofollow">SEE</a> without move/undo but they didn't share any simple pseudo code. If the next ply isn't calculated, cant see the recursive nature, Any speculators how this would work without move/undo? </p>
  
  <pre><code>int see(int square, int side)
  {
     value = 0;
     piece = get_smallest_attacker(square, side);
     /* skip if the square isn't attacked anymore by this side */
     if ( piece )
     {
        make_capture(piece, square);
        /* Do not consider captures if they lose material, therefor max zero */
        value = max (0, piece_just_captured() -see(square, other(side)) );
        undo_capture(piece, square);
     }
     return value;
  }
  </code></pre>
  
Tags: <engines><programming>
Title: Static Exchange Evaluation (SEE) without move undo
ThreadText: # Static Exchange Evaluation (SEE) without move undo
  
  (Asked by: username_0 on 2016-03-26)
  
  I read about a SEE without move/undo but they didn't share any simple pseudo code. If the next ply isn't calculated, cant see the recursive nature, Any speculators how this would work without move/undo? 
  int see(int square, int side)
  {
     value = 0;
     piece = get_smallest_attacker(square, side);
     /* skip if the square isn't attacked anymore by this side */
     if ( piece )
     {
        make_capture(piece, square);
        /* Do not consider captures if they lose material, therefor max zero */
        value = max (0, piece_just_captured() -see(square, other(side)) );
        undo_capture(piece, square);
     }
     return value;
  }
  
  **Tags:** engines, programming
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-03-28. Score: 2 | Accepted answer
  
  The pseudocode you copied from the wiki is just the easiest way to make you understand SEE. It is NOT used in a real chess engine, because building a swap list would be more efficient.
  You should check Stockfish. I'll give you some hints here.
  
  Goto position.cpp.
  Find the function Value Position::see(Move m) const. This is where SEE is implemented
  Check this loop:
  do {
    assert(slIndex < 32);
  
    // Add the new entry to the swap list
    swapList[slIndex] = -swapList[slIndex - 1] + PieceValue[MG][captured];
  
    // Locate and remove the next least valuable attacker
    captured = min_attacker(byTypeBB, to, stmAttackers, occupied, attackers);
    stm = ~stm;
    stmAttackers = attackers & pieces(stm);
    ++slIndex;
  
  } while (stmAttackers && (captured != KING || (--slIndex, false))); // Stop before a king capture
  
  
  Stockfish doesn't do exactly what your pseudocode does. It loops for all for possible attackers from the least powerful (min_attacker). For each attacker, the engine checks it's value (PieceValues[MG]) and add it to the swap list. The swap list will then be looped over for nega-max:
    // Having built the swap list, we negamax through it to find the best
    // achievable score from the point of view of the side to move.
    while (--slIndex)
        swapList[slIndex - 1] = std::min(-swapList[slIndex], swapList[slIndex - 1]);
  
  Don't be afraid of the while loop, all it does to check if an exchange is good or not. In fact, the wikipedia you stated has the reasoning:
  This uses a trick, equivalent to negamax in tree search, where the loss for the current side is the gain for the opposite side. This can be seen in the expression piece_just_captured() - see(square); which is the value of the piece captured (piece_just_captured()) minus the gain that the opponent might make after the move by recapturing. If that term becomes negative, one would better choose standing pat rather than to capture, which can be done by a conditional assignment, or by a max function with zero as second argument.
  
  
  ---
  
  