# Computer Chess Club Archives

## Messages

### Subject: Re: Detecting three-fold repetition?

Author: John Stanback

Date: 12:33:28 07/17/00

Go up one level in this thread

```On July 17, 2000 at 13:40:28, Tom Kerrigan wrote:

>I've had a number of requests to implement 3-fold repetition detection in TSCP.
>It's also clear that TSCP would do better in tournaments (although that isn't
>the goal...) if it could detect these draws.
>
>So the question is, is there an easy way to do the detection?
>
>In my "strong" program, I just compare hash keys. But TSCP doesn't keep hash
>keys and I have no intention for it to do so. So is there another way to do it?
>
>
>-Tom

Tom,

Here is a routine that I used in gnuchess a long time ago.  The
variable "cnt" contains the number of repetitions of the current
position (P).  GameCnt is the ply number of current position, including
game history and current search path.  Game50 is the last irrersable
move.  GameList[] contains all the game+search moves.
Initially the array b[] is zero'd.  Then the GameList[] array is
followed backward until the last non-reversible move
and b[f] is incremented and b[t] is decremented.  Variable c contains
a count of the number squares which are different from board position P.
If c == 0 it means that the board position in the loop matches
position P and *cnt is incremented.  I think there are some rare cases
where this doesn't work quite right, but it worked fine in practice.
It doesn't slow down the search because typically there has been
an irreversible move recently so that GameCnt <= Game50+3.

John

repetition(int *cnt)
{
int i,c,f,t;
unsigned char b[64];
unsigned int m;

*cnt = c = 0;
if (GameCnt > Game50+3)
{
for (i = 0; i < 64; b[i++] = 0);
for (i = GameCnt; i > Game50; i--)
{
m = GameList[i].gmove;
f = m>>8;
t = m&0xFF;
if (++b[f] == 0) c--; else c++;
if (--b[t] == 0) c--; else c++;
if (c == 0) (*cnt)++;
}
}
}

```