Answers.Body: <p>I think that your question is an excellent one. @Remillion, @itub and @SmallChess have answered it already. I believe that I mostly&mdash;perhaps wholly&mdash;agree with their answers, so now I would like to add an answer on a different level.</p>
  
  <p>The FIDE has not shown that it fully grasps the <em>kind</em> of question you are asking. You and I get it. To some extent, the FIDE does not.</p>
  
  <p>Therefore, for the time being, one probably cannot find authoritative answers to all such questions. This is a shame. If the FIDE asked your advice, they could fix the rules, but they don't quite see the need. Not yet.</p>
  
  <p>The FIDE's problem (though imperfectly grasped by the FIDE) is to express game mechanics in a way that</p>
  
  <ul>
  <li>is logically complete,</li>
  <li>does not require the reader to process the phraseology of rulebook language through complex chains of verbal inference,</li>
  <li>is unambiguous,</li>
  <li>is unlikely to be incorrectly understood, and</li>
  <li>is computationally tractable.</li>
  </ul>
  
  <p>Moreover, it would help if the FIDE would do as the American Contract Bridge League (ACBL) has done, cleanly separating game mechanics (which are abstract mathematical/logical constructs) from rules of player conduct (which are concrete and inherently imprecise).</p>
  
  <p>A good software engineer like you implicitly understands the problem. The FIDE, unfortunately, so far, is still groping toward an understanding.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-19T02:48:03.270Z
Answers.Text: In my own answer, I deliberately "missed the point" that you're making, on the grounds that such an answer would be needlessly complicated for the task at hand, i.e. writing a program conforming to the word of the Laws. As a (retro) problemist, I dabble a little in conceiving positions that poke holes at the ambiguities, and am aware of several previous and ongoing efforts to make the rules complete, consistent and extensible to fairy rules (i.e. few artificial explicit exceptions). It is surprisingly hard to do so - the issue is much deeper than it first seems.
Answers.UserDisplayName: Remellion
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-29T00:09:34.293Z
Answers.Text: I completely agree with your sentiment, and thanks for the reference to Bridge. So +1 from me. Magic the Gathering is another popular game where the rules are separated from the tournament guidelines. Having said that it is possible to use the existing laws to extract a correct meaning about draw by repetition
Answers.UserDisplayName: Laska
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-18T23:16:55.337Z
Answers.LastActivityDate: 2018-11-18T23:16:55.337Z
Answers.UserDisplayName: thb
Answers.Body: <p>While comparing FEN strings may be common practice, I don't think it is correct according a close reading of the rule. I would say that the rule is already completely specified by its first sentence (emphasis added):</p>
  
  <blockquote>
    <p>Positions are considered the same if and only if the same player has the move, pieces of the same kind and colour occupy the same squares and <strong>the possible moves of all the pieces of both players are the same</strong>.</p>
  </blockquote>
  
  <p>The rest is an addendum just to emphasize that the "possible moves of all the pieces" may be different for seemingly identical positions (i.e., all pieces in the same squares) due to castling or en passant. But I think it would be fundamentally wrong to count an "en passant state change" when no en passant move was actually possible, either because there was no enemy pawn next to the pawn that just moved or the enemy pawn is pinned. FEN records an "en passant target square" anyway for simplicity of implementation, but that doesn't mean that it is a <em>possible move</em>.</p>
  
  <p>As for castling, lack of castling rights due to check or obstruction doesn't matter for the purpose of this rule because if you couldn't castle due to check or obstruction in position 1, the same must be true for position 3 if all the pieces are in the same squares. If they were not, the position would already be obviously different without having to look at castling rights!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-18T23:00:57.970Z
Answers.Text: The FIDE would do well to seek the advice of OP or someone like him, because the FIDE is finding it difficult to express the rules in a logically complete, computationally tractable way. They're not thinking this through correctly. They have not quite understood the problem.
Answers.UserDisplayName: thb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-17T12:15:49.177Z
Answers.LastActivityDate: 2018-11-17T12:15:49.177Z
Answers.UserDisplayName: itub
Answers.Body: <p>This is an important corner case for programming and also comes up in certain chess problems. I begin explanations by saying that castling and en passant are handled slightly differently, but don’t worry about this. Each is handled in the most sensible way.</p>
  
  <p>Ideally one would look ahead in the game to find out whether the moves are actually playable. For en passant this is totally practicable because it just means looking one move ahead. Can the en passant actually be played or does a pin prevent it?</p>
  
  <p>Whether or not castling is actually playable may be complicated to determine by looking st the deeper future of the game. So the rule simply looks at whether the two rights have changed, i.e. whether king or rook have just moved.</p>
  
  <p>A genetics metaphor might be “check castling genotype and ep phenotype”.</p>
  
  <p>This is the intention behind the FIDE rules according to the head of the rules committee. There is some unwillingness to make arbitrary changes to the wording, but the current 2018 phrasing seems fairly clear. Certainly FIDE get the exact issue here.</p>
  
  <p>This applies both over the board and for chess problems</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-28T22:54:17.737Z
