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?
-
2One 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.– Nihar KarveJul 23, 2022 at 9:07
-
2For 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.– Nihar KarveJul 23, 2022 at 9:11
-
related chess.stackexchange.com/questions/39937/…– qwrAug 2, 2022 at 0:59
1 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.
-
2or 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)– qwrAug 2, 2022 at 0:58