Debian Codegraph @

texmf-dist/metapost/mpchess/mpchess-pgn.mp

raw
1 boolean _chess_input_pgn;
2 % some intern macros to deal with blocks of a PNG line
3
4 vardef _delete_first_spaces(expr s)=
5 save output;
6 _total_length:= length s;
7 string output;
8 _spaces := 0 ;
9 for j = 0 upto _total_length :
10 if substring (j,j+1) of s = " " :
11 _spaces := _spaces+1 ;
12 else :
13 exitif true ;
14 fi
15 endfor
16 output := substring (_spaces,_total_length) of s;
17 output
18 enddef;
19
20 vardef _get_first_block(expr s)=
21 save output;
22 string output;
23 output:=_delete_first_spaces(s);
24 _total_length:= length output;
25 _size := 0 ;
26 for j = 0 upto _total_length :
27 if substring (j,j+1) of output <> " " :
28 _size := _size+1 ;
29 else:
30 exitif(true) ;
31 fi
32 endfor
33 output := substring (0,_size) of output;
34 output
35 enddef;
36
37 vardef _drop_first_block(expr s)=
38 save output;
39 string output;
40 output:=_delete_first_spaces(s);
41 _total_length:= length output;
42 _size := 0 ;
43 for j = 0 upto _total_length :
44 if substring (j,j+1) of output <> " " :
45 _size := _size+1 ;
46 else :
47 exitif true ;
48 fi
49 endfor
50 output := substring (_size,_total_length) of output;
51 output
52 enddef;
53
54 % table of moves:
55 % _chess_moves_table[step][0]->white move
56 % _chess_moves_table[step][1]->black move
57 % step starts at 1
58 string _chess_moves_table[][];
59 numeric _chess_moves_table_numeric[][];
60 numeric _chess_pgn_step;
61 numeric _chess_step_numtable;
62 _chess_step_numtable:=0; % total number of moves (white+black) halfmove
63 _chess_pgn_step:=0; % total numbre of "couple" moves (should be uncomplete without black move)
64
65 def get_halfmove_number=
66 _chess_step_numtable
67 enddef;
68
69 def get_totalmove_number=
70 _chess_pgn_step
71 enddef;
72
73 % macro to deal the block (or half block) between to numbers of PNG step
74 % s = 3. Nf6 c6 4. [...]
75 vardef _get_move(expr s)=
76 save output,_step;
77 string output;
78 numeric _step;
79 output:=s;
80 % drop the step
81 _step := scantokens(_get_first_block(output));
82 output:=_drop_first_block(output); % get rid of i.
83
84 % get the white move and drop it
85 _chess_moves_table[_chess_pgn_step][0]:=_get_first_block(output); %get the white move
86 _extract_move(_chess_step_numtable,_get_first_block(output),"white"); % build the numeric table
87 _chess_step_numtable:=_chess_step_numtable+1;
88 output:=_drop_first_block(output); % rid of the treated block
89 output:=_delete_first_spaces(output);
90 % get the black move and drop it
91 if((output)<>""):
92 _chess_moves_table[_chess_pgn_step][1]:=_get_first_block(output);
93 _extract_move(_chess_step_numtable,_get_first_block(output),"black");
94 _chess_step_numtable:=_chess_step_numtable+1;
95 output:=_drop_first_block(output);
96 fi
97 _chess_pgn_step:=_chess_pgn_step+1;
98 output
99 enddef;
100
101 def _build_chess_moves_tables(expr s)=
102 save output;
103 string output;
104 output:=s;
105 forever:
106 output:=_get_move(output);
107 output:=_delete_first_spaces(output);
108 exitif(output="");
109 endfor
110 enddef;
111
112 % intern debug macro
113 def _show_chess_moves_table=
114 for i:=0 upto _chess_pgn_step-1:
115 show "move";
116 show _chess_moves_table[i][0];
117 if(2*i<_chess_step_numtable-1): % if the is no black move
118 show _chess_moves_table[i][1];
119 fi
120 endfor
121 enddef;
122
123 % intern debug macro
124 def _show_chess_moves_table_numeric=
125 for i:=0 upto _chess_step_numtable-1:
126 show "move";
127 show _chess_moves_table_numeric[i][0];
128 show _chess_moves_table_numeric[i][1];
129 show _chess_moves_table_numeric[i][2];
130 show _chess_moves_table_numeric[i][3];
131 show _chess_moves_table_numeric[i][4];
132 show _chess_moves_table_numeric[i][5];
133 show _chess_moves_table_numeric[i][6];
134 endfor
135 enddef;
136
137 vardef _is_cap(expr c)=
138 if((c="B") or (c="K") or (c="N") or (c="Q") or (c="R")):
139 true
140 else:
141 false
142 fi
143 enddef;
144
145 vardef _cap_to_int(expr c,wb)=
146 numeric _pm;
147 _pm:=1;
148 if(wb="black"):
149 _pm:=-1;
150 fi
151 if(c="B"):
152 _pm*_intWBishop
153 elseif(c="K"):
154 _pm*_intWKing
155 elseif(c="N"):
156 _pm*_intWKnight
157 elseif(c="Q"):
158 _pm*_intWQueen
159 elseif(c="R"):
160 _pm*_intWRook
161 fi
162 enddef;
163
164 vardef _cap_to_int(expr c,wb)=
165 numeric _pm;
166 _pm:=1;
167 if(wb="black"):
168 _pm:=-1;
169 fi
170 if(c="B"):
171 _pm*_intWBishop
172 elseif(c="K"):
173 _pm*_intWKing
174 elseif(c="N"):
175 _pm*_intWKnight
176 elseif(c="Q"):
177 _pm*_intWQueen
178 elseif(c="R"):
179 _pm*_intWRook
180 fi
181 enddef;
182
183 vardef _lettre_to_int(expr a)=
184 if(a="a"):
185 1
186 elseif(a="b"):
187 2
188 elseif(a="c"):
189 3
190 elseif(a="d"):
191 4
192 elseif(a="e"):
193 5
194 elseif(a="f"):
195 6
196 elseif(a="g"):
197 7
198 elseif(a="h"):
199 8
200 fi
201 enddef;
202
203 def _str_to_int(expr i)=
204 if(i="1"):
205 1
206 elseif(i="2"):
207 2
208 elseif(i="3"):
209 3
210 elseif(i="4"):
211 4
212 elseif(i="5"):
213 5
214 elseif(i="6"):
215 6
216 elseif(i="7"):
217 7
218 elseif(i="8"):
219 8
220 fi
221 enddef;
222
223 numeric _chess_extract_move_tab[];
224
225 vardef _extract_single_move(expr s,wb)=
226 % function to convert a PGN move (ie Nc6)
227 % to a array of numerics (type, wb,letter, nbr, capture,promotion,ambiguity)
228 % no castle moves !!!
229 % s is the string of PGN move
230 % wb is a string equal to "white" or "black"
231 % s=Nc6,wb="white" -> (_intBKnigh,+-1,3,6,0,0,0)
232 % capture is boolean
233 % promotion gives the int corresponding to the piece
234 % ambiguity gives the column if ambiguity
235 save _str,output,_piecetype,_letter,_nbr,_capture,_ambiguity;
236 numeric _pm,_capture,_promotion,_ambiguity;
237 _promotion:=0;
238 _pm:=1;
239 _capture:=0;
240 _ambiguity:=0;
241 if(wb="black"):
242 _pm:=-1;
243 fi
244 numeric _piecetype,_letter,_nbr;
245 string _str;
246 numeric output[];
247 _str := s;
248 _length:= length(_str);
249 if(_str="O-O"):
250 _piecetype:=100;
251 _letter:=100;
252 _nbr:=100;
253 elseif(_str="O-O-O"):
254 _piecetype:=1000;
255 _letter:=1000;
256 _nbr:=1000;
257 elseif(_is_cap(_get_char(_str,0))): % if the first char is a cap
258 _piecetype:=_cap_to_int(_get_char(_str,0),wb);
259 if(length(_str)=3): % type Nf6
260 _letter:=_lettre_to_int(_get_char(_str,1));
261 _nbr:=_str_to_int(_get_char(_str,2));
262 elseif(length(_str)=4):
263 if((_get_char(_str,3)="+") or (_get_char(_str,3)="#")): % type Nf4#
264 _letter:=_lettre_to_int(_get_char(_str,1));
265 _nbr:=_str_to_int(_get_char(_str,2));
266 elseif(_get_char(_str,1)="x"): % capture Nxf4
267 _capture:=1;
268 _letter:=_lettre_to_int(_get_char(_str,2));
269 _nbr:=_str_to_int(_get_char(_str,3));
270 else: % ambiguity Rfe2
271 _letter:=_lettre_to_int(_get_char(_str,2));
272 _nbr:=_str_to_int(_get_char(_str,3));
273 _ambiguity:=_lettre_to_int(_get_char(_str,1));
274 fi
275 elseif(length(_str)=5):
276 if(_get_char(_str,1)="x"): % capture type Nxf6+
277 _capture:=1;
278 _letter:=_lettre_to_int(_get_char(_str,2));
279 _nbr:=_str_to_int(_get_char(_str,3));
280 else: % ambiguity Rfe2(+|#)
281 _letter:=_lettre_to_int(_get_char(_str,2));
282 _nbr:=_str_to_int(_get_char(_str,3));
283 _ambiguity:=_lettre_to_int(_get_char(_str,1));
284 fi
285 fi
286 else:
287 _piecetype:=_pm;
288 if(length(_str)=2): % type f6
289 _letter:=_lettre_to_int(_get_char(_str,0));
290 _nbr:=_str_to_int(_get_char(_str,1));
291 elseif(length(_str)=3): % type f6+
292 _letter:=_lettre_to_int(_get_char(_str,0));
293 _nbr:=_str_to_int(_get_char(_str,1));
294 elseif(length(_str)>=4): % type exd8(+/#) or e8=Q(+/#)
295 if((_get_char(_str,1)="x") or (_get_char(_str,3)="#")): % type exd8
296 _capture:=1;
297 _letter:=_lettre_to_int(_get_char(_str,2));
298 _nbr:=_str_to_int(_get_char(_str,3));
299 _ambiguity:=_lettre_to_int(_get_char(_str,0));
300 else: % capture e8=Q
301 _promotion:=_lettre_to_int(_get_char(_str,3));
302 _letter:=_lettre_to_int(_get_char(_str,0));
303 _nbr:=_str_to_int(_get_char(_str,1));
304 fi
305 fi
306 fi
307 _chess_extract_move_tab[0]:=_piecetype;
308 _chess_extract_move_tab[1]:=_pm;
309 _chess_extract_move_tab[2]:=_letter;
310 _chess_extract_move_tab[3]:=_nbr;
311 _chess_extract_move_tab[4]:=_capture;
312 _chess_extract_move_tab[5]:=_promotion;
313 _chess_extract_move_tab[6]:=_ambiguity;
314 enddef;
315
316 vardef _extract_move(expr _step,s,wb)=
317 _extract_single_move(s,wb);
318 _chess_moves_table_numeric[_step][0]:=_chess_extract_move_tab[0];
319 _chess_moves_table_numeric[_step][1]:=_chess_extract_move_tab[1];
320 _chess_moves_table_numeric[_step][2]:=_chess_extract_move_tab[2];
321 _chess_moves_table_numeric[_step][3]:=_chess_extract_move_tab[3];
322 _chess_moves_table_numeric[_step][4]:=_chess_extract_move_tab[4];
323 _chess_moves_table_numeric[_step][5]:=_chess_extract_move_tab[5];
324 _chess_moves_table_numeric[_step][6]:=_chess_extract_move_tab[6];
325 enddef;
326
327 def build_chessboards_from_pgn(expr s)=
328 _build_chess_moves_tables(s);
329 _build_all_chessboards;
330 enddef;
331
332 def build_chessboards_from_pgn_file(expr s)=
333 set_empty_chessboard;
334 string _rf;
335 _rf:=readfrom s;
336 _build_chess_moves_tables(_rf);
337 _build_all_chessboards;
338 enddef;