I'm trying to program my own chess engine (no AI).
I know there is Chess Game Starter Kit and I watched it for start inspiration.
But what I didn't catch is where are validated moves (here is moves validation) for my non-king pieces preventing to get myself to check?
Imagine situation:
A5 – opponents rook
A4 – my bishop
A3 – my king
I can't move my bishop now since I would get to check.
Or how would you suggest to check this situation?
Thank you
Best Answer
For a given board position, most chess engines start by generating pseudo-legal moves only. By pseudo-legal, I mean a move will be generated even if it:
The reason for this is performance. As many moves won't actually be searched due to beta pruning, you save time by avoiding the full check of move validity.
For every move that is searched, you need to check that it's really valid. This is normally done by passing the King's colour and square (and squares next to the King for a castling move) into an IsAttacked method. If that method returns true, you know the move isn't valid and therefore you shouldn't include it in your search.
This is the IsAttacked method from my own C# chess engine. Bear in mind that my engine is magic bitboard-based, so the code won't be directly applicable to the chess starter kit to which you linked. Unless you're familiar with magic bitboards, the translation won't be trivial.
Here's a fragment of code that generates pseudo-legal castling moves for White:
And here's the code that checks whether a pseudo-legal castling move is actually legal: