Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / Visual Basic
Article

What is a PGN file and how to write PGN files

Rate me:
Please Sign up or sign in to vote.
1.14/5 (9 votes)
26 Sep 2007 58.2K   278   6   12
What is a PGN file and how to write PGN files
Screenshot - PGNFileExample.gif

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
Gideon Louw is a qualified MCSD and software professional.

He developed several industrial and government related applications that has successfully been installed all over the world.

Comments and Discussions

 
GeneralMy vote of 1 Pin
cariolihome21-Aug-11 5:17
cariolihome21-Aug-11 5:17 
GeneralWorthless Pin
MKauffman27-Sep-07 6:34
MKauffman27-Sep-07 6:34 
I don't mean to sound harsh, but this example is worthless. A more accurate title would be "How to create a StreamWriter and invoke the WriteLine method," because that is the only technical information you're providing in this article. If someone were interested in the PGN spec, I'm sure he or she would search for it through normal means[^], rather than looking for an poorly-written and elementary example in C#.

Perhaps if you wanted to make this article worthwhile, you could present a unique mechanism for serializing a game to this file format (though serialization articles are far too prevalent on this site anyway).

Better luck next time.
GeneralRe: Worthless Pin
Louwgi30-Sep-07 21:18
Louwgi30-Sep-07 21:18 
GeneralRe: Worthless Pin
MKauffman1-Oct-07 6:30
MKauffman1-Oct-07 6:30 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:07
Louwgi1-Oct-07 22:07 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:12
Louwgi1-Oct-07 22:12 
GeneralRe: Worthless Pin
Louwgi1-Oct-07 22:14
Louwgi1-Oct-07 22:14 
GeneralRe: Worthless Pin
MKauffman2-Oct-07 7:10
MKauffman2-Oct-07 7:10 
GeneralRe: Worthless Pin
Louwgi2-Oct-07 21:33
Louwgi2-Oct-07 21:33 
GeneralRe: Worthless Pin
Espiritu19-Aug-11 4:22
Espiritu19-Aug-11 4:22 
GeneralIf you could.... Pin
gwestwater27-Sep-07 2:30
gwestwater27-Sep-07 2:30 
GeneralRe: If you could.... Pin
Louwgi27-Sep-07 2:40
Louwgi27-Sep-07 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.