a
    d                     @   s  d Z ddlmZ ddlZg dZdHd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  < qld$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d.d/ ej+ee&e$d0d1d2 d3d4d5d6Z,ej-ee&d7e,d8 d9d3d:d7d;Z.e)/d<e. d=e.d>< e)j0d?d@dAdBe)j1e.dC e)j0d?dDdEdBe)j1e.dC e)2d@dF e(3  e4dG e5  dS )Ia
  
=============
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 d   n1 sB0    Y  |D ]j}|dr|dd dd\}}|d	|t|< qP|rP|d
}	|d}
|j	|	|
fi | i }qP|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/stable-diffusion-webui/venv/lib/python3.9/site-packages/../../../share/doc/networkx-3.1/examples/drawing/plot_chess_masters.py
<listcomp>2       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   r   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   r   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.
r   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   `   r   g        r   -1g      ?z1/2g      ?c                 C   s   g | ]}t | d  qS )2   )wins)r	   r,   r   r   r   r   m   r   zReshevsky, Samuel H)皙?gzBotvinnik, Mikhail M)gQ?gQzSmyslov, Vassily V)r1   gQ)   r2   )Zfigsizeg333333?m)alphawidthZ
edge_colorz#210070g?)Z	node_sizeZ
node_colorr4   kr!   gffffff?)ZecZfcr4      )Z	font_sizeZbboxZ	HelveticaZbold)ZfontnamecolorZ
fontweightZfontsizez+World Chess Championship Games: 1886 - 1985rr8   g?g?zedge width = # games playedcenter)ZhorizontalalignmentZ	transformZfontdictgQ?znode size = # games wonr1   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*   nodesedgesZopeningsr!   r"   r&   summaryitemsr6   r,   ZGraphZ	edgewidthdictfromkeysr0   r+   dr   r9   ZnodesizeZkamada_kawai_layoutposZsubplotsZfigaxZdraw_networkx_edgesZdraw_networkx_nodesZlabel_optionsZdraw_networkx_labelsfont	set_titletextZ	transAxesZmarginsZtight_layoutZaxisshowr   r   r   r   <module>   s   





