Repetitions

Home * Search * Repetitions



Repetitions of positions may happen during game play and inside the search of a chess program due to reversible moves played from both sides, which might be nullified in one or multiple further reversible moves. The player to move may claim a draw if the same position occurs three times, or will occur after an intended move, in any order, with the same player to move.

Two positions are considered same or equal, if all occupied squares and kind of pieces (not necessarily the same piece) they occupy are the same, the castling rights for both sides did not change, and no en passant capture was possible during the first occurrence, even if obviously not played.

=Assigning Draw Score= Threefold repetition implies a position occurred thrice, that is repeated twice. When to score the position as a draw, however, is an entirely different matter. Most programs do this on the first repetition, no matter whether the first occurrence of the repeated position appears in the current search space, or not. Other programs consider that fact, they avoid cycles inside the current search tree to make it a directed acyclic graph (DAG), but allow a one-fold repetition, if the first occurrence appears in the game history before the current root. Anyway, to wait for the second repetition one has its pros and cons. The Repetition score is either zero or the contempt factor.

=Rules= The rules of chess state a threefold repetition of a position gives the player to move the option to claim a draw, no matter whether the threefold repetition already occurred yet, or is about to occur after declaring the intended move in conjunction with the draw claim. The latter case is a bit tricky for computers, and is/was likely not strictly implemented in most programs or their user interfaces, since a draw message text or box appeared after the repetition occurs but no claim before. Since July 2014 a fivefold repetition is sufficient without any claim.

Fide Rule
9.2 The game is drawn, upon a correct claim by the player having the move, when the same position, for at least the third time (not necessarily by a repetition of moves) a) is about to appear, if he first writes his move on his scoresheet and declares    to the arbiter his intention to make this move, or  b) has just appeared, and the player claiming the draw has the move. Positions as in (a) and (b) are considered the same, if the same player has the move, pieces of the same kind and color occupy the same squares, and the possible moves of all the pieces of both players are the same. Positions are not the same if a pawn that could have been captured en passant can no longer in this manner be captured or if the right to castle has been changed temporarily or permanently.

Since July 01, 2014
9.6 If one or both of the following occur(s) then the game is drawn: a) the same position has appeared, as in 9.2b, for at least five consecutive alternate moves by each player.   b) any consecutive series of 75 moves have been completed by each player without the movement of any pawn and without any capture. If the last move resulted in checkmate, that shall take precedence.

Former Rule
A former (German) rule, was believed to be sufficient to make the game of chess finite : A chess game ends with a draw if a sequence of moves - with all pieces in exactly the same positions - is played three times successively.

was proved not sufficient by Max Euwe in 1929 by applying the Prouhet–Thue–Morse Sequence, i. e. with following move indices :

Euwe's prove was the reason, two other rules in force each of which guarantees chess to be a finite game, threefold repetition of positions and the fifty-move rule. =Repetition of Positions= By position we mean the location of all the pieces on the Chessboard as well as castling rights and en passant status. Since the rule speaks about the piece position, and not their identity (i.e. artificially constructed repeated position where the rooks of one player changed places still would be a draw), repetition may be detected by using the Zobrist- or BCH signatures of the position.

Transposition Table
One possible implementation was used by Ken Thompson in Belle, who told Bruce Moreland that he detected repetitions by using the transposition hash table, as follows : The idea is to set an "open" flag in the position's transposition table element when the hash table is probed. This flag stays set until the position is no longer being searched, meaning when the search function returns a value for that position.

At any given time, the only nodes that are "open" are nodes that are in the game history, or are in the current line in the tree search, so if the hash table probe encounters an open node, it must be because the current position has occurred somewhere before

This has the advantage that it uses data structures that are already present in the typical chess program, but there are a few problems with this idea. The hash table element must be written when a node is entered, so an "always replace" scheme must be used. This isn't a problem for Thompson, since his scheme involves using an "always replace" table, but other implementations might not use this kind of replacement scheme. Another problem is that there can be hash table entry collisions, and they must be dealt with. I am not talking about hash key collisions which occur when two positions map to the same 64-bit key, I'm talking about when two particular positions want to share the same hash table element, which should be pretty common. If two open nodes that want to share the same hash element, it's not immediately obvious what to do, other than not detect repetitions on the second one. Perhaps this problem could be dealt with via a re-hashing scheme, but this seems like an annoying thing to add in order to support functionality that isn't central to what the transposition table should be doing. A final problem is that it is hard to figure out how to adapt this to a multiprocessor search where there might be several search threads accessing the same hash table. When an open node is encountered, it might not indicate a repetition at all, since it could belong to a line being searched by another processor. This problem sounds complicated to solve.

