summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/mpchess/mpchess-chessboard.mp
blob: 4e0fc3bed35e2c4a12aa326037334712c5e67f69 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804




% 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,ambiguity)=
    % 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)):
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
                if(((jstart=3) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
            else:
                if((jstart=jend-1) and (istart=iend)): % straight move
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
                if(((jstart=jend-1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
            fi
        else:
            if(jend=5): % on the line that allow 2 square move
                if(((jstart=6) or (jstart=7)) and (iend=istart)):
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
                if(((jstart=6) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
            else:
                if((jstart=jend+1) and (istart=iend)): % straight move
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
                if(((jstart=jend+1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture
                    if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                        output:=true;
                    fi
                fi
            fi
        fi
    fi
    output
enddef;

vardef _bishop_candidate(expr istart, jstart,iend,jend,wb,ambiguity)=
    % 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)):
            if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                output:=true;
            fi
        fi
    fi
    output
enddef;

vardef _rook_candidate(expr istart, jstart,iend,jend,wb,ambiguity)=
    % 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)):
            if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                output:=true;
            fi
        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,ambiguity)=
    % 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))):
            if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))):
                output:=true;
            fi
        fi
    fi
    output
enddef;

vardef _chess_candidate(expr type, istart, jstart, iend, jend,wb,capture,ambiguity)=
    numeric _pm;
    _pm:=1;
    if(wb="black"):
        _pm:=-1;
    fi
    if(type=(_pm*_intWPawn)):
         _pawn_candidate(istart,jstart,iend,jend,wb,capture,ambiguity)
    elseif(type=_pm*_intWBishop):
        _bishop_candidate(istart,jstart,iend,jend,wb,ambiguity)
    elseif(type=_pm*_intWKing):
        _king_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWKnight):
        _knight_candidate(istart,jstart,iend,jend,wb,ambiguity)
    elseif(type=_pm*_intWQueen):
        _queen_candidate(istart,jstart,iend,jend,wb)
    elseif(type=_pm*_intWRook):
        _rook_candidate(istart,jstart,iend,jend,wb,ambiguity)
    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
        %  castling
        if(_piecetype=100): % short castling
            if(_pm=1):
                _tmp_line:=1;    
            else:
                _tmp_line:=8;
            fi
            if((chessboard_table[k][5][_tmp_line]=_pm*_intWKing) and
            (chessboard_table[k][8][_tmp_line]=_pm*_intWRook)): %  if king and
            % in the right places
                chessboard_table[k][5][_tmp_line]:=0;
                chessboard_table[k][8][_tmp_line]:=0;
                chessboard_table[k][6][_tmp_line]:=_pm*_intWRook;
                chessboard_table[k][7][_tmp_line]:=_pm*_intWKing;
            fi
        fi
        if(_piecetype=1000): % long castling
            if(_pm=1):
                _tmp_line:=1;    
            else:
                _tmp_line:=8;
            fi
            if((chessboard_table[k][5][_tmp_line]=_pm*_intWKing) and
            (chessboard_table[k][1][_tmp_line]=_pm*_intWRook)): %  if king and
            % in the right places
                chessboard_table[k][5][_tmp_line]:=0;
                chessboard_table[k][1][_tmp_line]:=0;
                chessboard_table[k][4][_tmp_line]:=_pm*_intWRook;
                chessboard_table[k][3][_tmp_line]:=_pm*_intWKing;
            fi
        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,_ambiguity)=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;


% 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;




