How to test 3rd repetition rule?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

This should be in the wiki

Post by sje »

This should be in the wiki, if it's not already there.

The easiest way to test game termination detection is to add a chunk of code to generate lots of games, each made from randomly selected moves, and keep track of the counts of the different game termination kinds.

Generate a million such games, and the counts should be fairly close to:

Code: Select all

[] rg 1000000
   Checkmate   152806 0.152806
  FiftyMoves   223350 0.22335
Insufficient   536842 0.536842
  Repetition    25374 0.025374
   Stalemate    61628 0.061628
Average ply length: 341.998
Maximum ply length: 922
Which shows that repetition draws should occur about 2.54% of the time.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

And some examples

Post by sje »

And some examples:

To assist with testing, I've produced a PGN file of a thousand random games, of which 24 are repetition draws.

In each game, note the Termination tag pair which indicates the game termination kind.

https://dl.dropboxusercontent.com/u/316 ... 000.pgn.gz
jswaff
Posts: 105
Joined: Mon Jun 09, 2014 12:22 am
Full name: James Swafford

Re: How to test 3rd repetition rule?

Post by jswaff »

In my Java engine chess4j, I pulled this logic out into a separate class and wrote a unit test to ensure that it works.

https://sourceforge.net/p/chess4j/code/ ... .java#l244

See 'testDrawRep()' at the bottom. It basically just starts with the opening position and plays through some moves, checking after each move whether it's a draw by rep or not.

I don't think I have anything that checks for a draw by rep within the context of a search but that satisfies me that the logic works, if I can manage to plug it in correctly.
User avatar
Luis Babboni
Posts: 464
Joined: Sat Feb 28, 2015 4:37 pm
Location: Argentina

Re: This should be in the wiki

Post by Luis Babboni »

sje wrote:This should be in the wiki, if it's not already there.

The easiest way to test game termination detection is to add a chunk of code to generate lots of games, each made from randomly selected moves, and keep track of the counts of the different game termination kinds.

Generate a million such games, and the counts should be fairly close to:

Code: Select all

[] rg 1000000
   Checkmate   152806 0.152806
  FiftyMoves   223350 0.22335
Insufficient   536842 0.536842
  Repetition    25374 0.025374
   Stalemate    61628 0.061628
Average ply length: 341.998
Maximum ply length: 922
Which shows that repetition draws should occur about 2.54% of the time.
Nice idea!!
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Ten million

Post by sje »

Code: Select all

[] rg 10000000
   Checkmate  1530488 0.153049
  FiftyMoves  2240233 0.224023
Insufficient  5358465 0.535847
  Repetition   258098 0.0258098
   Stalemate   612716 0.0612716
Average ply length: 342.005
Maximum ply length: 1017
User avatar
Werner Taelemans
Posts: 119
Joined: Mon Feb 03, 2014 11:57 am
Location: Belgium
Full name: Werner Taelemans

Re: How to test 3rd repetition rule?

Post by Werner Taelemans »

flok wrote:The int is a counter and when that reaches 3 at some point in the search-tree...
It is OK to use the draw score when the counter reaches 2, as explained in this thread:
http://www.talkchess.com/forum/viewtopic.php?t=51000