summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/mpchess/mpchess-pgn.mp
blob: 0ed11644b0d822c4281cad791a48428af128862a (plain)
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
boolean _chess_input_pgn;
% some intern macros to deal with blocks of a PNG line

vardef _delete_first_spaces(expr s)=
    save output;
    _total_length:= length s;
    string output;
    _spaces := 0 ;
    for j = 0 upto _total_length :
        if substring (j,j+1) of s = " " :  
            _spaces := _spaces+1 ; 
        else : 
            exitif true ; 
        fi 
    endfor  
    output :=  substring (_spaces,_total_length) of s;
    output
enddef;

vardef _get_first_block(expr s)=
    save output;
    string output;
    output:=_delete_first_spaces(s);
    _total_length:= length output;
    _size := 0 ;
    for j = 0 upto _total_length :
        if substring (j,j+1) of output <> " " :  
            _size := _size+1 ; 
        else: 
            exitif(true) ; 
        fi 
    endfor
    output :=  substring (0,_size) of output;
    output
enddef;

vardef _drop_first_block(expr s)=
    save output;
    string output;
    output:=_delete_first_spaces(s);
    _total_length:= length output;
    _size := 0 ;
    for j = 0 upto _total_length :
        if substring (j,j+1) of output <> " " :  
            _size := _size+1 ; 
        else : 
            exitif true ; 
        fi 
    endfor 
    output :=  substring (_size,_total_length) of output;
    output
enddef;

% table of moves:
% _chess_moves_table[step][0]->white move
% _chess_moves_table[step][1]->black move
% step starts at 1
string _chess_moves_table[][]; 
numeric _chess_moves_table_numeric[][];
numeric _chess_pgn_step;
numeric _chess_step_numtable;
_chess_step_numtable:=0; % total number of moves (white+black) halfmove
_chess_pgn_step:=0; % total numbre of "couple" moves (should be uncomplete without black move) 

def get_halfmove_number=
    _chess_step_numtable
enddef;

def get_totalmove_number=
    _chess_pgn_step
enddef;

% macro to deal the block (or half block) between to numbers of PNG step
% s = 3. Nf6 c6 4. [...]   
vardef _get_move(expr s)=
    save output,_step;
    string output;
    numeric _step;
    output:=s;
    % drop the step
    _step := scantokens(_get_first_block(output));
    output:=_drop_first_block(output); % get rid of i. 
    
    % get the white move and drop it
    _chess_moves_table[_chess_pgn_step][0]:=_get_first_block(output); %get the white move
    _extract_move(_chess_step_numtable,_get_first_block(output),"white"); % build the numeric table
    _chess_step_numtable:=_chess_step_numtable+1;
    output:=_drop_first_block(output); % rid of the treated block
    output:=_delete_first_spaces(output); 
    % get the black move and drop it
    if((output)<>""): 
        _chess_moves_table[_chess_pgn_step][1]:=_get_first_block(output);
        _extract_move(_chess_step_numtable,_get_first_block(output),"black");
        _chess_step_numtable:=_chess_step_numtable+1;
        output:=_drop_first_block(output);
    fi
    _chess_pgn_step:=_chess_pgn_step+1;
    output
enddef;

def _build_chess_moves_tables(expr s)=
    save output;
    string output;
    output:=s;
    forever:
        output:=_get_move(output);
        output:=_delete_first_spaces(output);
        exitif(output="");
    endfor
enddef;

% intern debug macro
def _show_chess_moves_table=
    for i:=0 upto _chess_pgn_step-1:
        show "move";
        show _chess_moves_table[i][0];
        if(2*i<_chess_step_numtable-1): % if the is no black move
            show _chess_moves_table[i][1];
        fi
    endfor 
enddef;

% intern debug macro
def _show_chess_moves_table_numeric=
    for i:=0 upto _chess_step_numtable-1:
        show "move";
        show _chess_moves_table_numeric[i][0];
        show _chess_moves_table_numeric[i][1];
        show _chess_moves_table_numeric[i][2];
        show _chess_moves_table_numeric[i][3];
        show _chess_moves_table_numeric[i][4];
        show _chess_moves_table_numeric[i][5];
        show _chess_moves_table_numeric[i][6];
    endfor 
enddef;

vardef _is_cap(expr c)=
    if((c="B") or (c="K") or (c="N") or (c="Q") or (c="R")):
        true
    else:
        false
    fi
enddef;

vardef _cap_to_int(expr c,wb)=
    numeric _pm;
    _pm:=1;
    if(wb="black"):
        _pm:=-1;
    fi
    if(c="B"):
        _pm*_intWBishop
    elseif(c="K"):
        _pm*_intWKing
    elseif(c="N"):
        _pm*_intWKnight
    elseif(c="Q"):
        _pm*_intWQueen
    elseif(c="R"):
        _pm*_intWRook
    fi
enddef;

vardef _cap_to_int(expr c,wb)=
    numeric _pm;
    _pm:=1;
    if(wb="black"):
        _pm:=-1;
    fi
    if(c="B"):
        _pm*_intWBishop
    elseif(c="K"):
        _pm*_intWKing
    elseif(c="N"):
        _pm*_intWKnight
    elseif(c="Q"):
        _pm*_intWQueen
    elseif(c="R"):
        _pm*_intWRook
    fi
