Draw by repetition

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Carbec
Posts: 133
Joined: Thu Jan 20, 2022 9:42 am
Location: France
Full name: Philippe Chevalier

Draw by repetition

Post by Carbec »

Hi,

I try to verify my repetition detection.
This is what says the FIDE :
The game is a draw if a position occurs (at least) three times during the game. (Intervening moves do not matter.) It must be claimed by the player with the turn to move. The claim is made:
(a) If the position is about to appear for the third time, the player making the claim first writes their move on their scoresheet and notifies the arbiter that they intend to make this move.
or
(b) If the position has just appeared for the third time, the player with the move can claim the draw.
Positions are considered the same if
(1) the same player has the move,
(2) pieces of the same kind and color occupy the same squares, and
(3) the possible moves of all the pieces are the same.
The code in Vice (and mine also) is as follow :

Code: Select all

static int IsRepetition(const S_BOARD *pos) {

	for(int index = pos->hisPly - pos->fiftyMove; index < pos->hisPly-1; ++index) {
		if(pos->posKey == pos->history[index].posKey) {
			return TRUE;
		}
	}
	return FALSE;
}
I don't understand why it returns true if only 2 repetitions, and not 3. There surely be a good reason,
but I don't see it.

Another thing, why not increment index by 4, since we must have the same player to play.

Thanks

Philippe
User avatar
hgm
Posts: 27829
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Draw by repetition

Post by hgm »

In search it is better to assume a draw in the first repetition. Waiting for the second just wastes depth. For repetion of positions from the game it is a different matter. But I don't think it would make any difference what you do Elo-wise.

Indeed it would be better to lop in steps of 2 through the history.
Uri Blass
Posts: 10324
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Draw by repetition

Post by Uri Blass »

A second repetition is also not a draw unless one of the sides claim it(if you consider the fide rules and not engine-engine rules).

The following happened in a human OTB game between X and Y(I guess it happened more than one time in the history).

It was important for X to win in a game against Y but Y played a line that force a repetition(otherwise Y is going to get a clear advantage).
What X should do in order to maximize the chances to win?

Not repeating the position is bad and repeating the position is going to allow Y to claim a draw.
The solution:
X thought for a long time and finally played the repetition move without claiming a draw so Y hoped to win thanks to the time advantage on the clock and did not repeat the position and continued to play and finally X won in the relevant game.
User avatar
lithander
Posts: 881
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Draw by repetition

Post by lithander »

hgm wrote: Sat Feb 05, 2022 10:32 am Indeed it would be better to lop in steps of 2 through the history.
I just feed all the positions from the history to the TT with a score of 0 and protect them against being overwritten. Because I do a TT lookup anyway checking for repetitions is now free. No loops over the history during the search necessary.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
Henk
Posts: 7220
Joined: Mon May 27, 2013 10:31 am

Re: Draw by repetition

Post by Henk »

You can use TT instead of IID move for move ordering but TT move may lead to a position where chance of a repetiton draw is higher. For TT entries were stored with different game history. So in fact they are polluted. Best not to use TT at all but IID searches are nor for free.
User avatar
hgm
Posts: 27829
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Draw by repetition

Post by hgm »

lithander wrote: Sat Feb 05, 2022 2:25 pm
hgm wrote: Sat Feb 05, 2022 10:32 am Indeed it would be better to lop in steps of 2 through the history.
I just feed all the positions from the history to the TT with a score of 0 and protect them against being overwritten. Because I do a TT lookup anyway checking for repetitions is now free. No loops over the history during the search necessary.
That is what I do in micro-Max too. But only for positions in the game history. To make it fool-proof you would also have to store all positions currently being searched this way, until they are finally overwritten by the score from that search. This could be problematic in multithreaded searches.
Richard Allbert
Posts: 792
Joined: Wed Jul 19, 2006 9:58 am

Re: Draw by repetition

Post by Richard Allbert »

Carbec wrote: Sat Feb 05, 2022 9:14 am
I don't understand why it returns true if only 2 repetitions, and not 3.
Thanks

Philippe
Better would be to iterate in steps of 2, yes.

Regarding the return after 2, it's simple:

if a repetition of the position leads to a best move, then the engine goes for a draw, it'll repeat. There's no need to go beyond the first repetition, as the first repetition has conformed it's a best move anyway.
syzygy
Posts: 5569
Joined: Tue Feb 28, 2012 11:56 pm

Re: Draw by repetition

Post by syzygy »

lithander wrote: Sat Feb 05, 2022 2:25 pm
hgm wrote: Sat Feb 05, 2022 10:32 am Indeed it would be better to lop in steps of 2 through the history.
I just feed all the positions from the history to the TT with a score of 0 and protect them against being overwritten. Because I do a TT lookup anyway checking for repetitions is now free. No loops over the history during the search necessary.
But being able to repeat a history position neither guarantees a draw nor prevents you from still being able to win.

Repeating an already repeated history position does guarantee a draw, so what you could do is put those in the TT as draw.
jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: Draw by repetition

Post by jdart »

Crafty did draw evaluation after 2 reps in the search a long time ago. It is a time-saver.

However, you need to be careful not to do that at the root position: you need to follow the FIDE rules there.
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: Draw by repetition

Post by mvanthoor »

Carbec wrote: Sat Feb 05, 2022 9:14 am I don't understand why it returns true if only 2 repetitions, and not 3. There surely be a good reason,
but I don't see it.
A move was just executed on the board. If this move causes a three-fold repetition, the game is a draw. Therefore the position you have on the board NOW, after executing the move, is the THIRD repetition, and thus you look for two repetitions in the past. If you'd look for 3 repetitions in the past, the one on the board would be the fourth.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL