-
Notifications
You must be signed in to change notification settings - Fork 18
/
pgn_test.go
218 lines (196 loc) · 6.99 KB
/
pgn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package pgn
import (
"fmt"
"os"
"strings"
"testing"
"text/scanner"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) { TestingT(t) }
type PGNSuite struct{}
var _ = Suite(&PGNSuite{})
var simple = `[Event "State Ch."]
[Site "New York, USA"]
[Date "1910.??.??"]
[Round "?"]
[White "Capablanca"]
[Black "Jaffe"]
[Result "1-0"]
[ECO "D46"]
[Opening "Queen's Gambit Dec."]
[Annotator "Reinfeld, Fred"]
[WhiteTitle "GM"]
[WhiteCountry "Cuba"]
[BlackCountry "United States"]
1. d4 d5 2. Nf3 Nf6 3. e3 c6 4. c4 e6 5. Nc3 Nbd7 6. Bd3 Bd6
7. O-O O-O 8. e4 dxe4 9. Nxe4 Nxe4 10. Bxe4 Nf6 11. Bc2 h6
12. b3 b6 13. Bb2 Bb7 14. Qd3 g6 15. Rae1 Nh5 16. Bc1 Kg7
17. Rxe6 Nf6 18. Ne5 c5 19. Bxh6+ Kxh6 20. Nxf7+ 1-0
`
func (s *PGNSuite) TestParse(c *C) {
r := strings.NewReader(simple)
sc := scanner.Scanner{}
sc.Init(r)
game, err := ParseGame(&sc)
if err != nil {
c.Fatal(err)
}
if game.Tags["Site"] != "New York, USA" {
c.Fatal("Site tag wrong: ", game.Tags["Site"])
}
if len(game.Moves) == 0 || game.Moves[0].From != D2 || game.Moves[0].To != D4 {
c.Fatal("first move is wrong", game.Moves[0])
}
if len(game.Moves) != 39 || game.Moves[38].From != E5 || game.Moves[38].To != F7 {
c.Fatal("last move is wrong", game.Moves[38])
}
}
func (s *PGNSuite) TestPGNScanner(c *C) {
f, err := os.Open("polgar.pgn")
if err != nil {
c.Fatal(err)
}
ps := NewPGNScanner(f)
for ps.Next() {
game, err := ps.Scan()
if err != nil {
fmt.Println(game)
c.Fatal(err)
}
}
}
func (s *PGNSuite) TestPGNParseWithCheckmate(c *C) {
pgnstr := `[Event "Live Chess"]
[Site "Chess.com"]
[Date "2014.10.10"]
[White "MarkoMakaj"]
[Black "AndreyOstrovskiy"]
[Result "1-0"]
[WhiteElo "2196"]
[BlackElo "2226"]
[TimeControl "1|1"]
[Termination "MarkoMakaj won by checkmate"]
1.d4 g6 2.c4 Bg7 3.Nc3 c5 4.Nf3 cxd4 5.Nxd4 Nc6 6.Nc2 Nf6 7.g3 O-O 8.Bg2 b6 9.O-O Bb7 10.b3 Rc8
11.Bb2 Qc7 12.Qd2 Qb8 13.Ne3 Rfd8 14.Rfd1 e6 15.Rac1 Qa8 16.Nb5 d5 17.cxd5 exd5 18.Bxf6 Bxf6 19.Nxd5 Bg7 20.e4 a6
21.Nbc3 b5 22.Qf4 Qa7 23.Nf6+ Kh8 24.Ncd5 Nd4 25.Qh4 h6 26.Rxc8 Rxc8 27.e5 Ne6 28.Ng4 Rc2 29.Nde3 Rxa2 30.Nxh6 Bxg2
31.Kxg2 Bxe5 32.Nxf7+ Kg7 33.Nxe5 Qxe3 34.Qe7+ Kh6 35.Nf7+ Kh5 36.Qh4# 1-0
`
r := strings.NewReader(pgnstr)
sc := scanner.Scanner{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e3+ Kxe3 69.Kxa7 Kd4 70.Kb6 Rg1 71.a7 Rg8
72.Kb7 Rg7+ 73.Kb6 1-0`
r := strings.NewReader(pgnstr)
sc := scanner.Scanner{}
sc.Init(r)
game, err := ParseGame(&sc)
c.Assert(err, IsNil)
// fmt.Println(game)
c.Assert(game.Tags["Site"], Equals, "Leipzig")
c.Assert(len(game.Moves), Equals, 145)
}
func (s *PGNSuite) TestComments(c *C) {
pgnstr := `[Event "Ch World (match)"]
[Site "New York (USA)"]
[Date "1886.03.24"]
[EventDate "?"]
[Round "19"]
[Result "0-1"]
[White "Johannes Zukertort"]
[Black "Wilhelm Steinitz"]
[ECO "D53"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "58"]
1. d4 {Notes by Robert James Fischer from a television
interview. } d5 2. c4 e6 3. Nc3 Nf6 4. Bg5 Be7 5. Nf3 O-O
6. c5 {White plays a mistake already; he should just play e3,
naturally.--Fischer} b6 7. b4 bxc5 8. dxc5 a5 9. a3 {Now he
plays this fantastic move; it's the winning move. -- Fischer}
d4 {He can't take with the knight, because of axb4.--Fischer}
10. Bxf6 gxf6 11. Na4 e5 {This kingside weakness is nothing;
the center is easily winning.--Fischer} 12. b5 Be6 13. g3 c6
14. bxc6 Nxc6 15. Bg2 Rb8 {Threatening Bb3.--Fischer} 16. Qc1
d3 17. e3 e4 18. Nd2 f5 19. O-O Re8 {A very modern move; a
quiet positional move. The rook is doing nothing now, but
later...--Fischer} 20. f3 {To break up the center, it's his
only chance.--Fischer} Nd4 21. exd4 Qxd4+ 22. Kh1 e3 23. Nc3
Bf6 24. Ndb1 d2 25. Qc2 Bb3 26. Qxf5 d1=Q 27. Nxd1 Bxd1
28. Nc3 e2 29. Raxd1 Qxc3 0-1`
r := strings.NewReader(pgnstr)
sc := scanner.Scanner{}
sc.Init(r)
game, err := ParseGame(&sc)
c.Assert(err, Equals, nil)
c.Assert(game, NotNil)
c.Assert(game.Tags["Site"], Equals, "New York (USA)")
c.Assert(len(game.Moves),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
Qb2+ 74.Kh3 Qbb6 0-1`
func (s *PGNSuite) TestIssue9(c *C) {
r := strings.NewReader(issue9)
sc := scanner.Scanner{}
sc.Init(r)
_, err := ParseGame(&sc)
if err != nil {
c.Fatal(err)
}
}
var issue14 = `
1. e4 {[%clk 0:15:09.9]} 1... Nc6 {[%clk 0:15:06.6]} 2. Nf3 {[%clk 0:15:15.1]} 2... e5 {[%clk 0:15:02.9]} 3. Bc4 {[%clk 0:15:16.9]} 3... h6 {[%clk 0:14:39.1]} 4. d4 {[%clk 0:15:23.1]} 4... exd4 {[%clk 0:14:30.7]} 5. Nxd4 {[%clk 0:15:29.1]} 5... Bc5 {[%clk 0:14:19]} 6. c3 {[%clk 0:15:06.4]} 6... Qe7 {[%clk 0:14:17.3]} 7. Qf3 {[%clk 0:14:48]} 7... Nf6 {[%clk 0:14:22.9]} 8. O-O {[%clk 0:14:49.9]} 8... Qxe4 {[%clk 0:14:00.9]} 9. Qxe4+ {[%clk 0:14:51]} 9... Nxe4 {[%clk 0:14:07.7]} 10. Re1 {[%clk 0:15:00.2]} 10... f5 {[%clk 0:13:48.3]} 11. f3 {[%clk 0:14:47.9]} 11... Bxd4+ {[%clk 0:12:38.1]} 12. cxd4 {[%clk 0:14:57.8]} 12... Nxd4 {[%clk 0:12:47]} 1-0`
func (s *PGNSuite) TestIssue14(c *C) {
r := strings.NewReader(issue14)
sc := scanner.Scanner{}
sc.Init(r)
_, err := ParseGame(&sc)
if err != nil {
c.Fatal(err)
}
}