Function Repository Resource:

ChessPGNDisplay

Source Notebook

Dynamically display chess games from a Portable Game Notation (PGN) string or file

Contributed by: Paco Jain (Wolfram Research)

ResourceFunction["ChessPGNDisplay"]["pgn",n]

interprets the string "pgn" as portable game notation (PGN) for chess games and displays the nth game.

ResourceFunction["ChessPGNDisplay"]["pgn"]

displays the first game.

Details and Options

The string given in the first argument to ResourceFunction["ChessPGNDisplay"] can be a raw PGN string or the path of a file containing PGN.
ResourceFunction["ChessPGNDisplay"] can also accept in its first argument an association of structured chess data of the type returned by ResourceFunction["ImportPGN"], or a list of such associations.

Examples

Basic Examples (2) 

Start with a PGN text string:

In[1]:=
myPGNString = "[Event \"DSB-07.Kongress\"]\n[Site \"Dresden\"]\n[Date \"1892.??.??\"]\n[Round \"16\"]\n[White \"Paulsen, Wilfried\"]\n[Black \"Von Gottschall, Hermann\"]\n[Result \"0-1\"]\n[ECO \"A00d\"]\n[PlyCount \"64\"]\n1. h3 e5 2. c4 Nc6 3. e3 Nf6 4. a3 d5 5. cxd5 Nxd5 6. Qc2 a6 7. Nf3 Be6\n8. Nc3 Be7 9. Ne4 O-O 10. Nc5 Bxc5 11. Qxc5 Qd7 12. b4 f6 13. Bb2 Rad8\n14. Rc1 Qf7 15. Qc2 Kh8 16. g4 Rfe8 17. b5 axb5 18. Bxb5 Rd6 19. Kf1 Nb6\n20. d3 Red8 21. d4 exd4 22. Bxd4 Bd5 23. e4 Bb3 24. Qc3 Nxd4 25. Nxd4 Rxd4\n26. Qxc7 Qxc7 27. Rxc7 Rxe4 28. Rxb7 Nc4 29. Kg2 Ne5 30. Bc6 Bc2\n31. Rc1 Rc4 32. Re1 Rxc6 0-1";

Display the game represented by this string:

In[2]:=
ResourceFunction["ChessPGNDisplay"][myPGNString]
Out[2]=

Click forward to view the board after black’s fourth move:

In[3]:=
ResourceFunction["ChessPGNDisplay"][myPGNString]
Out[3]=

Create a PGN containing multiple games and display the second game:

In[4]:=
mySecondPGN = "[Event \"USA-05.Congress\"]\n[Site \"New York\"]\n[Date \"1880.??.??\"]\n[Round \"1\"]\n[White \"Delmar, Eugene\"]\n[Black \"Judd, Max\"]\n[Result \"1/2-1/2\"]\n[ECO \"C48f\"]\n[PlyCount \"146\"]\n\n1. e4 e5 2. Nf3 Nc6 3. Nc3 Nf6 4. Bb5 Bc5 5. O-O d6 6. d4 exd4 7. Nxd4 Bxd4
 1/2-1/2";
ResourceFunction["ChessPGNDisplay"][
 StringRiffle[{myPGNString, mySecondPGN}, "\n\n"], 2]
Out[5]=

Scope (1) 

ChessPGNDisplay can also accept in its first argument an association of structured chess data of the type returned by ResourceFunction["ImportPGN"], or a list of such associations:

In[6]:=
gameData = ResourceFunction["ImportPGN"][myPGNString]
Out[6]=
In[7]:=
ResourceFunction["ChessPGNDisplay"][gameData]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 27 March 2019

Related Resources

License Information