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.

closed as too broad by Tony Ennis, Glorfindel, user1108, GloriaVictis, Herb Wolfe Mar 11 '17 at 15:08

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You should only be asking one question per post. You're asking a lot here, and it's difficult to read. – Herb Wolfe Mar 10 '17 at 22:12
  • The question is not very well written. If the OP requires more information from me, please ask. – SmallChess Mar 11 '17 at 11:07
  • 1
    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 – Laska Apr 10 '18 at 3:26

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.

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