Programming three-fold repetition for my chess engine I'm making a chess engine that has to implement a three-fold repetition as my AI vs AI engine repeats the same moves over and over again (Have a look at my post here - My chess AI makes the same repeated moves). I've read the rules of this scenario from the wiki and would like to implement it. I'm not sure if I have understood the rules thoroughly, so what I'm doing here is - 


Checking if the present move made (from the movelog I have) has already occurred before.
If it did, check for one more occurrence (as we need three in total).
If the moves pattern for all the three re-occurred moves is same, return true.


Please have a look at my Java code and suggest me the corrections.

    boolean isThreeFoldRepetition() {
        List&lt;Move&gt; allMoves = Table.get().getMoveLog().getMoves();
        int currentMoveIndex = allMoves.size()-1;
        int secondFoldMoveIndex = currentMoveIndex - 4;
        int firstFoldMoveIndex = secondFoldMoveIndex - 4;
        int distance = currentMoveIndex - secondFoldMoveIndex - 1;

        while(firstFoldMoveIndex &gt;= 0){
            Move thirdFoldMoveMade = allMoves.get(currentMoveIndex);
            Move secondFoldMoveMade = allMoves.get(secondFoldMoveIndex);
            Move firstFoldMoveMade = allMoves.get(firstFoldMoveIndex);

            if(thirdFoldMoveMade.equals(secondFoldMoveMade) &amp;&amp;
               secondFoldMoveMade.equals(firstFoldMoveMade) &amp;&amp;
               firstFoldMoveIndex - distance &gt;= 0){
                int gap;
                for(gap = 1; gap &lt;= distance; gap++){
                    thirdFoldMoveMade = allMoves.get(currentMoveIndex - gap);
                    secondFoldMoveMade = allMoves.get(secondFoldMoveIndex - gap);
                    firstFoldMoveMade = allMoves.get(firstFoldMoveIndex - gap);

                    if(!thirdFoldMoveMade.equals(secondFoldMoveMade) || !secondFoldMoveMade.equals(firstFoldMoveMade))
                        break;
                }
                if(gap == distance)
                    return true;
            }

            secondFoldMoveIndex -= 2;
            firstFoldMoveIndex = secondFoldMoveIndex - distance - 3;
        }
        return false;
    }
Threefold-repetition is about a position (not moves) occurring three times. Those positions do not have to be reached by the same moves. 

Also (as this also seems to be confused sometimes) it is completely irrelevant when this happens, e.g. you can have the same positions after moves 10, 42 and 63 and it would still be a draw. 

You should check whether the position is repeated. So basically at every move you need to check whether the position occurred in the game before. By position I mean the same position of all pieces on the board and the same castling rights and the same player to move and the same enpassant rights. That is, basically you are checking whether the FEN are the same (except for halfmove clock and fullmove number).

Something like:

int positionCount=0;
while(iterate through game positions) {
    if ( positionToCheck.piecesPosition == iterationPosition.piecesPosition &amp;&amp; 
         positionToCheck.castleRights == iterationPosition.castleRights &amp;&amp; 
         positionToCheck.enPassantRights == iterationPosition.enPassantRights &amp;&amp;
         positionToCheck.playerToMove == iterationPosition.playerToMove ) {
      positionCount++;
    }
    if (positionCount==3) { return true; }
}
return false;


Of course you can simplify the above algorithm. E.g. you only need to iterate over position with the same player to move. Also you only need to iterate over positions back to the last pawn move, capture or castling move.