summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/mpchess/mpchess-chessboard.mp
blob: 996e928274239f468c50eeafca16bd2159eca7b7 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454




% the chessboard
% chessboard_table[step][1-8][1-8] corresponding to step/A-H/1-8
% array 8x8 for each step of the game
numeric chessboard_table[][][];  
numeric chessboard_number;

% set the starting board
def init_chessboard =
    for i:=0 upto 500:
        _chess_wcaptures_nbr[i]:=0;
        _chess_bcaptures_nbr[i]:=0;
    endfor
    if(_chessSize=8):
    for i:=1 upto 8:
        for j:=1 upto 8:
         chessboard_table[0][i][j]:=0;
        endfor;
    endfor;
    for i:=1 upto 8:
        chessboard_table[0][i][2]:=_intWPawn;
        chessboard_table[0][i][7]:=_intBPawn;
    endfor;
    chessboard_table[0][1][1]:=_intWRook;chessboard_table[0][8][1]:=_intWRook;
    chessboard_table[0][1][8]:=_intBRook;chessboard_table[0][8][8]:=_intBRook; 
    chessboard_table[0][2][1]:=_intWKnight;chessboard_table[0][7][1]:=_intWKnight;
    chessboard_table[0][2][8]:=_intBKnight;chessboard_table[0][7][8]:=_intBKnight; 
    chessboard_table[0][3][1]:=_intWBishop;chessboard_table[0][6][1]:=_intWBishop;
    chessboard_table[0][3][8]:=_intBBishop;chessboard_table[0][6][8]:=_intBBishop;
    chessboard_table[0][4][1]:=_intWQueen;
    chessboard_table[0][4][8]:=_intBQueen;
    chessboard_table[0][5][1]:=_intWKing;
    chessboard_table[0][5][8]:=_intBKing;
    fi
    chessboard_number:=1;
enddef;

% clear the chessboard 
def clear_chessboard =
    for k:=0 upto 500:
        for i:=0 upto 20:
            for j:=0 upto 20:
                chessboard_table[k][i][j]:=0;
            endfor
        endfor
    endfor
enddef;

% function to clear an area specified with a arg of type "a1-b6","g1-f2", etc
def clear_areas(text plist)=
    save _first_letter,_firt_nbr, _second_letter,_second_nbr,_fl,_fn,_sl,_sn,_str;
    string _first_letter,_first_nbr, _second_letter,_second_nbr,_str;
    for _str = plist:
    _first_letter:=substring (0,1) of _str;
    _first_nbr:=substring (1,2) of _str;
    _second_letter:=substring (3,4) of _str;
    _second_nbr:=substring (4,5) of _str;
    _fl:=_lettre_to_int(_first_letter);
    _fn:=_str_to_int(_first_nbr);
    _sl:=_lettre_to_int(_second_letter);
    _sn:=_str_to_int(_second_nbr);
    for i:=_fl upto _sl:
        for j:=_fn upto _sn:
            chessboard_table[0][i][j]:=0;
        endfor
    endfor
    endfor
enddef;

% function to clear squares with a arg of type "a1-b6","g1-f2", etc
def clear_squares(text plist)=
    save _first_letter,_firt_nbr,_fl,_fn,_str;
    string _first_letter,_first_nbr,_str;
    for _str = plist:
    _first_letter:=substring (0,1) of _str;
    _first_nbr:=substring (1,2) of _str;
    _fl:=_lettre_to_int(_first_letter);
    _fn:=_str_to_int(_first_nbr);
    chessboard_table[0][_fl][_fn]:=0;
    endfor
enddef;

% function to clear an area specified with a arg of type "a","c",etc
def clear_files(text plist)=
    save _letter,_l,_str;
    string _letter,_str;
    for _str = plist:
    _letter:= _str;
    _l:=_lettre_to_int(_letter);
    for j:=1 upto _chessSize:
            chessboard_table[0][_l][j]:=0;
    endfor
    endfor
enddef;

% function to clear an area specified with a arg of type "1","5",etc
def clear_ranks(text plist)=
    save _nbr,_n,_str;
    string _nbr,_str;
    for _str = plist:
    _nbr:= _str;
    _n:=_str_to_int(_nbr);
    for i:=1 upto _chessSize:
            chessboard_table[0][i][_n]:=0;
    endfor
    endfor
enddef;