List of Keys
Most programs use an array or list of Zobrist- or BCH-keys and compare the current signature with keys 4, 6, 8 and so on plies along the actual variation. This is usually quite cheap, since testing often does not require looking through the entire list, since captures and pawn moves, that reset the halfmove clock for the purpose of enforcing fifty-move rule are known to make a repetition impossible. Since each thread or process may own its own copy of the game-record, this approach has also some merits in parallel search implementations.

John Stanback about his implementation in Zarkov : In Zarkov I simply keep 32 bits of hash for each move in the game history, whether it has actually occurred on the board or during the current search. I also have a variable that contains the ply at which the last irreversible move occurred. At each node in the search, including quiescence, if the current ply is at least 4 plies beyond the last irreversible move I test the current hash value against those for each position in the game history (for the current side to move only) back to the last irreversible move and count the number of matches (repetitions).

If the count is 2, then this is the third repetition and a draw score is returned. If the count is 1 and the current_ply > root_ply+2 then a draw score is also returned. This avoids problems that can occur if the program thinks that a move at the root leads to a draw (due to a single repetition) when the opponent may vary, but it also lets the program treat repeated positions in the search as draws which helps a lot. Since most positions in a search are less than 4 plies beyond the last irreversible move the repetition function is rarely called and the performance hit for detecting repetitions is negligible.

Dedicated Hash Table
Some programs, like Gerbil or Rookie use a separate small hash table, actually an implementation of a Bloom filter revealed by Ronald de Man as mentioned by Marcel van Kervinck in his thesis The design and implementation of the Rookie 2.0 Chess Playing Program : It is tempting to use the transposition table to help detecting repeated positions. However, overloaded transposition tables can lose entries. So we choose something else : a dedicated, small, repetition hash table. This tables has 2^14 one-byte entries that are initially zero (totaling 16KB). When entering a new position, the low 14 bits of the hash-key are used to index the table and bump up its value by one. The value is restored after unmaking the move. When entering a node and its value is found to be non-zero already, we know there could be a cycle, which we verify by tracing back the actual variation. The repetition table is large enough to sufficiently reduce the number of false hits and this vaporizes the costs of futile back-traces. =Repetition of Moves= An alternative, pragmatical approach based on repetitions of moves was proposed by Vladan Vučković and Đorđe Vidanović in 2004, as implemented in Axon and discussed in CCC.

The algorithm needs a move list containing the game record including the variation actually searched, 16-bit entries with origin and target square, and a flag whether a move is irreversible, as only information required. Starting with the last move made, the list is scanned backwards as long there are reversible moves. A local concatenating list of up to 24 entries (one entry for each piece able to make reversible moves) is used to determine cycles per piece, where consecutive moves on their target squares are merged to one "pseudo" move, using the earliest origin and the latest target square. If all distinct white and black moving pieces in the chain list contain equal from- and to coordinates, all cycles of moves are closed and a move repetition is detected. This is how the algorithm as given in the paper in 8086 assembly looks in pseudo C++, square coordinates are offset from a 12*12 mailbox approach, so that empty entries have no valid square index. bool repetition(SMove *pVariant) { SMove chainList[24], m;  short c = 0, i;

for (i=0; i < 24; ++i) { chainList[i].setEmpty; }

l: m = *pVariant--; /* fetch move and decrement move list pointer */ if ( m.isReversible ) { /* lookup chain list for from-coordinate that match current to coordinate */ for (i=0; i < 24; ++i) { if ( m.to == chainList[i].from ) { if ( m.from == chainList[i].to ) { if ( --c == 0 ) return true; /* repetition detected */ chainList[i].setEmpty; goto l;           } chainList[i].from = m.from; /* concatenate moves */ goto l;        } }     /* lookup for next empty slot, to add the current move in the chain list */ for (i=0; i < 24; ++i) { if ( chainList[i].isEmpty ) { chainList[i] = m;           ++c; goto l;        } }  }   return false; }

=See also=
 * Contempt Factor
 * Desperado
 * Draw
 * Fifty-move Rule
 * Graph History Interaction (GHI)
 * Path-Dependency
 * Perpetual Check
 * Pursuit
 * Repetitions in Belzebub
 * Repetitions in SCP
 * Transposition
 * Transposition Table
 * Vice Video on Repetition

=Publications=

1929

 * Max Euwe (1929). Mengentheoretische Betrachtungen über das Schachspiel. Proc. Konin. Akad. Wetenschappen (Amsterdam)

1980 ...

 * Murray Campbell (1985). The graph-history interaction: on ignoring position history. Computer Science Department, Carnegie Mellon University, Proceedings of the 1985 ACM annual conference, pdf
 * Harry Nelson, Robert Hyatt (1988). The Draw Heuristic of Cray Blitz. ICCA Journal, Vol. 11, No. 1 » Cray Blitz

1990 ...

 * Dennis Breuker, Jaap van den Herik, Jos Uiterwijk, Victor Allis (1999). A Solution to the GHI Problem for Best-First Search. CG 1998 » Graph History Interaction, Best-First