def _draw_main_lines(expr _k)(expr wb)(text slist)=
    save _str;
    _chess_arrow_width_save:=_chess_arrow_width;
    _step:=1.4;
    for _str = slist:
        _chess_arrow_width:=(0.1*_step)*_chessSquareU;
        _extract_single_move(_str,wb);
        _piecetype:=_chess_extract_move_tab[0];
        _pm:=_chess_extract_move_tab[1];
        _iend:=_chess_extract_move_tab[2];
        _jend:=_chess_extract_move_tab[3];
        _capture:=_chess_extract_move_tab[4];
        _promotion:=_chess_extract_move_tab[5];
        _ambiguity:=_chess_extract_move_tab[6];
        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,_ambiguity)=true):  
                        draw_arrows(_chess_main_lines_color)(_int_to_letter_w(i)&_int_to_str_w(j)&"--"&_int_to_letter_w(_iend)&_int_to_str_w(_jend));
                    fi
                fi
            endfor;
        endfor;
        _step:=max(_step-0.3,0.7);
    endfor;
    _chess_arrow_width:=_chess_arrow_width_save;
enddef;


def draw_white_main_lines(text slist)=
    _draw_main_lines(0)("white")(slist);
enddef;


def draw_black_main_lines(text slist)=
    _draw_main_lines(0)("black")(slist);
enddef;


def draw_white_main_lines_step(expr k)(text slist)=
    _draw_main_lines(k)("white")(slist);
enddef;


def draw_black_main_lines_step(expr k)(text slist)=
    _draw_main_lines(k)("black")(slist);
enddef;

color _circle_move_c;
_circle_move_c:=0.4[green,black];

def set_possible_moves_color(expr c)=
    _circle_move_c:=c;    
enddef;

def _circle_move(expr i,j)=
    path ss;
    if( _chess_view_wb): %white view
    ss:=(fullcircle scaled (0.25*_chessSquareU)) shifted
    ((i-0.5)*_chessSquareU,(j-0.5)*_chessSquareU);
    else: % black view
    ss:=(fullcircle scaled (0.25*_chessSquareU)) shifted
    ((_chessSize-i+0.5)*_chessSquareU,(_chessSize-j+0.5)*_chessSquareU);
    ;
    fi
    fill ss withcolor _circle_move_c;
enddef;

def show_possible_moves(expr s)=
    show_possible_moves_step(0)(s)
enddef;

