Introduction
What is a PGN File?
A PGN file is a file that is worldwide used by most chess programs. The file basically stores the full game between two players.
In this article I am going to explain how to write your own PGN file using .NET
Using the code
A PGN File is basically a text file - that consist of two sections. The first section a description of the specific game - example what tournament the game was played at who was the players and some more info,
The second part is the actual games using standard FIDE notation.
So from a .NET point of view to write the file is fairly simple, because you only have to make a reference to the System.IO
namespace and then write the file.
'Start writing the PGN File
Dim sw As New StreamWriter("c:\PGNFileExample.pgn")
'First write the info about the pgn file (game)
'Make sure the info section start with a "[" sign and ends with a "]" sign.
sw.WriteLine("[Event ""TournamentName""]")
sw.WriteLine("[Site ""?""]")
sw.WriteLine("[Date ""2007.09.27""]")
sw.WriteLine("[Round ""1""]")
sw.WriteLine("[White ""Louw, G.""]")
sw.WriteLine("<Black ""Mills, R."">")
sw.WriteLine("[Result ""1-0""]")
sw.WriteLine("[ECO ""A50""]")
sw.WriteLine("[PlyCount ""25""]")
'Now write the actual game moves with standard FIDE notation
sw.WriteLine("1. c4 b6 2. d4 e6 3. a3 Bb7 4. Nc3 Nf6 5. d5 Bd6 6. Nf3 O-O 7. e4 exd5 8. exd5 c6 9. Be2 Na6 10. O-O Nc7
11. Bg5 Re8 12. Qd2 h6 13. Bh4 cxd5 14. cxd5 a6 15. Rfe1 b5 16. Nd4 b4 17. axb4
Bxb4 18. Bf3 Rxe1+ 19. Rxe1 Bxd5 20. Nf5 Bxf3 21. gxf3 Ncd5 22. Kh1 Nxc3 23. Rg1
g6 24. Qxh6 ")
'Lastly write the result
sw.WriteLine("1-0")
'Any chess program that reads PGN files should now be able to import your chess game!
sw.Close()
Points of Interest
Hope this article make it easy for you to understand what is a PGN file.
and how to write PGN files,
If you have any comments or questions you are more then welcome to mail me or post it on the comment section.