-
Notifications
You must be signed in to change notification settings - Fork 10
/
pgntofen.test.py
executable file
·324 lines (284 loc) · 14.9 KB
/
pgntofen.test.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/python
# coding=utf8
import unittest
import pgntofen
class PgnToFenTester(unittest.TestCase):
def test_promote_move(self):
correctFen = 'Qnbqkbnr/pppppppp/8/8/8/8/1PPPPPPP/RNBQKBNR'
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.pgnToFen(['a8=Q']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_pessant_take_move(self):
correctFen = 'rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR'
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.pgnToFen(['e4','d5','exd5']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_black_pessant_move(self):
correctFen = 'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR'
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.pgnToFen(['e4','e5']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_castling_move(self):
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQ1RK1'
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.pgnToFen(['O-O'])
self.assertEqual(correctFen, pgnConverter.getFen())
correctFen = '1nkr1bnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQ1RK1'
pgnConverter.pgnToFen(['O-O-O'])
self.assertEqual(correctFen, pgnConverter.getFen())
def test_black_pessant_move(self):
correctFen = 'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR'
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.pgnToFen(['e4','e5']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_knight_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkb1r/pppppppp/5n2/8/8/5N2/PPPPPPPP/RNBQKB1R'
pgnConverter.moves(['Nf3','Nf6']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_column_knight_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkb1r/pppppppp/5n2/8/8/8/PPPNPPPP/RNBQKB1R'
pgnConverter.pgnToFen(['Nf3','Nf6', 'Nfd2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_row_knight_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkb1r/pppppppp/5n2/8/8/8/PPPNPPPP/RNBQKB1R'
pgnConverter.pgnToFen(['Nf3','Nf6', 'N3d2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_bishop_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PBPPPPPP/RN1QKBNR'
pgnConverter.pgnToFen(['Bb2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_bishop_advanced_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PBPPPPPP/RN1QKBNR'
pgnConverter.pgnToFen(['B1b2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_rook_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/1ppppppp/p7/8/8/P7/RPPPPPPP/1NBQKBNR'
pgnConverter.pgnToFen(['a3', 'a6', 'Ra2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_rook_on_line_but_one_is_blocked_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'R','1','1','1','K','1','1','R',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r']
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/8/3RK2R'
pgnConverter.pgnToFen(['Rd1']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_rook_on_line_but_one_is_blocked_move2(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'R','1','1','1','K','1','1','R',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r']
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/8/R3KR2'
pgnConverter.pgnToFen(['Rf1']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_rook_on_column_but_one_is_blocked_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'R','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'K','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'R','1','1','1','1','1','1','1',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r']
correctFen = 'rnbqkbnr/pppppppp/R7/8/K7/R7/8/8'
pgnConverter.pgnToFen(['Ra3']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_rook_on_column_but_one_is_blocked_move2(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'R','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'K','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'R','1','1','1','1','1','1','1',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r']
correctFen = 'rnbqkbnr/pppppppp/8/R7/K7/8/8/R7'
pgnConverter.pgnToFen(['Ra5']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_king_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBK1BNR'
pgnConverter.pgnToFen(['Kd1']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_chess_game(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = '8/8/4R1p1/2k3p1/1p4P1/1P1b1P2/3K1n2/8'
pgnFormat = 'e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7 Re1 b5 Bb3 d6 c3 O-O h3 Nb8 d4 Nbd7 c4 c6 cxb5 axb5 Nc3 Bb7 Bg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Converter.pgnToFen(pgnFormat.split(' '))
self.assertEqual(correctFen, pgnConverter.getFen())
def test_queen_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPQPPPP/RNB1KBNR'
pgnConverter.pgnToFen(['Qd2']);
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPQPPP/RNB1KBNR'
pgnConverter.pgnToFen(['Qe2']);
self.assertEqual(correctFen, pgnConverter.getFen())
def test_enpassent_move(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
correctFen = 'rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b d3 KQkq'
pgnConverter.pgnToFen(['d4']);
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_self_discovery_chek_rook_move_diagonal(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'K','1','1','1','1','1','1','R',
'1','1','1','1','1','1','1','1',
'1','1','R','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','q','1',
'1','1','1','1','1','1','1','1']
pgnConverter.pgnToFen(['Rc1']);
correctFen = '8/6q1/8/8/8/2R5/8/K1R5 b - Qkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_self_discovery_chek_rook_move_line(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'K','1','R','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'R','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'q','1','1','1','1','1','1','1',
'1','1','1','1','1','1','1','1']
pgnConverter.pgnToFen(['Rc3']);
correctFen = '8/q7/8/8/8/R1R5/8/K7 b - KQkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_self_discovery_chek_rook_move_diagnal_another_pos(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'1','1','1','K','1','1','1','1',
'1','1','R','1','1','1','1','1',
'1','1','1','1','1','1','1','1',
'1','1','1','1','1','1','R','1',
'1','1','1','1','1','1','1','q',
'1','1','1','1','1','1','1','1',
'1','1','1','1','q','1','1','1',
'1','1','1','1','1','1','1','1']
pgnConverter.pgnToFen(['Rc4']);
correctFen = '8/4q3/8/7q/2R3R1/8/8/3K4 b - KQkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_full_game_3(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
moves = "e4 c5 Nf3 d6 d4 cxd4 Nxd4 Nf6 Nc3 Nc6 Bg5 e5 Bxf6 gxf6 Nf5 Bxf5 exf5 Be7 Nd5 O-O c3 Kh8 Bd3 Nb8 O-O Nd7 Be4 Nb6 Nxb6 Qxb6 Qb3 Qc7 Bd5 Bd8 Rad1 Qd7 Rd3 Qxf5 Rf3 Qg6 Qxb7 Bb6 Be4 Qg4 Bxh7 Kxh7 Rh3+ Qxh3 gxh3 f5 Kh1 Rae8 Rg1 Bxf2 Rg2 Be3 Qf3 Bh6 Qxf5+ Kh8 Qf6+ Kh7 Qxd6 e4 Rg4 Re6 Qc5 e3 Qf5+ Kh8 Re4 Rg6 Rg4 Re6 Re4 Rg6 Rh4 Kg7 Qe5+ Kg8 Rg4 Kh7 Qe4 Rg8 Qf5 Rf8 Kg2 Kg7 Kf3 Kh8 Qe5+ Kh7 Qe4 Kh8 Ke2 Re6 Qd4+ Kh7 Qxa7 Rf6 Qd4 Rf2+ Ke1 Re8 Qd3+ Kh8 Qd6 Kh7 Rh4 Re6 Qd3+ Kh8 Qd4+ Kh7 Qd3+ Kh8 Rxh6+ Rxh6 Qxe3 Rff6 Qd4 Kg8 a4 Re6+ Kf2 Ref6+ Kg3 Rhg6+ Kh4 Rd6 Qe4 Rde6 Qa8+ Kg7 Qd5 Re2 Qd4+ Kh7 Qf4 Kg8 Qb8+ Kg7 Qf4 Reg2 Qd4+ Kh7 Qe4 Rxb2 a5 Rb8 a6 Rh8 Qe5 Kg8+"
pgnConverter.moves(moves.split(' '));
correctFen = '6kr/5p2/P5r1/4Q3/7K/2P4P/7P/8 w - -'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_file_accecpt_file(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
file = "test/Carlsen.pgn"
stats = pgnConverter.pgnFile(file);
self.assertEqual(len(stats['failed']), 0)
self.assertEqual(len(stats['succeeded']), 1974)
def test_moves_accecpt_str(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
moves = "1.e4 c5 2.Nf3 d6 1/2-1/2"
pgnConverter.moves(moves);
correctFen = 'rnbqkbnr/pp2pppp/3p4/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R w - KQkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_real_example_self_discovery_chess(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'1','1','1','1','1','1','1','K',
'P','P','1','1','1','1','1','P',
'1','1','P','1','p','1','1','P',
'1','1','1','1','Q','1','R','1',
'1','1','1','1','1','1','1','q',
'1','1','1','1','1','1','r','b',
'p','1','1','1','1','p','1','k',
'1','1','1','1','1','r','1','1']
pgnConverter.whiteToMove = False
pgnConverter.pgnToFen(['Rg8']);
correctFen = '6r1/p4p1k/6rb/7q/4Q1R1/2P1p2P/PP5P/7K w - KQkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_game_4(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
moves = 'd4 d5 c4 c6 Nf3 Nf6 Nc3 e6 e3 Nbd7 Qc2 Bd6 b3 O-O Be2 b6 Bb2 Qe7 O-O Bb7 Rfe1 Rfe8 Rad1 Rad8 Bf1 Bb4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xd3 Bxd3 Kd4'
pgnConverter.pgnToFen(moves.split());
correctFen = '8/8/3k4/1P6/3K4/1P1b1P1p/8/8 b - -'
self.assertEqual(correctFen, pgnConverter.getFullFen())
def test_real_example_self_discovery_chess2(self):
pgnConverter = pgntofen.PgnToFen()
pgnConverter.resetBoard()
pgnConverter.internalChessBoard = [
'1', '1', 'K', 'R', '1', 'B', '1', 'R',
'P', 'P', 'P', '1', '1', 'P', 'P', 'P',
'1', '1', 'N', '1', '1', 'N', '1', '1',
'1', '1', '1', '1', 'P', '1', '1', '1',
'1', '1', '1', '1', 'p', '1', 'B', '1',
'1', '1', '1', 'b', 'b', 'n', '1', '1',
'p', 'p', 'p', '1', '1', 'p', 'p', 'p',
'r', 'n', '1', 'k', '1', '1', '1', 'r']
pgnConverter.whiteToMove = False
pgnConverter.pgnToFen(['Nd7']);
correctFen = 'r2k3r/pppn1ppp/3bbn2/4p1B1/4P3/2N2N2/PPP2PPP/2KR1B1R w - KQkq'
self.assertEqual(correctFen, pgnConverter.getFullFen())
if __name__ == '__main__':
unittest.main()