Skip to content

Commit 63ec55b

Browse files
committed
Update fivefold repetition rule (fixes niklasf#295)
1 parent 98712c3 commit 63ec55b

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

chess/__init__.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,32 +1731,28 @@ def is_seventyfive_moves(self):
17311731
def is_fivefold_repetition(self):
17321732
"""
17331733
Since the 1st of July 2014 a game is automatically drawn (without
1734-
a claim by one of the players) if a position occurs for the fifth time
1735-
on consecutive alternating moves.
1734+
a claim by one of the players) if a position occurs for the fifth time.
1735+
Originally this had to occur on consecutive alternating moves, but
1736+
this has since been revised.
17361737
"""
17371738
transposition_key = self._transposition_key()
1738-
1739-
if len(self.move_stack) < 16:
1740-
return False
1741-
1739+
repetitions = 1
17421740
switchyard = collections.deque()
17431741

1744-
for _ in range(4):
1745-
# Go back two full moves each.
1746-
for _ in range(4):
1747-
switchyard.append(self.pop())
1742+
while self.move_stack and repetitions < 5:
1743+
move = self.pop()
1744+
switchyard.append(move)
17481745

1749-
# Check if the position was the same as before.
1750-
if self._transposition_key() != transposition_key:
1751-
while switchyard:
1752-
self.push(switchyard.pop())
1746+
if self.is_irreversible(move):
1747+
break
17531748

1754-
return False
1749+
if self._transposition_key() == transposition_key:
1750+
repetitions += 1
17551751

17561752
while switchyard:
17571753
self.push(switchyard.pop())
17581754

1759-
return True
1755+
return repetitions >= 5
17601756

17611757
def can_claim_draw(self):
17621758
"""

test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,10 @@ def test_fivefold_repetition(self):
10891089
self.assertFalse(board.is_fivefold_repetition())
10901090
board.push_san("Qd8")
10911091

1092-
# This is a threefold repetition but not a fivefold repetition, because
1093-
# consecutive moves are required for that.
1092+
# This is a threefold repetition, and also a fivefold repetition since
1093+
# it no longer has to occur on consecutive moves.
10941094
self.assertTrue(board.can_claim_threefold_repetition())
1095-
self.assertFalse(board.is_fivefold_repetition())
1095+
self.assertTrue(board.is_fivefold_repetition())
10961096
self.assertEqual(board.fen().split()[0], fen.split()[0])
10971097

10981098
def test_fifty_moves(self):

0 commit comments

Comments
 (0)