Skip to content

Instantly share code, notes, and snippets.

@mlakolar
Created December 31, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlakolar/b55fa91dbfaf0285ca937f7366b9b5a5 to your computer and use it in GitHub Desktop.
Save mlakolar/b55fa91dbfaf0285ca937f7366b9b5a5 to your computer and use it in GitHub Desktop.
import os
import argparse
import sys
import chess
import chess.pgn
from timeit import default_timer as timer
def parse_args():
"""
Define an argument parser and return the parsed arguments
"""
parser = argparse.ArgumentParser(
prog='analyze',
description='takes a list of PGN files and analyzes last position'
'of each PGN')
parser.add_argument("--file", "-f",
help="PGN file with repertoir",
required=True,
metavar="list_FILE")
parser.add_argument("--side", "-s",
help="white/black (1/0)",
type=int,
required=True,
default="1")
return parser.parse_args()
def printPath(node):
moves = []
while 1:
if node.move is None:
break
moves.append(node.move)
node = node.parent
moves.reverse()
board = chess.Board()
print( board.variation_san(moves) )
def traverseTree(node, side):
if node.board().turn == side:
if not node.variations:
printPath(node)
return
if len(node.variations) > 1:
printPath(node)
for next_node in node.variations:
traverseTree(next_node, side)
def main():
args = parse_args()
game = chess.pgn.read_game(open(args.file))
traverseTree(game, args.side)
sys.exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment