33import json
44import math
55import os
6+ import os .path
67from typing import Dict , Any , Union
8+ import urllib .request
79
810import chess
911import chess .engine
@@ -56,28 +58,31 @@ def opening(game_in_question):
5658 ply_count = 0
5759 root_node = game_in_question .parent
5860 node = game_in_question .end ()
59- with open ('eco_codes/eco.json' , 'r' ) as eco_file :
60- eco_data = json .load (eco_file )
61- while not node == game_in_question .parent :
62- prev_node = node .parent
61+ url = urllib .request .urlopen (
62+ "https://raw.githubusercontent.com/CoderAryanAnand/pythonChessAnalyzer/main/chessAnalyzer/eco_codes/eco"
63+ ".json" )
64+ content = url .read ()
65+ eco_data = json .loads (content )
66+ while not node == game_in_question .parent :
67+ prev_node = node .parent
6368
64- fen = eco_fen (node .board ())
65- classification = classify_fen (fen , eco_data )
69+ fen = eco_fen (node .board ())
70+ classification = classify_fen (fen , eco_data )
6671
67- if classification ["code" ] != "" :
68- # Add some comments classifying the opening
69- node .comment = "{} {}" .format (classification ["code" ],
70- classification ["desc" ])
71- # Remember this position so we don't analyze the moves
72- # preceding it later
73- root_node = node
74- # Break (don't classify previous positions)
75- break
72+ if classification ["code" ] != "" :
73+ # Add some comments classifying the opening
74+ node .comment = "{} {}" .format (classification ["code" ],
75+ classification ["desc" ])
76+ # Remember this position so we don't analyze the moves
77+ # preceding it later
78+ root_node = node
79+ # Break (don't classify previous positions)
80+ break
7681
77- ply_count += 1
78- node = prev_node
82+ ply_count += 1
83+ node = prev_node
7984
80- return node .parent , root_node , ply_count
85+ return node .parent , root_node , ply_count
8186
8287
8388def winning_chances (centipawns ):
@@ -119,7 +124,10 @@ def __init__(self, eval_time: float, cwd: str, eng_file: str = 'Engine/Stockfish
119124 self .engine = chess .engine .SimpleEngine .popen_uci (self .eng_file )
120125 self .white_mvs , self .black_mvs , self .black_lpos , self .white_lpos = 0 , 0 , list (), list ()
121126 self .current_working_directory = cwd
122- os .mkdir (self .current_working_directory + '/Game Report' )
127+ try :
128+ os .mkdir (self .current_working_directory + '/Game Report' )
129+ except FileExistsError :
130+ pass
123131
124132 def get_eval (self , fen : str ) -> float :
125133 """
@@ -156,27 +164,27 @@ def get_best_var(self, fen: str, uci=False) -> list or str:
156164 else :
157165 return board .variation_san (result ['pv' ])
158166
159- def create_graph (self , pgn_loc : str , loc : str ):
167+ def graph (self , pgn_loc : str , loc : str ):
160168 graph (pgn_loc , self .eng_file , location = loc )
161169
162170 def annotate_game (self , pgn_loc : str ) -> chess .pgn .Game :
163171 """
164- Goes through the game_in_question and analyses and annotates it
172+ Goes through the game and analyses and annotates it
165173 """
166- # Read game_in_question and create starting position
174+ # Read game and create starting position
167175 pgn = chess .pgn .read_game (open (pgn_loc ))
168176
169- # Find out what opening the game_in_question has
177+ # Find out what opening the game has
170178 opening (pgn )
171179
172- # Mark the end of the game_in_question
173- pgn .end ().comment = "End of game_in_question . " \
174- "The moves after this one are just telling you how the game_in_question could have gone on."
180+ # Mark the end of the game
181+ pgn .end ().comment = "End of game . " \
182+ "The moves after this one are just telling you how the game could have gone on."
175183 # Set variables for the loop
176184 # prev_eva = 0.00 # not needed anymore
177185 is_opening = True
178186
179- # Iterate through the game_in_question
187+ # Iterate through the game
180188 for node in pgn .mainline ():
181189 move = node .move
182190 board = node .board ()
@@ -229,7 +237,7 @@ def annotate_game(self, pgn_loc: str) -> chess.pgn.Game:
229237 print (f'\n \n \n { bColors .WARNING } { bColors .BOLD } Use <print(pgn, file=open(pgn_loc, "w"), end="\\ n\\ n")> '
230238 f'to add the '
231239 f'annotations to '
232- f'the game_in_question .\n \n { bColors .END_C } ' )
240+ f'the game .\n \n { bColors .END_C } ' )
233241 return pgn
234242
235243 @staticmethod
@@ -240,16 +248,16 @@ def train_coordinates():
240248
241249 def game_report (self , pgn_loc : str , annotate = False ) -> tuple :
242250
243- # Read game_in_question and create starting position
251+ # Read game and create starting position
244252 pgn = chess .pgn .read_game (open (pgn_loc ))
245253
246254 board = chess .Board ()
247255
248256 opening (pgn )
249257
250- # Mark the end of the game_in_question
251- pgn .end ().comment = "End of game_in_question . " \
252- "The moves after this one are just telling you how the game_in_question could have gone on."
258+ # Mark the end of the game
259+ pgn .end ().comment = "End of game . " \
260+ "The moves after this one are just telling you how the game could have gone on."
253261 # Set variables for the loop
254262 is_opening = True
255263 self .white_mvs : Dict [str , Union [int , Any ]] = {'Forced move' : 0 , 'Best move' : 0 , 'Excellent move' : 0 ,
@@ -259,7 +267,7 @@ def game_report(self, pgn_loc: str, annotate=False) -> tuple:
259267 'Good move' : 0 , 'Interesting move' : 0 , 'Book move' : 0 ,
260268 'Inaccuracy' : 0 , 'Mistake' : 0 , 'Blunder' : 0 }
261269
262- # Iterate through the game_in_question
270+ # Iterate through the game
263271 for node in pgn .mainline ():
264272 move = node .move
265273 lgl_mvs = len ([i for i in board .legal_moves ])
@@ -379,7 +387,7 @@ def game_report(self, pgn_loc: str, annotate=False) -> tuple:
379387 f .close ()
380388
381389 loc = 'Game Report/%s/Graph/Graph of {}.html' % (headers ["White" ] + " vs " + headers ["Black" ])
382- self .create_graph (pgn_loc , loc )
390+ self .graph (pgn_loc , loc )
383391 else :
384392 pass
385393
0 commit comments