65.9K
CodeProject 正在变化。 阅读更多。
Home

什么是 PGN 文件以及如何编写 PGN 文件

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.14/5 (9投票s)

2007 年 9 月 27 日

viewsIcon

60540

downloadIcon

297

什么是 PGN 文件以及如何编写 PGN 文件

Screenshot - PGNFileExample.gif

引言

PGN 文件是什么?

PGN 文件是大多数国际象棋程序普遍使用的文件。该文件基本上存储了两位玩家之间的完整棋局。

在本文中,我将解释如何使用 .NET 编写自己的 PGN 文件。

使用代码

PGN 文件基本上是一个文本文件 - 它由两个部分组成。第一部分是特定棋局的描述 - 例如,棋局是在哪个比赛中进行的,谁是玩家,以及更多信息。

第二部分是使用标准国际象棋记谱法记录的实际棋局。

因此,从 .NET 的角度来看,编写文件相当简单,因为你只需要引用 System.IO 命名空间,然后写入文件即可。

'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("")

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()


关注点

希望这篇文章能让你更容易理解 PGN 文件是什么。

以及如何编写 PGN 文件,

如果您有任何意见或问题,欢迎给我发邮件或在评论区发帖。

© . All rights reserved.