plirof2

chess- other - pgn????

Aug 25th, 2022
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import chess.pgn
  2. import chess
  3. import chess.svg
  4. import imageio
  5. import chess.engine
  6. import os
  7. from matplotlib import pyplot
  8. from cairosvg import svg2png
  9.  
  10. class analyse_multi_games:
  11.    
  12.     def __init__(self, file_name, player=""):
  13.         self.game_file = open(file_name, "r")
  14.         self.player = player
  15.         self.engine = chess.engine.SimpleEngine.popen_uci("/home/destroyer5237/Documentos/Projetos_Python/PythonChess/stockfish-10-linux/Linux/stockfish_10_x64")
  16.         self.board = chess.Board()
  17.         self.depth = 36
  18.         self.analyses_games = []
  19.         self.games_instances = []
  20.        
  21.         if "MovesPng" not in os.listdir() and "MovesPng" not in os.listdir():
  22.             os.mkdir("MovesPng")
  23.             os.mkdir("GraphicsGame")
  24.    
  25.     def start_games(self, number_of_games=True):
  26.        
  27.         pgn_game = False
  28.        
  29.         if number_of_games == True:
  30.             while not pgn_game is None:
  31.                 pgn_game = chess.pgn.read_game(self.game_file)
  32.                 self.games_instances.append(pgn_game)
  33.         else:
  34.             for num in range(number_of_games):
  35.                 pgn_game = chess.pgn.read_game(self.game_file)
  36.    
  37.                 if pgn_game is None:
  38.                     return True
  39.                 else:
  40.                     self.games_instances.append(pgn_game)
  41.    
  42.     def analyse_pgn(self, games_number, time_analyse=1):
  43.        
  44.         for game in range(games_number):
  45.                
  46.                 pgn_game = self.games_instances[game]
  47.                 player_with = True
  48.                 list_pontuation = []
  49.                 self.board = chess.Board()
  50.                
  51.                 if self.player != "":
  52.                     if pgn_game.headers["White"] == self.player:
  53.                         player_with = True
  54.                     else:
  55.                         player_with = False
  56.                
  57.                
  58.                 for move in pgn_game.mainline_moves():
  59.                     score = self.engine.analyse(board=self.board, limit=chess.engine.Limit(time=time_analyse, depth=self.depth))["score"]
  60.                     pontuation = chess.engine.PovScore(score, self.board.turn)
  61.                     if not pontuation.is_mate():
  62.                         if self.board.turn == player_with:
  63.                             list_pontuation.append(int(str(pontuation))/100)
  64.                         else:
  65.                             list_pontuation.append(-int(str(pontuation))/100)
  66.                     else:
  67.                         list_pontuation.append(str(pontuation))
  68.                        
  69.                     self.board.push(move)
  70.                
  71.                 self.analyses_games.append(list_pontuation)
  72.        
  73.     def get_gif_game(self, game_number=1, speed=2):                        
  74.        
  75.         game_gif = self.games_instances[game_number - 1]
  76.         board_gif = chess.Board()
  77.         count = 0
  78.         images_files = []
  79.        
  80.         for move in game_gif.mainline_moves():
  81.             generated_svg = chess.svg.board(board=board_gif)
  82.             svg2png(bytestring=generated_svg, parent_width=500, parent_height=500, write_to=f"MovesPng/move_{count}.png")
  83.             images_files.append(f"MovesPng/move_{count}.png")
  84.             count += 1
  85.             board_gif.push(move)
  86.        
  87.         images = []
  88.         for filename in images_files:
  89.             images.append(imageio.imread(filename))
  90.            
  91.         imageio.mimsave(f'game_{game_number}.gif', images, fps=speed)
  92.        
  93.         for i in range(count):
  94.             os.remove(f"MovesPng/move_{i}.png")
  95.    
  96.     def generate_game_graphics(self, game=1):
  97.        
  98.         if len(self.analyses_games) < game:
  99.             return "Jogo ainda não foi analisado!"  
  100.        
  101.         game_to_graphic = self.analyses_games[game-1]
  102.         pgn_game = self.games_instances[game-1]
  103.        
  104.         x_graphics = [x for x in range(1, len(game_to_graphic) + 1)]
  105.         title_name = pgn_game.headers["Event"]
  106.         pyplot.rcParams["figure.figsize"] = (6.4*4, 4.8)
  107.         pyplot.plot(x_graphics, game_to_graphic, color="black")
  108.         pyplot.plot(x_graphics, game_to_graphic, color="black", marker="o")
  109.         pyplot.title(f"Partida {title_name}")
  110.         pyplot.ylabel("Sua Vantagem")
  111.         pyplot.xlabel("Lance")
  112.         pyplot.savefig(f"GameGraphics_{game}.svg")
  113.        
  114.    
  115.    
  116. games_file = input("Digite o nome do arquivo com seus jogos:")
  117.  
  118. ana = analyse_multi_games(games_file, player="NDestroyer5237")
  119. ana.start_games(number_of_games=2)
  120. ana.get_gif_game(2)
  121. ana.analyse_pgn(games_number=1, time_analyse=0.1)
  122. ana.generate_game_graphics(game=1)
Add Comment
Please, Sign In to add comment