4

I am writing a legal move generator, and the last piece of the puzzle is preventing pinned pieces from being moved such that the king will be in check.

I have read a lot of posts here and on stackoverflow and the only answers given seem to be vague references to stockfish's code which I can't seem to decipher or an incomplete explanation.

I am writing my generator in Python and would really appreciate a detailed explanation of an approach I can take.

Thanks.

  • 3
    These kinds of features are covered in Python-Chess, and if you want to implement things on your own, the library is open-source, so you can also look into it as a source of inspiration. – Ellie Jan 3 at 20:44
  • @Phonon I have looked at that code but it is very convoluted and I can't actually work out what it's doing – Jack P Jan 3 at 21:16
  • @Jack P, the Python chess library uses bitboards for board representation. This is a somewhat advanced topic, so I wouldn't recommend wading too deep. The Python Chess library is nice for beginners that want someone else to do the dirty work of chess programming for them. This doesn't appear to be you. – Noah Caplinger Jan 4 at 3:34
4

There are two approaches:

1) For the piece in question you're considering to move, look at the vector going through it to the king. For example, if they king and the piece are separated horizontally/vertically, look at the line going through it to the king. Is there an enemy rook/queen on this line, such that your piece is currently between it and your king? Or, if the piece is on a diagonal from the king, see if there's an enemy bishop/queen on this diagonal. However, if your piece moves along this vector (and thus continues blocking your king), it's fine. For example, assume your king is on h8, your queen is on e5, and the opponent's bishop is on a1. There's an enemy piece on the a1-h8 vector (which your queen and king are on), so moving the queen off this vector/diagonal is illegal. But moving the queen along the vector (e.g., anywhere from b2 to g7) won't violate the pin.

2) The simpler approach is a general purpose illegal move checker. Make a list of all pseudo-legal moves, and then check each to see if it results in an illegal position where your king is en prise. This encompasses what you're trying to do with not moving a pinned piece out of the way, since it would result in an illegal position.

| improve this answer | |
3

This answer expounds on Inertial Ignorance’s #2. The idea is that you make pseudo-legal moves (moves according to piece movement, not accounting for legality) then check for legality.

The way we’ll do this is by making the move, then looking at the square of the king. Loop through all squares to the right of the king (starting from the closest square) and stop at the first piece. If it’s an opponent’s rook or queen, the move was illegal. Similarly do this for the other directions, including diagonals and knight moves. If it “passes” all the tests, it’s legal (assuming you’ve also checked the other weird rules regarding castling/en passant)

This will give you your first “general” legality check.

Assuming you’re using this to build an engine, this will be something you want to optimize a LOT (though Python really isn’t he language for this). To give you an idea: run through a bunch of games and find the directions which the king is checked from most often, then loop through those directions first.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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