o
    ˷e                     @   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rFe	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qed7 Ze	e qieeZdd e D Zee dZejddD ]:\Z ZZ!e!d "dZ#e#d d kree   d!7  < qe#d d"kree   d#7  < ee  d#7  < qee  d!7  < qd$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ECOSitechess_masters_WCC.pgn.bz2c                 C   s   ddl }t }i }|| }dd |D }W d   n1 s!w   Y  |D ]5}|drE|dd dd\}}|d	|t|< q(|r]|d
}	|d}
|j	|	|
fi | 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/ideatree/venv/lib/python3.10/site-packages/../../../share/doc/networkx-2.8.6/examples/drawing/plot_chess_masters.py
<listcomp>2       z#chess_pgn_graph.<locals>.<listcomp>[    "WhiteBlack)
bz2nxMultiDiGraphBZ2File
startswithsplitstripstrpopadd_edge)pgn_filer   Ggame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   )H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   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   B97z vs 
z   z: c                 C   s    g | ]\}}t t||qS r   )lenr"   get_edge_data)r
   uvr   r   r   r   `   s     g        r   -1g      ?z1/2g      ?c                 C   s   g | ]}t | d  qS )2   )wins)r
   r6   r   r   r   r   m   s    zReshevsky, Samuel H)皙?gzBotvinnik, Mikhail M)gQ?gQzSmyslov, Vassily V)r;   gQ)   r<   )figsizeg333333?m)alphawidth
edge_colorz#210070g?)	node_size
node_colorr?   kr(   gffffff?)ecfcr?      )	font_sizebbox	Helveticabold)fontnamecolor
fontweightfontsizez+World Chess Championship Games: 1886 - 1985rrM   g?g?zedge width = # games playedcenter)horizontalalignment	transformfontdictgQ?znode size = # games wonr;   off)r   )6__doc__matplotlib.pyplotpyplotpltnetworkxr   game_detailsr*   r"   printnumber_of_edgesnumber_of_nodesto_undirectedr+   connected_componentsGccr3   nodesedgesopeningsr(   r)   r.   summaryitemsrD   r6   Graph	edgewidthdictfromkeysr:   r5   dr   rP   nodesizekamada_kawai_layoutpossubplotsfigaxdraw_networkx_edgesdraw_networkx_nodeslabel_optionsdraw_networkx_labelsfont	set_titletext	transAxesmarginstight_layoutaxisshowr   r   r   r   <module>   s    




