U
    9%e                     @   s  d Z ddlmZ ddlZdddddgZdJd	d
Ze Ze	de
  de  d e Zdd eeD Zeedkre	ded    dd ejddD Ze	dee d e	d e	d ejddD ]`\ZZZed dkre de dZe D ] \ZZede de d7 Zqed7 Ze	e qeeZdd e D Zee d ZejddD ]z\Z ZZ!e!d "d!Z#e#d d"kree   d#7  < n@e#d d$kree   d%7  < ee  d%7  < nee  d#7  < qrd&d eD Z$e%eZ&e&d'  d(7  < e&d)  d*7  < e&d+  d,7  < ej'd-d.\Z(Z)ej*ee&d/ed0d1 ej+ee&e$d2d3d4 d5d6d7d8Z,ej-ee&d9e,d: d;d5d<d9d=Z.e)/d>e. d?e.d@< e)j0dAdBdCdDe)j1e.dE e)j0dAdFdGdDe)j1e.dE e)2dBdH e(3  e4dI e5  dS )Ka
  
=============
Chess Masters
=============

An example of the MultiDiGraph class.

The function `chess_pgn_graph` reads a collection of chess matches stored in
the specified PGN file (PGN ="Portable Game Notation").  Here the (compressed)
default file::

    chess_masters_WCC.pgn.bz2

contains all 685 World Chess Championship matches from 1886--1985.
(data from http://chessproblem.my-free-games.com/chess/games/Download-PGN.php)

The `chess_pgn_graph()` function returns a `MultiDiGraph` with multiple edges.
Each node is the last name of a chess master. Each edge is directed from white
to black and contains selected game info.

The key statement in `chess_pgn_graph` below is::

    G.add_edge(white, black, game_info)

where `game_info` is a `dict` describing each game.
    NEventDateResultECOZSitechess_masters_WCC.pgn.bz2c              	   C   s   ddl }t }i }|| }dd |D }W 5 Q R X |D ]f}|drv|dd dd\}}|d	|t|< q<|r<|d
}	|d}
|j	|	|
f| i }q<|S )zRead chess games in pgn format in pgn_file.

    Filenames ending in .bz2 will be uncompressed.

    Return the MultiDiGraph of players connected by a chess game.
    Edges contain game data in a dict.

    r   Nc                 S   s   g | ]}|  d qS )z
)decoderstrip).0line r   /var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/drawing/plot_chess_masters.py
<listcomp>2   s     z#chess_pgn_graph.<locals>.<listcomp>[    "WhiteZBlack)
bz2nxZMultiDiGraphBZ2File
startswithsplitstripstrpopZadd_edge)Zpgn_filer   GZgameZdatafilelinesr
   tagvaluewhiteblackr   r   r   chess_pgn_graph$   s    	


r"   zLoaded z chess games between z	 players
c                 C   s   g | ]}t |qS r   )HZsubgraph)r	   cr   r   r   r   J   s     r   r   z/Note the disconnected component consisting of:
c                 C   s   h | ]\}}}|d  qS )r   r   )r	   r    r!   	game_infor   r   r   	<setcomp>O   s     r&   T)dataz
From a total of z different openings,z-the following games used the Sicilian openingz5with the Najdorff 7...Qb6 "Poisoned Pawn" variation.
ZB97z vs 
z   z: c                 C   s    g | ]\}}t t||qS r   )lenr   Zget_edge_data)r	   uvr   r   r   r   `   s     g        -1g      ?z1/2g      ?c                 C   s   g | ]}t | d  qS )2   )wins)r	   r+   r   r   r   r   m   s     zReshevsky, Samuel H)皙?gzBotvinnik, Mikhail M)gQ?gQzSmyslov, Vassily V)r0   gQ)   r1   )Zfigsizeg333333?m)alphawidthZ
edge_colorz#210070g?)Z	node_sizeZ
node_colorr3   kr    gffffff?)ecfcr3      )Z	font_sizeZbboxZ	Helveticabold)ZfontnamecolorZ
fontweightZfontsizez+World Chess Championship Games: 1886 - 1985rr:   g?g?zedge width = # games playedcenter)ZhorizontalalignmentZ	transformZfontdictgQ?znode size = # games wonr0   off)r   )6__doc__Zmatplotlib.pyplotZpyplotZpltZnetworkxr   Zgame_detailsr"   r   printZnumber_of_edgesZnumber_of_nodesZto_undirectedr#   Zconnected_componentsZGccr)   ZnodesedgesZopeningsr    r!   r%   summaryitemsr5   r+   ZGraphZ	edgewidthdictfromkeysr/   r*   dr   r;   ZnodesizeZkamada_kawai_layoutposZsubplotsZfigZaxZdraw_networkx_edgesZdraw_networkx_nodesZlabel_optionsZdraw_networkx_labelsZfont	set_titletextZ	transAxesZmarginsZtight_layoutZaxisshowr   r   r   r   <module>   s   





