Answers.Body: <p>You generate all the legal king moves up front, regardless of whether there's a check. Then determine the number of checking pieces. There will be two scenarios relevant to the question.</p>
  <p>If it is a double check, the only legal moves are king moves out of check, so you simply return the existing list of moves</p>
  <p>Single check is trickier. There are multiple cases here depending on the checking piece:</p>
  <ul>
  <li><p>If the checker is a knight, simply generate all piece moves which capture this knight. Return the list of captures and existing king evasions</p>
  </li>
  <li><p>If the checker is a pawn, you do the same thing, <strong>but</strong> you also have to check if an e.p. move works.</p>
  </li>
  <li><p>For any other checking piece (the &quot;ray&quot; pieces: queen, bishop, rook), the most efficient way to do this for bitboard-based move generation is using masks. You generate two bitboards: one containing all the intermediate squares between the king and the checking piece, and one containing a single set bit for the location of the checking piece. If you're not storing quiet moves and captures separately, you can combine both masks. If there are no checks, this bitboard is simply the all-ones bitboard.</p>
  <p>Then, for every piece and pawn, you presumably generate a bitboard of target squares. You AND this with the mask(s) above, to restrict to legal check evasions. Then create the moves as usual, from this reduced bitboard.</p>
  </li>
  </ul>
  <p>A concrete implementation can be found in the <code>generate_legals</code> function in my (very optimised) chess move generator on <a href="https://github.com/nkarve/surge/blob/master/src/position.h" rel="noreferrer">GitHub</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-16T11:08:08.890Z
Answers.LastActivityDate: 2021-06-16T11:08:08.890Z
Answers.UserDisplayName: Nihar Karve
Body: <p>In my chess engine, I'm attempting to create a strictly legal move generator, instead of generating pseduo-legal moves, then discarding them later in the search and evaluation phase.</p>
  <p>So far, here is my planned approach:</p>
  <ul>
  <li>Remove pinned pieces from the board</li>
  <li>If the king is in check
  <ul>
  <li>Generate possible moves from the check evasion function</li>
  </ul>
  </li>
  <li>Otherwise
  <ul>
  <li>Generate possible moves for pinned pieces</li>
  <li>Generate possible moves for non-pinned pieces</li>
  </ul>
  </li>
  </ul>
  <p>The only part of this algorithm that I don't quite understand how to implement is the check evasion move generator. As I understand it, there are three ways of evading a check:</p>
  <ul>
  <li>Capture the checking piece</li>
  <li>Block the checking piece</li>
  <li>Move the king</li>
  </ul>
  <p>Now although I understand these three cases, I'm having a hard time trying to think of a way to implement them concretely and efficiently. What strategies are usually taken in creating a check evasion function? Also note, I'm working with bitboards.</p>
  
Tags: <engines><programming><computer-chess><chess-ai>
Title: How can a check evasion move generation algorithm be done efficiently?
ThreadText: # How can a check evasion move generation algorithm be done efficiently?
  
  (Asked by: username_0 on 2021-05-30)
  
  In my chess engine, I'm attempting to create a strictly legal move generator, instead of generating pseduo-legal moves, then discarding them later in the search and evaluation phase.
  
  So far, here is my planned approach:
  
  * Remove pinned pieces from the board
  * If the king is in check
    + Generate possible moves from the check evasion function
  * Otherwise
    + Generate possible moves for pinned pieces
    + Generate possible moves for non-pinned pieces
  
  The only part of this algorithm that I don't quite understand how to implement is the check evasion move generator. As I understand it, there are three ways of evading a check:
  
  * Capture the checking piece
  * Block the checking piece
  * Move the king
  
  Now although I understand these three cases, I'm having a hard time trying to think of a way to implement them concretely and efficiently. What strategies are usually taken in creating a check evasion function? Also note, I'm working with bitboards.
  
  **Tags:** engines, programming, computer-chess, chess-ai
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-16. Score: 6 | Accepted answer
  
  You generate all the legal king moves up front, regardless of whether there's a check. Then determine the number of checking pieces. There will be two scenarios relevant to the question.
  
  If it is a double check, the only legal moves are king moves out of check, so you simply return the existing list of moves
  
  Single check is trickier. There are multiple cases here depending on the checking piece:
  
  * If the checker is a knight, simply generate all piece moves which capture this knight. Return the list of captures and existing king evasions
  * If the checker is a pawn, you do the same thing, **but** you also have to check if an e.p. move works.
  * For any other checking piece (the "ray" pieces: queen, bishop, rook), the most efficient way to do this for bitboard-based move generation is using masks. You generate two bitboards: one containing all the intermediate squares between the king and the checking piece, and one containing a single set bit for the location of the checking piece. If you're not storing quiet moves and captures separately, you can combine both masks. If there are no checks, this bitboard is simply the all-ones bitboard.
  
    Then, for every piece and pawn, you presumably generate a bitboard of target squares. You AND this with the mask(s) above, to restrict to legal check evasions. Then create the moves as usual, from this reduced bitboard.
  
  A concrete implementation can be found in the `generate_legals` function in my (very optimised) chess move generator on [GitHub](https://github.com/nkarve/surge/blob/master/src/position.h).
  
  
  ---
  
  