3
3
import json
4
4
import math
5
5
import os
6
+ import os .path
6
7
from typing import Dict , Any , Union
8
+ import urllib .request
7
9
8
10
import chess
9
11
import chess .engine
@@ -56,28 +58,31 @@ def opening(game_in_question):
56
58
ply_count = 0
57
59
root_node = game_in_question .parent
58
60
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
63
68
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 )
66
71
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
76
81
77
- ply_count += 1
78
- node = prev_node
82
+ ply_count += 1
83
+ node = prev_node
79
84
80
- return node .parent , root_node , ply_count
85
+ return node .parent , root_node , ply_count
81
86
82
87
83
88
def winning_chances (centipawns ):
@@ -119,7 +124,10 @@ def __init__(self, eval_time: float, cwd: str, eng_file: str = 'Engine/Stockfish
119
124
self .engine = chess .engine .SimpleEngine .popen_uci (self .eng_file )
120
125
self .white_mvs , self .black_mvs , self .black_lpos , self .white_lpos = 0 , 0 , list (), list ()
121
126
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
123
131
124
132
def get_eval (self , fen : str ) -> float :
125
133
"""
@@ -156,27 +164,27 @@ def get_best_var(self, fen: str, uci=False) -> list or str:
156
164
else :
157
165
return board .variation_san (result ['pv' ])
158
166
159
- def create_graph (self , pgn_loc : str , loc : str ):
167
+ def graph (self , pgn_loc : str , loc : str ):
160
168
graph (pgn_loc , self .eng_file , location = loc )
161
169
162
170
def annotate_game (self , pgn_loc : str ) -> chess .pgn .Game :
163
171
"""
164
- Goes through the game_in_question and analyses and annotates it
172
+ Goes through the game and analyses and annotates it
165
173
"""
166
- # Read game_in_question and create starting position
174
+ # Read game and create starting position
167
175
pgn = chess .pgn .read_game (open (pgn_loc ))
168
176
169
- # Find out what opening the game_in_question has
177
+ # Find out what opening the game has
170
178
opening (pgn )
171
179
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."
175
183
# Set variables for the loop
176
184
# prev_eva = 0.00 # not needed anymore
177
185
is_opening = True
178
186
179
- # Iterate through the game_in_question
187
+ # Iterate through the game
180
188
for node in pgn .mainline ():
181
189
move = node .move
182
190
board = node .board ()
@@ -229,7 +237,7 @@ def annotate_game(self, pgn_loc: str) -> chess.pgn.Game:
229
237
print (f'\n \n \n { bColors .WARNING } { bColors .BOLD } Use <print(pgn, file=open(pgn_loc, "w"), end="\\ n\\ n")> '
230
238
f'to add the '
231
239
f'annotations to '
232
- f'the game_in_question .\n \n { bColors .END_C } ' )
240
+ f'the game .\n \n { bColors .END_C } ' )
233
241
return pgn
234
242
235
243
@staticmethod
@@ -240,16 +248,16 @@ def train_coordinates():
240
248
241
249
def game_report (self , pgn_loc : str , annotate = False ) -> tuple :
242
250
243
- # Read game_in_question and create starting position
251
+ # Read game and create starting position
244
252
pgn = chess .pgn .read_game (open (pgn_loc ))
245
253
246
254
board = chess .Board ()
247
255
248
256
opening (pgn )
249
257
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."
253
261
# Set variables for the loop
254
262
is_opening = True
255
263
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:
259
267
'Good move' : 0 , 'Interesting move' : 0 , 'Book move' : 0 ,
260
268
'Inaccuracy' : 0 , 'Mistake' : 0 , 'Blunder' : 0 }
261
269
262
- # Iterate through the game_in_question
270
+ # Iterate through the game
263
271
for node in pgn .mainline ():
264
272
move = node .move
265
273
lgl_mvs = len ([i for i in board .legal_moves ])
@@ -379,7 +387,7 @@ def game_report(self, pgn_loc: str, annotate=False) -> tuple:
379
387
f .close ()
380
388
381
389
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 )
383
391
else :
384
392
pass
385
393
0 commit comments