Answers.LastActivityDate: 2019-08-28T22:54:17.737Z
Answers.UserDisplayName: Laska
Answers.Body: <blockquote>
    <p>...So the first part of my question is, am I correct in my assessment of how the right to castle affects whether positions are identical?...</p>
    
    <p>...other restrictions like check, obstructions, or even whether the relevant rook was captured are not considered (though the last is probably irrelevant)...</p>
  </blockquote>
  
  <p>Yes. Imagine we play a game from the starting position, but you don't have castling rights and I do. Obviously, it'd be a different game. For instance, you won't be to castle in Ruy Lopez. Winning advantage for me even before the first move.</p>
  
  <blockquote>
    <p>First, say a pawn moves two squares, thus making it capturable by en passant, but there's no enemy pawn next to its destination. Does the fact that it would have been vulnerable to en-passant make the position non-identical to another otherwise identical position a few turns later?</p>
  </blockquote>
  
  <p>Pawns never move backward. A position that no enemy pawn being able to explore en passant is obviously different to one that does.</p>
  
  <blockquote>
    <p>Second, say a pawn moves two squares, landing next to an enemy pawn, giving that pawn the right to en passant, but where exercising the right to en passant would expose the king (thus barring the otherwise legal en passant)</p>
  </blockquote>
  
  <p>You don't have to check for king safety.</p>
  
  <p>I think you're over-complicating the rules. In programming, all you have to do is compare your FEN strings (ignore 50 moves counter and impossible en passant).</p>
  
  <pre><code>FEN compareFEN = ...;
  for (i from 0 to current move) {
      FEN fen = fens[i]
      if compareFENIgnore50MoveAndImpossibleEnpassant(compareFEN, fen)
          print("Repetition found!")
  } 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-18T22:56:20.597Z
Answers.Text: To OP, you write, "I think you're over-complicating the rules." I disagree. The FIDE could use the help of someone like OP to rewrite these draw rules in a clean, clear, logically unambiguous way. The draw rules are cleaner and clearer than they used to be, but they still need work. OP's problem, if I understand, is not that he is unable to impute a definite meaning to the rules as he reads them. Rather, his problem is that the rules fail to afford him confidence that his future users will impute the same meaning to the rules as *they* read them.
Answers.UserDisplayName: thb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-17T09:14:10.090Z
Answers.LastActivityDate: 2019-09-23T21:47:11.180Z
Answers.LastEditDate: 2019-09-23T21:47:11.180Z
Answers.UserDisplayName: SmallChess
Answers.Body: <p>Although it does not directly answer the question, here is a chess problem that demonstrates how the right to castle affects calculating threefold repetition. I sourced it from <a href="https://www.yacpdb.org/#67373" rel="nofollow noreferrer">Yet Another Chess Problem Databse.</a></p>
  <pre><code>[Title &quot;Nenad Petrović, Problem (Zagreb) 1959, 1st Prize, Mate In 8&quot;]
  [FEN &quot;r3k2r/p2p4/p1pP2p1/5pN1/5p2/1Q3p2/PP4b1/KB6 w kq - 0 1&quot;]
  
  1. Qb7 {In the initial position, Black has both castling rights.} Rd8 2. Qb3 Ra8 {The starting diagram is reached again, but now Black has lost queenside castling rights.} 3. Bd3 Rh1+ 4. Bb1 Rh8 {For the third and final time we have the same diagram, but now Black has lost both castling rights and has no hope of claiming a draw.} 5. Qc3 Rh1 6. Qg7 Rxb1+ 7. Kxb1 Rb8 8. Qg8#
  </code></pre>
  <p>The point is that when Black plays 4... Rh8 they may not claim a draw by threefold repetition. This is because, in each instance, the diagram is the same, but the rights, i.e. Black’s ability to castle, are not. In position #1, Black has both rights, in position #2, Black has only one right, and in position #3, Black has zero rights. Each position is different, and therefore, by the FIDE laws of chess, as cited in other answers, Black cannot claim a draw.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-19T11:00:26.127Z
