-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c631896
commit a0bde4b
Showing
9 changed files
with
169 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import chess | ||
import collections | ||
|
||
|
||
# Unfortunately the gameover detection of python-chess is very slow | ||
# because it treats the game as drawn if one player can claim draw | ||
# AFTER their next move. This means the detection loops over every | ||
# possible move and that calls the move generator which is SLOW. | ||
# We dont need this claiming mechanic here, so we implement our own | ||
# detection. (Speedup of 2x) | ||
class GameOverDetection: | ||
def is_game_over(board) -> bool: | ||
if board.is_seventyfive_moves(): | ||
return True | ||
|
||
# Insufficient material. | ||
if board.is_insufficient_material(): | ||
return True | ||
|
||
# Stalemate or checkmate. | ||
if not any(board.generate_legal_moves()): | ||
return True | ||
|
||
# Fivefold repetition. | ||
if board.is_fivefold_repetition(): | ||
return True | ||
|
||
# Fifty move rule | ||
if board.halfmove_clock > 100: | ||
return True | ||
|
||
# Threefold repetition | ||
if GameOverDetection.is_threefold_repetition(board): | ||
return True | ||
|
||
return False | ||
|
||
def is_threefold_repetition(board) -> bool: | ||
transposition_key = board._transposition_key() | ||
transpositions = collections.Counter() | ||
transpositions.update((transposition_key,)) | ||
|
||
# Count positions. | ||
switchyard = [] | ||
while board.move_stack: | ||
move = board.pop() | ||
switchyard.append(move) | ||
|
||
if board.is_irreversible(move): | ||
break | ||
|
||
transpositions.update((board._transposition_key(),)) | ||
|
||
while switchyard: | ||
board.push(switchyard.pop()) | ||
|
||
# Threefold repetition occured. | ||
if transpositions[transposition_key] >= 3: | ||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
|
||
python -m cProfile -o tests/results/profiling main.py --bench | ||
gprof2dot -f pstats tests/results/profiling | dot -Tpng -o tests/results/profile.png | ||
dt=$(date '+%Y-%m-%d-%H-%M-%S') | ||
python -m cProfile -o "tests/results/profiling-$dt" main.py --bench | ||
gprof2dot -f pstats "tests/results/profiling-$dt" | dot -Tpng -o "tests/results/profile-$dt.png" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.