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?

  • 2
    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. Commented Jul 23, 2022 at 9:07
  • 2
    For your actual question, 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. Commented Jul 23, 2022 at 9:11
  • related chess.stackexchange.com/questions/39937/…
    – qwr
    Commented Aug 2, 2022 at 0:59

1 Answer 1


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.

  • 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)
    – qwr
    Commented Aug 2, 2022 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.