2000 ...

 * Thomas Lincke (2002). Exploring the Computational Limits of Large Exhaustive Search Problems. Ph.D thesis, ETH Zurich, pdf, Chapter 4.3 Cycles
 * Vladan Vučković, Đorđe Vidanović (2004). A New Approach to Draw Detection by Move Repetition in Computer Chess Programming. CoRR arXiv:cs/0406038
 * Akihiro Kishimoto, Martin Müller (2004). A General Solution to the Graph History Interaction Problem. AAAI National Conference, pdf
 * Akihiro Kishimoto (2005). Correct and Efficient Search Algorithms in the Presence of Repetitions. Ph.D. thesis, University of Alberta, Received the 2005 ChessBase Best-Publication Award, pdf » Proof-Number Search
 * Akihiro Kishimoto, Martin Müller (2005). A Solution to the GHI Problem for Depth-First Proof-Number Search. 7th Joint Conference on Information Sciences (JCIS2003), pp. 489 - 492, pdf » Graph History Interaction
 * Daniel Andersson (2009). Perfect-Information Games with Cycles. Ph.D. thesis, Aarhus University, advisor Peter Bro Miltersen, pdf

2010 ...

 * Marcel van Kervinck (2013). A fast software-based method for upcoming cycle detection in search trees. pdf preview

=Forum Posts=

1990 ...

 * Repetition detection w/hash tables by Bruce Moreland, rec.games.chess, August 14, 1993

1995 ...

 * Draw by Repetition Code by cmayer, rgcc, December 31, 1996
 * Repetitions in Crafty by Martin Borriss, rgcc, January 30, 1997 » Crafty
 * Perpetual check by Ian Kennedy, rgcc, May 19, 1997
 * Handling of repetition (draw) in transposition table by Bjarke Dahl Ebert, rgcc, June 9, 1997
 * triple repetition by Daniel Kang, rgcc, October 26, 1997
 * draw by repetition and hash tables by Werner Inmann, CCC, July 24, 1998
 * Problem with draws by rep and hash table by James Robertson, CCC, October 07, 1999

2000 ...

 * Detecting three-fold repetition? by Tom Kerrigan, CCC, July 17, 2000
 * Re: Detecting three-fold repetition? by John Stanback, CCC, July 17, 2000 » SCP Repetition detection

2001 2002 2003 2004
 * The Old way to detect 3fold repetition (help needed) by Gianluigi Masciulli, CCC, March 26, 2001
 * "Don't trust draw score" <=Is it true? by Teerapong Tovirat, CCC, August 08, 2001 » Transposition Table, Path-Dependency
 * Detecting Draws using a Small Hash Table? by Steve Maughan, CCC, February 20, 2002
 * 3 fold repetiton (Seacher v Postmodernist) by Frank Phillips, CCC, March 02, 2002
 * A new(?) technique to recognize draws by Heiner Marxen, CCC, June 01, 2002 » Perpetual Check, Corresponding Squares
 * Repetitions: is this code correct? by Vladimir Medvedev, CCC, July 12, 2002
 * Draw claims by Thomas Mayer, CCC, June 17, 2003
 * In computer games 3-fold repetition should be AUTOMATICALLY draw! by Jouni Uski, CCC, November 30, 2003
 * perpetual check recognition by Andreas Herrmann, CCC, January 30, 2004
 * A New Approach to Draw Detection by Move Repetition by Gian-Carlo Pascutto, CCC, July 29, 2004
 * Draw Detection by Move Repetition Procedure - Comments by Đorđe Vidanović, CCC, August 01, 2004
 * How to handle the first repetition correctly? (long) by Sven Schüle, Winboard Forum, August 10, 2004
 * Repetition Detection by Renze Steenhuisen, CCC, December 01, 2004
 * Perpetual check by Alessandro Scotti, Winboard Forum, December 03, 2004

2005 ...
2007 2008 2009
 * When to claim repetition and 50 move draws? by Peter Hughes, Winboard Forum, March 26, 2005
 * draw by repetition by Harm Geert Muller, Winboard Forum, November 21, 2005
 * Handling 3-rep/50-move in hash tables by Pradu Kannan, Winboard Forum, February 19, 2007
 * avoidrep-pruning by Gerd Isenberg, CCC, May 15, 2007 » Pruning
 * Problem with Transposition Table and Repitition-Draw by Oliver Brausch, CCC, January 11, 2008 » Transposition Table
 * Again, rep-draws (and score aging) by Harm Geert Muller, Winboard Forum, January 28, 2008
 * Repetition detection question by Robert Hyatt, CCC, January 28, 2008
 * Repetition detection structure by Mathieu Pagé, CCC, February 23, 2008
 * Semi-Path Dependent Hashing: a semi-useless idea by Zach Wegner, CCC, May 24, 2008
 * Repetition Detection by Colin, CCC, August 13, 2008
 * When and how to return a draw evaluation? by Mathieu Pagé, CCC, August 26, 2008 » Draw
 * Draw by repetition when mate is possible by Mathieu Pagé, CCC, December 30, 2008 » Checkmate
 * wrong draw claim or right? by Will Singleton, CCC, December 03, 2009
 * Stupid Extension Problem/Question by John Merlino, CCC, December 23, 2009 » Extensions