def _int_to_piece(expr i)=
    if(i=_intWPawn):
        _chess_wP
    elseif(i=_intBPawn):
        _chess_bP
    elseif(i=_intWKnight):
        _chess_wN
    elseif(i=_intBKnight):
        _chess_bN
    elseif(i=_intWBishop):
        _chess_wB
    elseif(i=_intBBishop):
        _chess_bB
    elseif(i=_intWRook):
        _chess_wR
    elseif(i=_intBRook):
        _chess_bR
    elseif(i=_intWQueen):
        _chess_wQ
    elseif(i=_intBQueen):
        _chess_bQ
    elseif(i=_intWKing):
        _chess_wK
    elseif(i=_intBKing):
        _chess_bK
    fi
enddef;

def _draw_step_chessboard(expr k) = 
    for i:=1 upto _chessSize:
        for j:=1 upto _chessSize:
            %show chessboard_table[k][i][j];
            if(chessboard_table[k][i][j] <> 0):
                draw ( _int_to_piece(chessboard_table[k][i][j]) shifted ((i-1)*_chessSquareU,(j-1)*_chessSquareU));
            fi
        endfor;
    endfor;
enddef; 


vardef _pawn_candidate(expr istart, jstart,iend,jend,wb,capture)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    % capture equal 1 if there is a capture
    save output;
    boolean output;
    output = false;
    if(((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1))):
        if(wb="white"):        
            if(jend=4): % on the line that allow 2 square move
                if(((jstart=3) or (jstart=2)) and (iend=istart)):
                    output:=true;
                fi
                if(((jstart=3) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture
                    output:=true;
                fi
            else:
                if((jstart=jend-1) and (istart=iend)): % straight move
                    output:=true;
                fi
                if(((jstart=jend-1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture
                    output:=true;
                fi
            fi
        else:
            if(jend=5): % on the line that allow 2 square move
                if(((jstart=6) or (jstart=7)) and (iend=istart)):
                    output:=true;
                fi
                if(((jstart=6) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture
                    output:=true;
                fi
            else:
                if((jstart=jend+1) and (istart=iend)): % straight move
                    output:=true;
                fi
                if(((jstart=jend+1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture
                    output:=true;
                fi
            fi
        fi
    fi
    output
enddef;

vardef _bishop_candidate(expr istart, jstart,iend,jend,wb)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    save output;
    boolean output;
    output = false;
    if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)):
        if(abs(iend-istart)=abs(jend-jstart)):
            output:=true;
        fi
    fi
    output
enddef;

vardef _rook_candidate(expr istart, jstart,iend,jend,wb)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    save output;
    boolean output;
    output = false;
    if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)):
        if((iend=istart) or (jend=jstart)):
            output:=true;
        fi
    fi
    output
enddef;


vardef _queen_candidate(expr istart, jstart,iend,jend,wb)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    save output;
    boolean output;
    output = false;
    if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)):
        if((iend-istart) or (jend-jstart) or (abs(iend-istart)=abs(jend-jstart))):
            output:=true;
        fi
    fi
    output
enddef;

vardef _king_candidate(expr istart, jstart,iend,jend,wb)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    save output;
    boolean output;
    output = false;
    if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)):
        if((abs(iend-istart)<=1) and (abs(jend-jstart)<=1)):
            output:=true;
        fi
    fi
    output
enddef;

vardef _knight_candidate(expr istart, jstart,iend,jend,wb)=
    % i int from letter, column
    % j int for line
    % return true if (istart,jstart) to (iend,jend)
    % is a possible mvt
    % wb is "white" or "black"
    save output;
    boolean output;
    output = false;
    if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)):
        if(((abs(iend-istart)=2) and (abs(jend-jstart)=1)) or ((abs(iend-istart)=1) and (abs(jend-jstart)=2))):
            output:=true;
        fi
    fi
    output
enddef;

vardef _chess_candidate(expr type, istart, jstart, iend, jend,wb,capture)=
    numeric _pm;
    _pm:=1;
    if(wb="black"):
        _pm:=-1;
    fi
    if(type=(_pm*_intWPawn)):
         _pawn_candidate(istart,jstart,iend,jend,wb,capture)
    elseif(type=_pm*_intWBishop):
        _bishop_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWKing):
        _king_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWKnight):
        _knight_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWQueen):
        _queen_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWRook):
        _rook_candidate(istart,jstart,iend,jend,wb)
    fi
enddef;

def _chess_copy_chessboard(expr step)=
    save i,j;
    for i:=1 upto _chessSize:
        for j:=1 upto _chessSize:
            chessboard_table[step][i][j]:=chessboard_table[step-1][i][j];
        endfor;
    endfor;
