Answers.Body: <p>There are two approaches:</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-03T21:22:10.437Z
Answers.LastActivityDate: 2020-01-03T21:22:10.437Z
Answers.UserDisplayName: Inertial Ignorance
Answers.Body: <p>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. </p>
  
  <p>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)</p>
  
  <p>This will give you your first “general” legality check. </p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-04T00:05:13.900Z
Answers.LastActivityDate: 2020-01-04T00:05:13.900Z
Answers.UserDisplayName: Noah Caplinger
Body: <p>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.</p>
  <p>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.</p>
  <p>I am writing my generator in Python and would really appreciate a detailed explanation of an approach I can take.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-03T20:44:39.003Z
Comments.Text: [These kinds of features](https://python-chess.readthedocs.io/en/latest/core.html#chess.BaseBoard.pin) are covered in [Python-Chess](https://python-chess.readthedocs.io/en/latest/), 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.
Comments.UserDisplayName: Ellie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-03T21:16:01.843Z
Comments.Text: @Phonon I have looked at that code but it is very convoluted and I can't actually work out what it's doing
Comments.UserDisplayName: Jack P
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-04T03:34:52.433Z
Comments.Text: @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.
Comments.UserDisplayName: Noah Caplinger
Tags: <programming>
Title: How to calculate pinned chess moves
ThreadText: # How to calculate pinned chess moves
  
  (Asked by: username_0 on 2020-01-03)
  
  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.
  
  **Tags:** programming
  
  > **Comments:**
  > **username_1**: [These kinds of features](https://python-chess.readthedocs.io/en/latest/core.html#chess.BaseBoard.pin) are covered in [Python-Chess](https://python-chess.readthedocs.io/en/latest/), 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.  
  > **username_0**: @Phonon I have looked at that code but it is very convoluted and I can't actually work out what it's doing  
  > **username_2**: @username_0, 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.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2020-01-03. Score: 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.
  
  
  ---
  
  ### Answer by username_2 on 2020-01-04. Score: 3
  
  This answer expounds on username_3’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.
  
  
  ---
  
  