Efficient ways to go from pseduo legal to fully legal move generation 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.
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.