enddef;

vardef _lettre_to_int(expr a)=
    if(a="a"):
        1
    elseif(a="b"):
        2
    elseif(a="c"):
        3
    elseif(a="d"):
        4
    elseif(a="e"):
        5
    elseif(a="f"):
        6
    elseif(a="g"):
        7
    elseif(a="h"):
        8
    fi
enddef;

def _str_to_int(expr i)=
    if(i="1"):
        1
    elseif(i="2"):
        2
    elseif(i="3"):
        3
    elseif(i="4"):
        4
    elseif(i="5"):
        5
    elseif(i="6"):
        6
    elseif(i="7"):
        7
    elseif(i="8"):
        8
    fi
enddef;



vardef _extract_move(expr _step,s,wb)=
    % function to convert a PGN move (ie Nc6)
    % to a array of numerics (type, wb,letter, nbr, capture,promotion,ambiguity)
    % no castle moves !!!
    % s is the string of PGN move
    % wb is a string equal to "white" or "black" 
    % s=Nc6,wb="white" -> (_intBKnigh,+-1,3,6,0,0,0)
    % capture is boolean
    % promotion gives the int corresponding to the piece
    % ambiguity gives the column if ambiguity 
    save _str,output,_piecetype,_letter,_nbr,_capture,_ambiguity;
    numeric _pm,_capture,_promotion,_ambiguity;
    _promotion:=0;
    _pm:=1;
    _capture:=0;
    _ambiguity:=0;
    if(wb="black"):
        _pm:=-1;
    fi
    numeric _piecetype,_letter,_nbr;
    string _str;
    numeric output[];
    _str := s;
    _length:= length(_str);
    if(str="O-O"):
        _piecetype:=100;
        _letter:=100;
        _nbr:=100;
    elseif(str="O-O-O"):
        _piecetype:=1000;
        _letter:=1000;
        _nbr:=1000;
    elseif(_is_cap(_get_char(_str,0))): % if the first char is a cap
        _piecetype:=_cap_to_int(_get_char(_str,0),wb);
        if(length(_str)=3): % type Nf6
            _letter:=_lettre_to_int(_get_char(_str,1));
            _nbr:=_str_to_int(_get_char(_str,2));
        elseif(length(_str)=4):
            if((_get_char(_str,3)="+") or (_get_char(_str,3)="#")): % type Nf4#
                _letter:=_lettre_to_int(_get_char(_str,1));
                _nbr:=_str_to_int(_get_char(_str,2));
            elseif(_get_char(_str,1)="x"): % capture Nxf4
                _capture:=1;
                _letter:=_lettre_to_int(_get_char(_str,2));
                _nbr:=_str_to_int(_get_char(_str,3));
            else: % ambiguity Rfe2
                _letter:=_lettre_to_int(_get_char(_str,2));
                _nbr:=_str_to_int(_get_char(_str,3));
                _ambiguity:=_lettre_to_int(_get_char(_str,1));
            fi
        elseif(length(_str)=5): 
            if(_get_char(_str,1)="x"): % capture type Nxf6+
                _capture:=1;
                _letter:=_lettre_to_int(_get_char(_str,2));
                _nbr:=_str_to_int(_get_char(_str,3));
            else: % ambiguity Rfe2(+|#)
                _letter:=_lettre_to_int(_get_char(_str,2));
                _nbr:=_str_to_int(_get_char(_str,3));
                _ambiguity:=_lettre_to_int(_get_char(_str,1));
            fi
        fi
    else:
        _piecetype:=_pm;
        if(length(_str)=2): % type f6
            _letter:=_lettre_to_int(_get_char(_str,0));
            _nbr:=_str_to_int(_get_char(_str,1));
        elseif(length(_str)=3): % type f6+
            _letter:=_lettre_to_int(_get_char(_str,0));
            _nbr:=_str_to_int(_get_char(_str,1));    
        elseif(length(_str)>=4): % type exd8(+/#) or e8=Q(+/#)
            if((_get_char(_str,1)="x") or (_get_char(_str,3)="#")): % type exd8
                _capture:=1;
                _letter:=_lettre_to_int(_get_char(_str,2));
                _nbr:=_str_to_int(_get_char(_str,3));
                _ambiguity:=_lettre_to_int(_get_char(_str,0));
            else: % capture e8=Q
                _promotion:=_lettre_to_int(_get_char(_str,3));
                _letter:=_lettre_to_int(_get_char(_str,0));
                _nbr:=_str_to_int(_get_char(_str,1));
            fi
        fi
    fi
    _chess_moves_table_numeric[_step][0]:=_piecetype;
    _chess_moves_table_numeric[_step][1]:=_pm;
    _chess_moves_table_numeric[_step][2]:=_letter;
    _chess_moves_table_numeric[_step][3]:=_nbr;
    _chess_moves_table_numeric[_step][4]:=_capture;
    _chess_moves_table_numeric[_step][5]:=_promotion;
    _chess_moves_table_numeric[_step][6]:=_ambiguity;
enddef;