
    Gd                     L   d Z ddlmZ ddlZg dZdEdZ e            Z e	de
                                 de                                 d           e                                Zd	  ej        e          D             Z ee          d
k    r& e	ded
                                                     d e                    d          D             Z e	d ee           d            e	d            e	d           e                    d          D ]P\  ZZZed         dk    r>e de dZe                                D ]\  ZZede de dz  Zedz  Z e	e           Q ej        e          Zd e                                D             Ze                    e                                d          Ze                    d          D ]{\  Z ZZ!e!d         "                    d          Z#e#d         dk    ree xx         dz  cc<   >e#d         dk    r!ee xx         d z  cc<   eexx         d z  cc<   keexx         dz  cc<   |d! eD             Z$ ej%        e          Z&e&d"xx         d#z  cc<   e&d$xx         d%z  cc<   e&d&xx         d'z  cc<    ej'        d()          \  Z(Z) ej*        ee&d*ed+,            ej+        ee&e$d-d./           d0d1d2d3Z, ej-        ee&d4e,5           d6d0d7d4d8Z.e)/                    d9e.           d:e.d;<   e)0                    d<d=d>d?e)j1        e.@           e)0                    d<dAdBd?e)j1        e.@           e)2                    d=dC           e(3                                  ej4        dD            ej5                     dS )Fa
  
=============
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                    ddl }t          j                    }i }|                    |           5 }d |D             }ddd           n# 1 swxY w Y   |D ]}|                    d          rG|dd                             dd          \  }}|                    d          |t          |          <   ^|r;|                    d	          }	|                    d
          }
 |j	        |	|
fi | i }|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                 \    g | ])}|                                                     d           *S )z
)decoderstrip).0lines     =share/doc/networkx-3.1/examples/drawing/plot_chess_masters.py
<listcomp>z#chess_pgn_graph.<locals>.<listcomp>2   s.    CCC$%%f--CCC    [    "WhiteBlack)
bz2nxMultiDiGraphBZ2File
startswithsplitstripstrpopadd_edge)pgn_filer   Ggamedatafilelinesr   tagvaluewhiteblacks              r   chess_pgn_graphr,   $   sZ    JJJ
AD	X		 D(CC(CCCD D D D D D D D D D D D D D D  ??3 
	ad))#q11JC"[[--DSNN  ))))
5%004000Hs   AAAzLoaded z chess games between z	 players
c                 B    g | ]}t                               |          S  )Hsubgraph)r   cs     r   r   r   J   s"    999qzz!}}999r   r   z/Note the disconnected component consisting of:
c                 &    h | ]\  }}}|d          S )r   r.   )r   r*   r+   	game_infos       r   	<setcomp>r4   O   s$    QQQ!:%	IeQQQr   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                 d    g | ]-\  }}t          t                              ||                    .S r.   )lenr$   get_edge_data)r   uvs      r   r   r   `   s2    >>>DAqSA&&''>>>r   g        r   -1g      ?z1/2g      ?c                 .    g | ]}t           |         d z  S )2   )wins)r   r<   s     r   r   r   m   s     $$$QDGbL$$$r   zReshevsky, Samuel H)皙?gzBotvinnik, Mikhail M)gQ?gQzSmyslov, Vassily V)rB   gQ)   rC   )figsizeg333333?m)alphawidth
edge_colorz#210070g?)	node_size
node_colorrF   kr*   gffffff?)ecfcrF      )	font_sizebbox	Helveticabold)fontnamecolor
fontweightfontsizez+World Chess Championship Games: 1886 - 1985rrT   g?g?zedge width = # games playedcenter)horizontalalignment	transformfontdictgQ?znode size = # games wonrB   off)r   )6__doc__matplotlib.pyplotpyplotpltnetworkxr   game_detailsr,   r$   printnumber_of_edgesnumber_of_nodesto_undirectedr/   connected_componentsGccr9   nodesedgesopeningsr*   r+   r3   summaryitemsrK   r<   Graph	edgewidthdictfromkeysrA   r;   dr   rW   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   <module>r      s   6            :99   < O Wa!!WW8I8I8K8KWWW  
 OO995b5a889993s88a<<	E
MSV\\^^
M
MNNN RQagg4g>P>PQQQ >33x==>>> ? ? ? 5 6 6 6 > ? ? ? wwDw11  E5)5  )))))OO%% 	( 	(DAq'Q''!''''GG4g BHQKK ?>AGGII>>>	 }}QWWYY$$wwDw!!  GAq!	(#Ats{{Q3	
1Q3Q3Q3$$!$$$ bQ    m +       } ,       ] *   
#,x
(
(
(R  q#S	c J J J J  q#is S S S S'C88  3"= A A A A  #VQSTT :D A A AW ! l      l     

3           




r   