def show_possible_moves_step(expr k)(expr s)=
    string _first_letter,_first_nbr;
    pair _shiftPiece;
    boolean _meet_piece;
    _first_letter:=substring (0,1) of s;
    _first_nbr:=substring (1,2) of s;
    _fl:=_lettre_to_int(_first_letter);
    _fn:=_str_to_int(_first_nbr);
    _piecetype:=chessboard_table[k][_fl][_fn];
    %color background of square and draw again the piece
    color_square(_circle_move_c)(s);
    if(_chess_view_wb):
        _shiftPiece:=((_fl-1)*_chessSquareU,(_fn-1)*_chessSquareU);
    else:
        _shiftPiece:=((_chessSize-_fl)*_chessSquareU,(_chessSize-_fn)*_chessSquareU);
    fi
    draw ( _int_to_piece(chessboard_table[k][_fl][_fn]) shifted _shiftPiece);
    if(_piecetype=_intBPawn): %         
        if(_fn=7): % 2 steps allowed
            if(chessboard_table[k][_fl][_fn-2]=0): % two step
                _circle_move(_fl,_fn-2);
            fi
        fi
        if(_fn-1>=1):
            if(chessboard_table[k][_fl][_fn-1]=0): % one step
                _circle_move(_fl,_fn-1);
            fi
            if((chessboard_table[k][_fl-1][_fn-1]<>0) and (_fl-1>0)): % capture
                _circle_move(_fl-1,_fn-1);
            fi
            if((chessboard_table[k][_fl+1][_fn-1]<>0) and (_fl+1<=8)): % capture
                _circle_move(_fl+1,_fn-1);
            fi
        fi
    fi
    if(_piecetype=_intWPawn): %         
        if(_fn=2): % 2 steps allowed
            if(chessboard_table[k][_fl][_fn+2]=0): % two step
                _circle_move(_fl,_fn+2);
            fi
        fi
        if(_fn-1>=1):
            if(chessboard_table[k][_fl][_fn+1]=0): % one step
                _circle_move(_fl,_fn+1);
            fi
            if((chessboard_table[k][_fl-1][_fn+1]<>0) and (_fl-1>0)): % capture
                _circle_move(_fl-1,_fn+1);
            fi
            if((chessboard_table[k][_fl+1][_fn+1]<>0) and (_fl+1<=8)): % capture
                _circle_move(_fl+1,_fn+1);
            fi
        fi
    fi
    if((_piecetype=_intBKnight) or (_piecetype=_intWKnight)): % night
        if(_fl-2>=1):
            if(_fn+1<=8):
                if((_piecetype*chessboard_table[k][_fl-2][_fn+1])<=0):
                    _circle_move(_fl-2,_fn+1);
                fi
            fi
            if(_fn-1>=1):
                if((_piecetype*chessboard_table[k][_fl-2][_fn-1])<=0):
                _circle_move(_fl-2,_fn-1);
                fi
            fi
        fi
        if(_fl+2<=8):
            if(_fn+1<=8):
                if((_piecetype*chessboard_table[k][_fl+2][_fn+1])<=0):
                _circle_move(_fl+2,_fn+1);
                fi
            fi
            if(_fn-1>=1):
                if((_piecetype*chessboard_table[k][_fl+2][_fn-1])<=0):
                _circle_move(_fl+2,_fn-1);
                fi
            fi
        fi
        if(_fn-2>=1):
            if(_fl+1<=8):
                if((_piecetype*chessboard_table[k][_fl+1][_fn-2])<=0):
                _circle_move(_fl+1,_fn-2);
                fi
            fi
            if(_fl-1>=1):
                if((_piecetype*chessboard_table[k][_fl-1][_fn-2])<=0):
                _circle_move(_fl-1,_fn-2);
                fi
            fi
        fi
        if(_fn+2<=8):
            if(_fl+1<=8):
                if((_piecetype*chessboard_table[k][_fl+1][_fn+2])<=0):
                _circle_move(_fl+1,_fn+2);
                fi
            fi
            if(_fl-1>=1):
                if((_piecetype*chessboard_table[k][_fl-1][_fn+2])<=0):
                _circle_move(_fl-1,_fn+2);
                fi
            fi
        fi
    fi
    if((_piecetype=_intBBishop) or (_piecetype=_intWBishop)or (_piecetype=_intBQueen) or (_piecetype=_intWQueen)): % bishop
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii+1;
            _jj:=_jj+1;
            exitif((_ii>8) or (_jj>8));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                    _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii-1;
            _jj:=_jj-1;
            exitif((_ii<1) or (_jj<1));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii-1;
            _jj:=_jj+1;
            exitif((_ii<1) or (_jj>8));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii+1;
            _jj:=_jj-1;
            exitif((_ii>8) or (_jj<1));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;
    fi
    if((_piecetype=_intBRook) or (_piecetype=_intWRook)or (_piecetype=_intBQueen) or (_piecetype=_intWQueen)): % rook
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii+1;
            exitif((_ii>8));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                    _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _ii:=_ii-1;
            exitif((_ii<1));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _jj:=_jj+1;
            exitif((_jj>8));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;
        _ii:=_fl;
        _jj:=_fn;
        _meet_piece:=false;
        forever:
            _jj:=_jj-1;
            exitif((_jj<1));
            if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)):
                _circle_move(_ii,_jj);
            fi
            if(chessboard_table[k][_ii][_jj]<>0):
                _meet_piece:=true;
            fi
        endfor;    
    fi
    if((_piecetype=_intBKing) or (_piecetype=_intWKing)): % king
        for _ii:=-1  upto 1:
            for _jj:=-1  upto 1:
                if((_fl+_ii>0) and (_fl+_ii<9) and (_fn+_jj>0) and (_fn+_jj<9)): 
                    if((_piecetype*chessboard_table[k][_fl+_ii][_fn+_jj])<=0):
                        _circle_move(_fl+_ii,_fn+_jj);
                    fi
                fi
            endfor;
        endfor;
    fi
enddef;