2010 ...
2011 2012 2013 2014
 * Repetition Detection Without Hashing by Levi Aho, CCC, February 13, 2010
 * nullmove and repetitive draw detection by Edward Yu, CCC, June 20, 2010 » Null Move Pruning
 * bizarre repetition detection issue by Robert Hyatt, CCC, November 04, 2010
 * Repetitions/50 moves and TT by Sergei Markoff, CCC, September 13, 2011
 * repetition detection by Edmund Moshammer, CCC, October 29, 2011
 * Draw by 3-fold repetition? by Andy Duplain, CCC, January 06, 2012
 * Aquarium IDEA, repetitions, and minimax over cycles by kevinfat, OpenChess Forum, September 17, 2012 » Aquarium
 * Why is this game not a draw? by Carlos Pagador, CCC, October 29, 2012
 * Re: Move Tables - explain as if I'm five by Karlo Bala Jr., CCC, November 05, 2012 » Repetitions in Belzebub
 * FIDE Rules by crystalclear, Winboard Forum, December 11, 2012
 * Half Move Clock Confusion by HumbleProgrammer, OpenChess Forum, January 10, 2013 » Fifty-move Rule, Halfmove Clock
 * Repetition check by Harm Geert Muller, CCC, March 29, 2013
 * Upcoming repetition detection by Marcel van Kervinck, OpenChess Forum, April 06, 2013
 * Stockfish bug by Steven Atkinson, CCC, May 30, 2013 » Stockfish
 * Repetition check by Harm Geert Muller, CCC, July 20, 2013
 * UCI protocol issue by Jon Dart, CCC, July 27, 2013 » UCI
 * ep and castle rights hashing by Natale Galioto, CCC, September 15, 2013 » Castling rights, En passant, Transposition Table
 * Two fold repetition rule by Michel Van den Bergh, CCC, January 22, 2014
 * FIDE's new rules for chess by Mark Lefler, CCC, July 20, 2014

2015 ...
2016
 * Using TT for detecting repetitions by thevinenator, OpenChess Forum, August 18, 2015
 * Simple triple repetition handler by Stefano Gemma, CCC, February 21, 2016
 * How to test 3rd repetition rule? by Luis Babboni, CCC, April 12, 2016
 * This should be in the wiki by Steven Edwards, CCC, April 13, 2016

2017 2018
 * 3rd repetition, a case where not cause castle rights... but by Luis Babboni, CCC, April 14, 2016 » Castling rights
 * Triple Repitition: Is this considered a repitition or not? by Jayakiran Akurathi, CCC, May 07, 2016 » En passant
 * Repetitions in the Search Tree by Jayakiran Akurathi, CCC, May 29, 2016
 * Syzygy and draw by repetition by Jon Dart, CCC, July 22, 2016 » Syzygy Bases
 * perpetual check position by Erin Dame, CCC, August 16, 2016 » Perpetual Check
 * Hashed repetition table by J. Wesley Cleveland, CCC, September 04, 2016 » Repetition Hash Table
 * transposition tables and three-fold repetition by Erin Dame, CCC, September 10, 2016 » Transposition Table
 * The new chess rules (5-fold repetition and 75-move draw) by Lyudmil Antonov, FishCooking, November 29, 2016 » Stockfish, Fifty-move Rule
 * Have engines updated for fide 2014 draw rules? by Norm Pollock, CCC, January 28, 2017
 * Reporting a draw in UCI by Vince Sempronio, CCC, May 05, 2017 » Draw, UCI
 * 3-fold repetition and cutechess-cli by Lars Mathiesen, CCC, January 14, 2018 » En passant

=External Links=
 * Cycle detection (graph theory) from Wikipedia
 * Cycle detection from Wikipedia
 * Repetition Detection from Bruce Moreland's Programming Topics
 * Threefold repetition from Wikipedia
 * Perpetual check from Wikipedia
 * Fide Handbook - E.I.01A. Laws of Chess
 * A Chess Firewall at Zero? by Ken Regan, Gödel's Lost Letter and P=NP, January 21, 2016
 * Agnes Obel - It's Happening Again (2016), BBC 6 Music Live Room, YouTube Video
 * featuring Kristina Koropecki, Charlotte Danhier, Catherine De Biasio

=References=

Up one Level