Answers.LastActivityDate: 2021-05-02T15:14:38.760Z
Answers.LastEditDate: 2021-05-02T15:14:38.760Z
Answers.UserDisplayName: Rewan Demontay
Answers.Body: <p>You have identified the correct rule governing these situations. 9.2.2 is the relevant clause that applies; 9.2.2.1 and 9.2.2.2 are just clarifications of this rule.</p>
  
  <p>So to answer your queries:</p>
  
  <p>1) Your interpretation of castling rights is correct. Each player has two castling rights which are irrespective of the current board position. The right to castle short is lost only when the king or h-rook moves for the first time. The right to castle long is lost only when the king or a-rook moves for the first time. Other restrictions are irrelevant.</p>
  
  <p>2) If a pawn moves two squares without an enemy pawn ready to capture en passant next to it, the position is the same as the one which could reoccur after some more moves. This is because all the players have the same possible (legal) moves (the relevant en passant is, was, and will be unavailable) so under 9.2.2 it would be the same position.</p>
  
  <p>3) Similarly, if en passant is illegal due to a pin (exposing own king to check) or another reason, it does not count as a possible (legal) move, so under 9.2.2 the position and the repeated one later are considered the same.</p>
  
  <p>An additional note: comparing FEN is not a correct implementation according to the FIDE rules, since FEN always records down a pawn moving two squares, regardless of whether an en passant capture is possible or not. Hence a direct PGN comparison would yield a wrong conclusion in the two scenarios you described.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-18T23:02:01.520Z
Answers.Text: Your answer is not wrong but, unfortunately, I believe that it partly misses the point. The point is that FIDE rules are written in a way that allows opposite interpretations.
Answers.UserDisplayName: thb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-22T22:38:49.500Z
Answers.Text: "since FEN always records down a pawn moving two squares, regardless of whether an en passant capture is possible or not"
  
  I think this actually makes it easier, the FEN is irrelevant for the e-p but possible moves differ.
Answers.UserDisplayName: Ferazhu
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-17T15:04:44.360Z
Answers.LastActivityDate: 2018-11-17T15:04:44.360Z
Answers.UserDisplayName: Remellion
Body: <p>I'm writing a chess app, and have some questions regarding the intricacies of the threefold repetition rules.</p>
  
  <p>As of 2018, the <a href="https://www.fide.com/fide/handbook.html?id=208&amp;view=article" rel="nofollow noreferrer">FIDE handbook</a> has this to say about what constitutes identical positions for the purpose of threefold repetitions.</p>
  
  <blockquote>
    <p>9.2.2  Positions are considered the same if and only if the same player has the move, pieces of the same kind and colour occupy the
    same squares and the possible moves of all the pieces of both players
    are the same. Thus positions are not the same if:</p>
    
    <p>9.2.2.1   at the start of the sequence a pawn could have been captured en passant</p>
    
    <p>9.2.2.2   a king had castling rights with a rook that has not been moved, but forfeited these after moving. The castling rights are lost
    only after the king or rook is moved.</p>
  </blockquote>
  
  <p><a href="https://chess.stackexchange.com/questions/6188/draws-castling-rights-and-the-fides-2014-rulebook-revision">This question</a> and <code>Rule 9.2.2.2</code> address calculating right to castle. Looking at both, my conclusion is that it only checks whether the relevant piece(s) have moved; other restrictions like check, obstructions, or even whether the relevant rook was captured are not considered (though the last is probably irrelevant). </p>
  
  <p>The same rule also seems to specify that each player starts with 2 castling rights- one for kingside and one for queenside- rather than 3- one for the king and one for each rook.</p>
  
  <p>So the first part of my question is, am I correct in my assessment of how the right to castle affects whether positions are identical?</p>
  
  <p>For the second part of my question, there seems to be a little ambiguity regarding en-passant.<code>Rule 9.2.2.1</code> is clear about the case of if a pawn can legally en-passant one turn and can't during a later turn, then the positions aren't identical even if everything else is the same.</p>
  
  <p>But there are two cases with en passant that aren't clear, or at least, should be clarified in light of how right to castle is calculated.</p>
  
  <p>First, say a pawn moves two squares, thus making it capturable by en-passant, but there's no enemy pawn next to its destination. Does the fact that it would have been vulnerable to en passant make the position non-identical to another otherwise identical position a few turns later?</p>
  
  <p>Second, say a pawn moves two squares, landing next to an enemy pawn, giving that pawn the right to en-passant, but where exercising the right to en-passant would expose the king (thus barring the otherwise legal en passant). Would that position be considered different from an otherwise identical position a few turns later?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-18T22:50:14.207Z
