Last active
December 9, 2021 16:17
-
-
Save bemitc/78971aca6e26eb4d8deb9a90756d22d0 to your computer and use it in GitHub Desktop.
count unique positions in a pgn file
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
#!/usr/bin/env python3 | |
import sys | |
import chess.pgn | |
positions = [] | |
class UniqPositionsVisitor(chess.pgn.BaseVisitor): | |
def visit_board(self, board): | |
if board.fen() not in positions: | |
positions.append(board.fen()) | |
def result(self): | |
return True | |
for f in sys.argv[1:]: | |
pgn = open(f) | |
while True: | |
game = chess.pgn.read_game(pgn, Visitor=UniqPositionsVisitor) | |
if game == None: | |
break | |
pgn.close() | |
print(len(positions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment