Answers.Body: <p>This is my understanding:</p>
  
  <ul>
  <li>You have a pseudo-legal move generator</li>
  <li>Now, you want to get rid of the illegal moves</li>
  </ul>
  
  <p>How serious do you want your engine be? You can always write a function that takes a position and checks your king. Everytime when you wants to check a pseudo-legal move, you call the function. It will definitely work, and it's simple but it's slow.</p>
  
  <p>Let's study at what a modern engine does. The source code for Stockfish is: <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp" rel="nofollow noreferrer">https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp</a></p>
  
  <pre><code>/// Position::legal() tests whether a pseudo-legal move is legal
  
  bool Position::legal(Move m) const {
  </code></pre>
  
  <p>Check en passant:</p>
  
  <pre><code>// En passant captures are a tricky special case. Because they are rather
  // uncommon, we do it simply by testing whether the king is attacked after
  // the move is made.
  </code></pre>
  
  <p>Do you want move your king?</p>
  
  <pre><code>// If the moving piece is a king, check whether the destination
  // square is attacked by the opponent. Castling moves are checked
  // for legality during move generation.
  </code></pre>
  
  <p>You can make the move if it's not pinned. Or it's pinned, but it still protects the king.</p>
  
  <pre><code>// A non-king move is legal if and only if it is not pinned or it
  // is moving along the ray towards or away from the king.
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-03-11T10:07:18.827Z
Answers.LastActivityDate: 2019-09-21T18:06:21.703Z
Answers.LastEditDate: 2019-09-21T18:06:21.703Z
Answers.UserDisplayName: SmallChess
Body: <p>so I've written an entire pseudo legal move generator as well as a simple gui to interface with. I also have my AI only play the best move available without going into a minimax tree. Before I begin any further AI coding I need to factor in putting a king into check and how it changes the list of legal moves. My question is how should i go about this. Given a board state and a list of pseudo legal moves, am I forced to then generate all responses by my opponent to ensure none of these moves will checkmate me? If I go from generating legal moves for 10 of my pieces would I then have to generate all responses to make sure none of those moves result in a checkmate? How also would I go about taking this response-parse requirement for a minimax algorithm? Would I store multiple boards in memory one for each node that I've traversed? Let me know what kind of design principles you think I should look out for.  </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-10T22:12:36.177Z
Comments.Text: You should only be asking one question per post. You're asking a lot here, and it's difficult to read.
Comments.UserDisplayName: Herb
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-11T11:07:01.970Z
Comments.Text: The question is not very well written. If the OP requires more information from me, please ask.
Comments.UserDisplayName: SmallChess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-10T03:26:29.453Z
Comments.Text: Well, here was another hopeful first time visitor, with a reasonable but not perfect question, who in our pickiness and power we chose to snub. Did he ever come back? Of course not. Yes, yes there's an enthusiastic raft of questions here, but we can be patient, and focus on the apparent key point: filtering illegal moves from a pseudo-legal move generator. The poster was labouring under a misunderstanding that this has something to do with checkmate. Eventually, through communication, he might have been informed and **then** been able to amend his post. But no, some people wanted to punish him
Comments.UserDisplayName: Laska
Tags: <engines><programming>
Title: Efficient ways to go from pseduo legal to fully legal move generation
ThreadText: # Efficient ways to go from pseduo legal to fully legal move generation
  
  (Asked by: username_0 on 2017-03-10)
  
  so I've written an entire pseudo legal move generator as well as a simple gui to interface with. I also have my AI only play the best move available without going into a minimax tree. Before I begin any further AI coding I need to factor in putting a king into check and how it changes the list of legal moves. My question is how should i go about this. Given a board state and a list of pseudo legal moves, am I forced to then generate all responses by my opponent to ensure none of these moves will checkmate me? If I go from generating legal moves for 10 of my pieces would I then have to generate all responses to make sure none of those moves result in a checkmate? How also would I go about taking this response-parse requirement for a minimax algorithm? Would I store multiple boards in memory one for each node that I've traversed? Let me know what kind of design principles you think I should look out for.
  
  **Tags:** engines, programming
  
  > **Comments:**
  > **username_1**: You should only be asking one question per post. You're asking a lot here, and it's difficult to read.  
  > **username_2**: The question is not very well written. If the OP requires more information from me, please ask.  
  > **username_3**: Well, here was another hopeful first time visitor, with a reasonable but not perfect question, who in our pickiness and power we chose to snub. Did he ever come back? Of course not. Yes, yes there's an enthusiastic raft of questions here, but we can be patient, and focus on the apparent key point: filtering illegal moves from a pseudo-legal move generator. The poster was labouring under a misunderstanding that this has something to do with checkmate. Eventually, through communication, he might have been informed and **then** been able to amend his post. But no, some people wanted to punish him  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-03-11. Score: 2
  
  This is my understanding:
  
  You have a pseudo-legal move generator
  Now, you want to get rid of the illegal moves
  
  How serious do you want your engine be? You can always write a function that takes a position and checks your king. Everytime when you wants to check a pseudo-legal move, you call the function. It will definitely work, and it's simple but it's slow.
  Let's study at what a modern engine does. The source code for Stockfish is: https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp
  /// Position::legal() tests whether a pseudo-legal move is legal
  
  bool Position::legal(Move m) const {
  
  Check en passant:
  // En passant captures are a tricky special case. Because they are rather
  // uncommon, we do it simply by testing whether the king is attacked after
  // the move is made.
  
  Do you want move your king?
  // If the moving piece is a king, check whether the destination
  // square is attacked by the opponent. Castling moves are checked
  // for legality during move generation.
  
  You can make the move if it's not pinned. Or it's pinned, but it still protects the king.
  // A non-king move is legal if and only if it is not pinned or it
  // is moving along the ray towards or away from the king.
  
  
  ---
  
  