Answers.Body: <p>Yes and no.</p>
  <p>In many board positions it is possible, but you will need to check a number of additional moves to verify if a King move is legal. You could essentially treat each square that the king could move to like a Queen and a Knight and then see if any enemy pieces can be found on those squares. Then you could check what kind of piece it is to determine if that move would put the King in check. That would prevent you from needing to generate legal moves for the opponent, however that might be very tricky for things like en passant.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-02T00:58:47.600Z
Answers.Text: or you can leave king check legality to the search function, which must compute the opponent's attacks any way (even if they are not actually possible because that would put his king in check)
Answers.UserDisplayName: qwr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-01T21:01:44.020Z
Answers.LastActivityDate: 2022-08-01T21:41:13.513Z
Answers.LastEditDate: 2022-08-01T21:41:13.513Z
Answers.UserDisplayName: brekker
Body: <p>Let's say I have a position where white is to move, and I need white's legal moves. Is there any way where it is not necessary to generate black's legal moves? You only need black's moves for white's king moves, and it would greatly improve efficiency if you could somehow use black's moves from last turn instead of generating all of black's legal moves?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-23T09:07:10.967Z
Comments.Text: One option is to do pseudolegal move generation (king might step into danger), then only check for legality during the evaluation phase. Due to tree pruning from alphabeta or other heuristics, you will check far fewer nodes than are actually generated.
Comments.UserDisplayName: Nihar Karve
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-23T09:11:03.697Z
Comments.Text: For your actual question, https://www.chessprogramming.org/Attack_and_Defend_Maps might be helpful. My personal opinion is that it's not really worth it - on-the-fly generation is pretty cheap with lookup tables and magic bitboards, and movegen isn't the most time-critical component of the engine anyway.
Comments.UserDisplayName: Nihar Karve
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-02T00:59:34.467Z
Comments.Text: related https://chess.stackexchange.com/questions/39937/why-isnt-legal-move-filtering-done-at-search-time
Comments.UserDisplayName: qwr
Tags: <engines><software><computer-chess>
Title: Is it possible to only generate one color's legal moves?
ThreadText: # Is it possible to only generate one color's legal moves?
  
  (Asked by: username_0 on 2022-07-22)
  
  Let's say I have a position where white is to move, and I need white's legal moves. Is there any way where it is not necessary to generate black's legal moves? You only need black's moves for white's king moves, and it would greatly improve efficiency if you could somehow use black's moves from last turn instead of generating all of black's legal moves?
  
  **Tags:** engines, software, computer-chess
  
  > **Comments:**
  > **username_1**: One option is to do pseudolegal move generation (king might step into danger), then only check for legality during the evaluation phase. Due to tree pruning from alphabeta or other heuristics, you will check far fewer nodes than are actually generated.  
  > **username_1**: For your actual question, https://www.chessprogramming.org/Attack_and_Defend_Maps might be helpful. My personal opinion is that it's not really worth it - on-the-fly generation is pretty cheap with lookup tables and magic bitboards, and movegen isn't the most time-critical component of the engine anyway.  
  > **username_2**: related https://chess.stackexchange.com/questions/39937/why-isnt-legal-move-filtering-done-at-search-time  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2022-08-01. Score: 2 | Accepted answer
  
  Yes and no.
  
  In many board positions it is possible, but you will need to check a number of additional moves to verify if a King move is legal. You could essentially treat each square that the king could move to like a Queen and a Knight and then see if any enemy pieces can be found on those squares. Then you could check what kind of piece it is to determine if that move would put the King in check. That would prevent you from needing to generate legal moves for the opponent, however that might be very tricky for things like en passant.
  
  > **Comments:**
  > **username_2**: or you can leave king check legality to the search function, which must compute the opponent's attacks any way (even if they are not actually possible because that would put his king in check)  
  
  ---
  
  