enddef;

def _chess_copy_captures(expr step)=
    save i,j;
    %show _chess_wcaptures_nbr[step-1];
    if(_chess_wcaptures_nbr[step-1]>0):
    for i:=0 upto _chess_wcaptures_nbr[step-1]-1:
        _chess_white_captures[step][i]:=_chess_white_captures[step-1][i];
    endfor;
    fi
    %show _chess_bcaptures_nbr[step-1];
    if(_chess_bcaptures_nbr[step-1]>0):
    for i:=0 upto _chess_bcaptures_nbr[step-1]-1:
        _chess_black_captures[step][i]:=_chess_black_captures[step-1][i];
    endfor;
    fi
enddef;

numeric _chess_white_captures[][],_chess_black_captures[][];
numeric _chess_wcaptures_nbr[], _chess_bcaptures_nbr[];
_chess_wcaptures_nbr[0]:=0;
_chess_bcaptures_nbr[0]:=0;

def _build_all_chessboards=
    save _wb;
    string _wb;
    init_chessboard; % k=0
    for k:=1 upto (_chess_step_numtable):
        _chess_copy_chessboard(k);
        _chess_copy_captures(k);
        _piecetype:=_chess_moves_table_numeric[k-1][0];
        _pm:=_chess_moves_table_numeric[k-1][1];
        _iend:=_chess_moves_table_numeric[k-1][2];
        _jend:=_chess_moves_table_numeric[k-1][3];
        _capture:=_chess_moves_table_numeric[k-1][4];
        _promotion:=_chess_moves_table_numeric[k-1][5];
        _ambiguity:=_chess_moves_table_numeric[k-1][6];
        if(_pm=1):
            _wb:="white";
        else:
            _wb:="black";
        fi        
        for i:=1 upto _chessSize:
            for j:=1 upto _chessSize:   
                if(chessboard_table[k][i][j]=_piecetype): 
                    if(_chess_candidate(_piecetype, i, j, _iend, _jend,_wb,_capture)=true):  
                        if(_capture<>0):
                            if(_pm=1):
                                _chess_white_captures[k][_chess_wcaptures_nbr[k-1]]:=chessboard_table[k][i][j];
                                _chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1]+1;
                                _chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1];
                            else:
                                _chess_black_captures[k][_chess_bcaptures_nbr[k-1]]:=chessboard_table[k][i][j];
                                _chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1]+1;
                                _chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1];
                            fi
                        else:
                            _chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1];
                            _chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1];
                        fi
                        
                        chessboard_table[k][i][j]:=0; %erase piece
                        if(_promotion<>0):
                            chessboard_table[k][_iend][_jend]:=_promotion;
                        else:
                            chessboard_table[k][_iend][_jend]:=_piecetype;
                        fi
                    fi
                fi
            endfor;
        endfor;
    endfor;
    chessboard_number:=_chess_step_numtable;
enddef;

def build_chessboards_from_pgn(expr s)=
    _build_chess_moves_tables(s);
    _build_all_chessboards;
enddef;


% set the starting empty board
def set_empty_chessboard =
    for i:=0 upto 500:
        _chess_wcaptures_nbr[i]:=0;
        _chess_bcaptures_nbr[i]:=0;
    endfor
    for i:=1 upto _chessSize:
        for j:=1 upto _chessSize:
         chessboard_table[0][i][j]:=0;
        endfor;
    endfor;
    chessboard_number:=1;
enddef;


def add_white_pieces(text plists) =
    save _str,_piecetype, _letter, _nbr,wb;
    string wb;
    wb:="white";
    for _str = plists:
        if(_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));
            fi
        else:
            _piecetype:=_intWPawn;
            if(length(_str)=2): % type f6
                _letter:=_lettre_to_int(_get_char(_str,0));
                _nbr:=_str_to_int(_get_char(_str,1)); 
            fi
        fi
        chessboard_table[0][_letter][_nbr]:=_piecetype;
    endfor
enddef;

def add_black_pieces(text plists) =
    save _str,_piecetype, _letter, _nbr,wb;
    string wb;
    wb:="black";
    for _str = plists:
        if(_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));
            fi
        else:
            _piecetype:=_intBPawn;
            if(length(_str)=2): % type f6
                _letter:=_lettre_to_int(_get_char(_str,0));
                _nbr:=_str_to_int(_get_char(_str,1)); 
            fi
        fi
        chessboard_table[0][_letter][_nbr]:=_piecetype;
    endfor
enddef;