Comments.Text: This is a good question. It might be a [duplicate,](https://chess.stackexchange.com/q/777/315) though.
Comments.UserDisplayName: thb
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-19T01:55:07.927Z
Comments.Text: I don't think it is. The question you referenced only mentions en passant. This is the only question, as far as I could tell, which mentions both special cases in one question.
Comments.UserDisplayName: SarcasticSully
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-19T22:26:40.210Z
Comments.Text: The answer is there "...the possible moves of all the pieces of both players are the same."  So if there is a change in the possible moves it does not count toward the repetition.
Comments.UserDisplayName: Ywapom
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-19T22:35:28.060Z
Comments.Text: @Ywapom that's what I initially thought. But considering how it works around castling, which accounts for more than currently possible move, it's worth 
  at least clarifying.
Comments.UserDisplayName: SarcasticSully
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-20T01:21:01.753Z
Comments.Text: So the answer is castling does effect the repetition but only if it is possible to castle in one of the positions being considered for 3-fold as that would change the possible moves? But it is possible to claim 3 fold in positions where castling in the future has not been ruled out in some and is allowed in others but in all of them it is not possible on that turn say due to a piece being in the way?
Comments.UserDisplayName: silent-tiger
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-11-20T19:22:45.873Z
Comments.Text: @silent-tiger in the case you mentioned, it would not be possible. If my king, f-bishop, and h-rook are in their starting squares, it's a different position if both the king and rook haven't moved, versus if at least one has, even though the possible moves in that position are identical. All 4 rights to castle in the future- kingside and queenside of each color- are a part of the position for the purposes of 3-fold-repetition, even if the position doesn't allow castling right now.
Comments.UserDisplayName: SarcasticSully
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T15:45:45.520Z
Comments.Text: "check, obstructions, or even whether the relevant rook was captured" depend on the board state. How can you have two board states that are the same, but there is a check/obstruction/rook capture in one, but not the other?
Comments.UserDisplayName: Acccumulation
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T20:36:50.590Z
Comments.Text: @Acccumulation If there's a check or obstruction in one, the board state can still be different. Consider: even thought the currently possible set of moves is the same, the possible future moves are different, so you'll play different moves to prepare to castle.
  
  A literal reading of the rules gives each player 2 rights to castle at the start of the game- one kingside, and one queenside. These are only destroyed when a relevant piece is moved. This addresses the check/obstruction case, but unfortunately, it also makes the board state change with a rook capture, despite no substantive change.
Comments.UserDisplayName: SarcasticSully
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-29T20:42:36.663Z
Comments.Text: "the board state can still be different." Of the board state is different, the the right to castle is moot. The three-fold repetition rule only applies if the board is identical. "it also makes the board state change with a rook capture, despite no substantive change." How is a rook capture not a substantive change? Changes don't get much more substantive than that.
Comments.UserDisplayName: Acccumulation
Tags: <rules><castling><en-passant><threefold-repetition>
Title: Rules: How does right to castle and en passant get considered for the purpose of calculating a threefold repetition?
ThreadText: # Rules: How does right to castle and en passant get considered for the purpose of calculating a threefold repetition?
  
  (Asked by: username_0 on 2018-11-17)
  
  I'm writing a chess app, and have some questions regarding the intricacies of the threefold repetition rules.
  
  As of 2018, the [FIDE handbook](https://www.fide.com/fide/handbook.html?id=208&view=article) has this to say about what constitutes identical positions for the purpose of threefold repetitions.
  
  > 9.2.2 Positions are considered the same if and only if the same player has the move, pieces of the same kind and colour occupy the
  > same squares and the possible moves of all the pieces of both players
  > are the same. Thus positions are not the same if:
  >
  > 9.2.2.1 at the start of the sequence a pawn could have been captured en passant
  >
  > 9.2.2.2 a king had castling rights with a rook that has not been moved, but forfeited these after moving. The castling rights are lost
  > only after the king or rook is moved.
  
  [This question](https://chess.stackexchange.com/questions/6188/draws-castling-rights-and-the-fides-2014-rulebook-revision) and `Rule 9.2.2.2` address calculating right to castle. Looking at both, my conclusion is that it only checks whether the relevant piece(s) have moved; other restrictions like check, obstructions, or even whether the relevant rook was captured are not considered (though the last is probably irrelevant).
  
  The same rule also seems to specify that each player starts with 2 castling rights- one for kingside and one for queenside- rather than 3- one for the king and one for each rook.
  
  So the first part of my question is, am I correct in my assessment of how the right to castle affects whether positions are identical?
  
  For the second part of my question, there seems to be a little ambiguity regarding en-passant.`Rule 9.2.2.1` is clear about the case of if a pawn can legally en-passant one turn and can't during a later turn, then the positions aren't identical even if everything else is the same.
  
  But there are two cases with en passant that aren't clear, or at least, should be clarified in light of how right to castle is calculated.
  
  First, say a pawn moves two squares, thus making it capturable by en-passant, but there's no enemy pawn next to its destination. Does the fact that it would have been vulnerable to en passant make the position non-identical to another otherwise identical position a few turns later?
  
  Second, say a pawn moves two squares, landing next to an enemy pawn, giving that pawn the right to en-passant, but where exercising the right to en-passant would expose the king (thus barring the otherwise legal en passant). Would that position be considered different from an otherwise identical position a few turns later?
  
  **Tags:** rules, castling, en-passant, threefold-repetition
  
  > **Comments:**
  > **username_1**: This is a good question. It might be a [duplicate,](https://chess.stackexchange.com/q/777/315) though.  
  > **username_0**: I don't think it is. The question you referenced only mentions en passant. This is the only question, as far as I could tell, which mentions both special cases in one question.  
  > **username_2**: The answer is there "...the possible moves of all the pieces of both players are the same."  So if there is a change in the possible moves it does not count toward the repetition.  
  > **username_0**: @username_2 that's what I initially thought. But considering how it works around castling, which accounts for more than currently possible move, it's worth  at least clarifying.  
  > **username_3**: So the answer is castling does effect the repetition but only if it is possible to castle in one of the positions being considered for 3-fold as that would change the possible moves? But it is possible to claim 3 fold in positions where castling in the future has not been ruled out in some and is allowed in others but in all of them it is not possible on that turn say due to a piece being in the way?  
  > **username_0**: @username_3 in the case you mentioned, it would not be possible. If my king, f-bishop, and h-rook are in their starting squares, it's a different position if both the king and rook haven't moved, versus if at least one has, even though the possible moves in that position are identical. All 4 rights to castle in the future- kingside and queenside of each color- are a part of the position for the purposes of 3-fold-repetition, even if the position doesn't allow castling right now.  
  > **username_4**: "check, obstructions, or even whether the relevant rook was captured" depend on the board state. How can you have two board states that are the same, but there is a check/obstruction/rook capture in one, but not the other?  
  > **username_0**: @username_4 If there's a check or obstruction in one, the board state can still be different. Consider: even thought the currently possible set of moves is the same, the possible future moves are different, so you'll play different moves to prepare to castle.  A literal reading of the rules gives each player 2 rights to castle at the start of the game- one kingside, and one queenside. These are only destroyed when a relevant piece is moved. This addresses the check/obstruction case, but unfortunately, it also makes the board state change with a rook capture, despite no substantive change.  
  > **username_4**: "the board state can still be different." Of the board state is different, the the right to castle is moot. The three-fold repetition rule only applies if the board is identical. "it also makes the board state change with a rook capture, despite no substantive change." How is a rook capture not a substantive change? Changes don't get much more substantive than that.  
  
  ---
  
  ## 6 answers
  
  ### Answer by username_5 on 2018-11-17. Score: 7 | Accepted answer
  
  You have identified the correct rule governing these situations. 9.2.2 is the relevant clause that applies; 9.2.2.1 and 9.2.2.2 are just clarifications of this rule.
  
  So to answer your queries:
  
  1) Your interpretation of castling rights is correct. Each player has two castling rights which are irrespective of the current board position. The right to castle short is lost only when the king or h-rook moves for the first time. The right to castle long is lost only when the king or a-rook moves for the first time. Other restrictions are irrelevant.
  
  2) If a pawn moves two squares without an enemy pawn ready to capture en passant next to it, the position is the same as the one which could reoccur after some more moves. This is because all the players have the same possible (legal) moves (the relevant en passant is, was, and will be unavailable) so under 9.2.2 it would be the same position.
  
  3) Similarly, if en passant is illegal due to a pin (exposing own king to check) or another reason, it does not count as a possible (legal) move, so under 9.2.2 the position and the repeated one later are considered the same.
  
  An additional note: comparing FEN is not a correct implementation according to the FIDE rules, since FEN always records down a pawn moving two squares, regardless of whether an en passant capture is possible or not. Hence a direct PGN comparison would yield a wrong conclusion in the two scenarios you described.
  
  > **Comments:**
  > **username_1**: Your answer is not wrong but, unfortunately, I believe that it partly misses the point. The point is that FIDE rules are written in a way that allows opposite interpretations.  
  > **username_10**: "since FEN always records down a pawn moving two squares, regardless of whether an en passant capture is possible or not"  I think this actually makes it easier, the FEN is irrelevant for the e-p but possible moves differ.  
  
  ---
  
  ### Answer by username_1 on 2018-11-18. Score: 6
  
  I think that your question is an excellent one. @Remillion, @username_7 and @username_8 have answered it already. I believe that I mostly—perhaps wholly—agree with their answers, so now I would like to add an answer on a different level.
  
  The FIDE has not shown that it fully grasps the *kind* of question you are asking. You and I get it. To some extent, the FIDE does not.
  
  Therefore, for the time being, one probably cannot find authoritative answers to all such questions. This is a shame. If the FIDE asked your advice, they could fix the rules, but they don't quite see the need. Not yet.
  
  The FIDE's problem (though imperfectly grasped by the FIDE) is to express game mechanics in a way that
  
  * is logically complete,
  * does not require the reader to process the phraseology of rulebook language through complex chains of verbal inference,
  * is unambiguous,
  * is unlikely to be incorrectly understood, and
  * is computationally tractable.
  
  Moreover, it would help if the FIDE would do as the American Contract Bridge League (ACBL) has done, cleanly separating game mechanics (which are abstract mathematical/logical constructs) from rules of player conduct (which are concrete and inherently imprecise).
  
  A good software engineer like you implicitly understands the problem. The FIDE, unfortunately, so far, is still groping toward an understanding.
  
  > **Comments:**
  > **username_5**: In my own answer, I deliberately "missed the point" that you're making, on the grounds that such an answer would be needlessly complicated for the task at hand, i.e. writing a program conforming to the word of the Laws. As a (retro) problemist, I dabble a little in conceiving positions that poke holes at the ambiguities, and am aware of several previous and ongoing efforts to make the rules complete, consistent and extensible to fairy rules (i.e. few artificial explicit exceptions). It is surprisingly hard to do so - the issue is much deeper than it first seems.  
  > **username_6**: I completely agree with your sentiment, and thanks for the reference to Bridge. So +1 from me. Magic the Gathering is another popular game where the rules are separated from the tournament guidelines. Having said that it is possible to use the existing laws to extract a correct meaning about draw by repetition  
  
  ---
  
  ### Answer by username_7 on 2018-11-17. Score: 4
  
  While comparing FEN strings may be common practice, I don't think it is correct according a close reading of the rule. I would say that the rule is already completely specified by its first sentence (emphasis added):
  
  > Positions are considered the same if and only if the same player has the move, pieces of the same kind and colour occupy the same squares and **the possible moves of all the pieces of both players are the same**.
  
  The rest is an addendum just to emphasize that the "possible moves of all the pieces" may be different for seemingly identical positions (i.e., all pieces in the same squares) due to castling or en passant. But I think it would be fundamentally wrong to count an "en passant state change" when no en passant move was actually possible, either because there was no enemy pawn next to the pawn that just moved or the enemy pawn is pinned. FEN records an "en passant target square" anyway for simplicity of implementation, but that doesn't mean that it is a *possible move*.
  
  As for castling, lack of castling rights due to check or obstruction doesn't matter for the purpose of this rule because if you couldn't castle due to check or obstruction in position 1, the same must be true for position 3 if all the pieces are in the same squares. If they were not, the position would already be obviously different without having to look at castling rights!
  
  > **Comments:**
  > **username_1**: The FIDE would do well to seek the advice of OP or someone like him, because the FIDE is finding it difficult to express the rules in a logically complete, computationally tractable way. They're not thinking this through correctly. They have not quite understood the problem.  
  
  ---
  
  ### Answer by username_6 on 2019-08-28. Score: 2
  
  This is an important corner case for programming and also comes up in certain chess problems. I begin explanations by saying that castling and en passant are handled slightly differently, but don’t worry about this. Each is handled in the most sensible way.
  
  Ideally one would look ahead in the game to find out whether the moves are actually playable. For en passant this is totally practicable because it just means looking one move ahead. Can the en passant actually be played or does a pin prevent it?
  
  Whether or not castling is actually playable may be complicated to determine by looking st the deeper future of the game. So the rule simply looks at whether the two rights have changed, i.e. whether king or rook have just moved.
  
  A genetics metaphor might be “check castling genotype and ep phenotype”.
  
  This is the intention behind the FIDE rules according to the head of the rules committee. There is some unwillingness to make arbitrary changes to the wording, but the current 2018 phrasing seems fairly clear. Certainly FIDE get the exact issue here.
  
  This applies both over the board and for chess problems
  
  
  ---
  
  ### Answer by username_8 on 2018-11-17. Score: 2
  
  ...So the first part of my question is, am I correct in my assessment of how the right to castle affects whether positions are identical?...
  ...other restrictions like check, obstructions, or even whether the relevant rook was captured are not considered (though the last is probably irrelevant)...
  
  Yes. Imagine we play a game from the starting position, but you don't have castling rights and I do. Obviously, it'd be a different game. For instance, you won't be to castle in Ruy Lopez. Winning advantage for me even before the first move.
  
  First, say a pawn moves two squares, thus making it capturable by en passant, but there's no enemy pawn next to its destination. Does the fact that it would have been vulnerable to en-passant make the position non-identical to another otherwise identical position a few turns later?
  
  Pawns never move backward. A position that no enemy pawn being able to explore en passant is obviously different to one that does.
  
  Second, say a pawn moves two squares, landing next to an enemy pawn, giving that pawn the right to en passant, but where exercising the right to en passant would expose the king (thus barring the otherwise legal en passant)
  
  You don't have to check for king safety.
  I think you're over-complicating the rules. In programming, all you have to do is compare your FEN strings (ignore 50 moves counter and impossible en passant).
  FEN compareFEN = ...;
  for (i from 0 to current move) {
      FEN fen = fens[i]
      if compareFENIgnore50MoveAndImpossibleEnpassant(compareFEN, fen)
          print("Repetition found!")
  }
  
  > **Comments:**
  > **username_1**: To OP, you write, "I think you're over-complicating the rules." I disagree. The FIDE could use the help of someone like OP to rewrite these draw rules in a clean, clear, logically unambiguous way. The draw rules are cleaner and clearer than they used to be, but they still need work. OP's problem, if I understand, is not that he is unable to impute a definite meaning to the rules as he reads them. Rather, his problem is that the rules fail to afford him confidence that his future users will impute the same meaning to the rules as *they* read them.  
  
  ---
  
  ### Answer by username_9 on 2019-09-19. Score: 1
  
  Although it does not directly answer the question, here is a chess problem that demonstrates how the right to castle affects calculating threefold repetition. I sourced it from Yet Another Chess Problem Databse.
  [Title "Nenad Petrović, Problem (Zagreb) 1959, 1st Prize, Mate In 8"]
  [FEN "r3k2r/p2p4/p1pP2p1/5pN1/5p2/1Q3p2/PP4b1/KB6 w kq - 0 1"]
  
  1. Qb7 {In the initial position, Black has both castling rights.} Rd8 2. Qb3 Ra8 {The starting diagram is reached again, but now Black has lost queenside castling rights.} 3. Bd3 Rh1+ 4. Bb1 Rh8 {For the third and final time we have the same diagram, but now Black has lost both castling rights and has no hope of claiming a draw.} 5. Qc3 Rh1 6. Qg7 Rxb1+ 7. Kxb1 Rb8 8. Qg8#
  
  The point is that when Black plays 4... Rh8 they may not claim a draw by threefold repetition. This is because, in each instance, the diagram is the same, but the rights, i.e. Black’s ability to castle, are not. In position #1, Black has both rights, in position #2, Black has only one right, and in position #3, Black has zero rights. Each position is different, and therefore, by the FIDE laws of chess, as cited in other answers, Black cannot claim a draw.
  
  
  ---
  
  