| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
% the chessboard |
| 6 |
% chessboard_table[step][1-8][1-8] corresponding to step/A-H/1-8 |
| 7 |
% array 8x8 for each step of the game |
| 8 |
numeric chessboard_table[][][]; |
| 9 |
numeric chessboard_number; |
| 10 |
|
| 11 |
% set the starting board |
| 12 |
def init_chessboard = |
| 13 |
for i:=0 upto 500: |
| 14 |
_chess_wcaptures_nbr[i]:=0; |
| 15 |
_chess_bcaptures_nbr[i]:=0; |
| 16 |
endfor |
| 17 |
if(_chessSize=8): |
| 18 |
for i:=1 upto 8: |
| 19 |
for j:=1 upto 8: |
| 20 |
chessboard_table[0][i][j]:=0; |
| 21 |
endfor; |
| 22 |
endfor; |
| 23 |
for i:=1 upto 8: |
| 24 |
chessboard_table[0][i][2]:=_intWPawn; |
| 25 |
chessboard_table[0][i][7]:=_intBPawn; |
| 26 |
endfor; |
| 27 |
chessboard_table[0][1][1]:=_intWRook;chessboard_table[0][8][1]:=_intWRook; |
| 28 |
chessboard_table[0][1][8]:=_intBRook;chessboard_table[0][8][8]:=_intBRook; |
| 29 |
chessboard_table[0][2][1]:=_intWKnight;chessboard_table[0][7][1]:=_intWKnight; |
| 30 |
chessboard_table[0][2][8]:=_intBKnight;chessboard_table[0][7][8]:=_intBKnight; |
| 31 |
chessboard_table[0][3][1]:=_intWBishop;chessboard_table[0][6][1]:=_intWBishop; |
| 32 |
chessboard_table[0][3][8]:=_intBBishop;chessboard_table[0][6][8]:=_intBBishop; |
| 33 |
chessboard_table[0][4][1]:=_intWQueen; |
| 34 |
chessboard_table[0][4][8]:=_intBQueen; |
| 35 |
chessboard_table[0][5][1]:=_intWKing; |
| 36 |
chessboard_table[0][5][8]:=_intBKing; |
| 37 |
fi |
| 38 |
chessboard_number:=1; |
| 39 |
enddef; |
| 40 |
|
| 41 |
% clear the chessboard |
| 42 |
def clear_chessboard = |
| 43 |
for k:=0 upto 500: |
| 44 |
for i:=0 upto 20: |
| 45 |
for j:=0 upto 20: |
| 46 |
chessboard_table[k][i][j]:=0; |
| 47 |
endfor |
| 48 |
endfor |
| 49 |
endfor |
| 50 |
enddef; |
| 51 |
|
| 52 |
% function to clear an area specified with a arg of type "a1-b6","g1-f2", etc |
| 53 |
def clear_areas(text plist)= |
| 54 |
save _first_letter,_firt_nbr, _second_letter,_second_nbr,_fl,_fn,_sl,_sn,_str; |
| 55 |
string _first_letter,_first_nbr, _second_letter,_second_nbr,_str; |
| 56 |
for _str = plist: |
| 57 |
_first_letter:=substring (0,1) of _str; |
| 58 |
_first_nbr:=substring (1,2) of _str; |
| 59 |
_second_letter:=substring (3,4) of _str; |
| 60 |
_second_nbr:=substring (4,5) of _str; |
| 61 |
_fl:=_lettre_to_int(_first_letter); |
| 62 |
_fn:=_str_to_int(_first_nbr); |
| 63 |
_sl:=_lettre_to_int(_second_letter); |
| 64 |
_sn:=_str_to_int(_second_nbr); |
| 65 |
for i:=_fl upto _sl: |
| 66 |
for j:=_fn upto _sn: |
| 67 |
chessboard_table[0][i][j]:=0; |
| 68 |
endfor |
| 69 |
endfor |
| 70 |
endfor |
| 71 |
enddef; |
| 72 |
|
| 73 |
% function to clear squares with a arg of type "a1-b6","g1-f2", etc |
| 74 |
def clear_squares(text plist)= |
| 75 |
save _first_letter,_firt_nbr,_fl,_fn,_str; |
| 76 |
string _first_letter,_first_nbr,_str; |
| 77 |
for _str = plist: |
| 78 |
_first_letter:=substring (0,1) of _str; |
| 79 |
_first_nbr:=substring (1,2) of _str; |
| 80 |
_fl:=_lettre_to_int(_first_letter); |
| 81 |
_fn:=_str_to_int(_first_nbr); |
| 82 |
chessboard_table[0][_fl][_fn]:=0; |
| 83 |
endfor |
| 84 |
enddef; |
| 85 |
|
| 86 |
% function to clear an area specified with a arg of type "a","c",etc |
| 87 |
def clear_files(text plist)= |
| 88 |
save _letter,_l,_str; |
| 89 |
string _letter,_str; |
| 90 |
for _str = plist: |
| 91 |
_letter:= _str; |
| 92 |
_l:=_lettre_to_int(_letter); |
| 93 |
for j:=1 upto _chessSize: |
| 94 |
chessboard_table[0][_l][j]:=0; |
| 95 |
endfor |
| 96 |
endfor |
| 97 |
enddef; |
| 98 |
|
| 99 |
% function to clear an area specified with a arg of type "1","5",etc |
| 100 |
def clear_ranks(text plist)= |
| 101 |
save _nbr,_n,_str; |
| 102 |
string _nbr,_str; |
| 103 |
for _str = plist: |
| 104 |
_nbr:= _str; |
| 105 |
_n:=_str_to_int(_nbr); |
| 106 |
for i:=1 upto _chessSize: |
| 107 |
chessboard_table[0][i][_n]:=0; |
| 108 |
endfor |
| 109 |
endfor |
| 110 |
enddef; |
| 111 |
|
| 112 |
def _int_to_piece(expr i)= |
| 113 |
if(i=_intWPawn): |
| 114 |
_chess_wP |
| 115 |
elseif(i=_intBPawn): |
| 116 |
_chess_bP |
| 117 |
elseif(i=_intWKnight): |
| 118 |
_chess_wN |
| 119 |
elseif(i=_intBKnight): |
| 120 |
_chess_bN |
| 121 |
elseif(i=_intWBishop): |
| 122 |
_chess_wB |
| 123 |
elseif(i=_intBBishop): |
| 124 |
_chess_bB |
| 125 |
elseif(i=_intWRook): |
| 126 |
_chess_wR |
| 127 |
elseif(i=_intBRook): |
| 128 |
_chess_bR |
| 129 |
elseif(i=_intWQueen): |
| 130 |
_chess_wQ |
| 131 |
elseif(i=_intBQueen): |
| 132 |
_chess_bQ |
| 133 |
elseif(i=_intWKing): |
| 134 |
_chess_wK |
| 135 |
elseif(i=_intBKing): |
| 136 |
_chess_bK |
| 137 |
fi |
| 138 |
enddef; |
| 139 |
|
| 140 |
def _draw_step_chessboard(expr k) = |
| 141 |
for i:=1 upto _chessSize: |
| 142 |
for j:=1 upto _chessSize: |
| 143 |
%show chessboard_table[k][i][j]; |
| 144 |
if(chessboard_table[k][i][j] <> 0): |
| 145 |
draw ( _int_to_piece(chessboard_table[k][i][j]) shifted ((i-1)*_chessSquareU,(j-1)*_chessSquareU)); |
| 146 |
fi |
| 147 |
endfor; |
| 148 |
endfor; |
| 149 |
enddef; |
| 150 |
|
| 151 |
|
| 152 |
vardef _pawn_candidate(expr istart, jstart,iend,jend,wb,capture,ambiguity)= |
| 153 |
% i int from letter, column |
| 154 |
% j int for line |
| 155 |
% return true if (istart,jstart) to (iend,jend) |
| 156 |
% is a possible mvt |
| 157 |
% wb is "white" or "black" |
| 158 |
% capture equal 1 if there is a capture |
| 159 |
save output; |
| 160 |
boolean output; |
| 161 |
output = false; |
| 162 |
if(((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1))): |
| 163 |
if(wb="white"): |
| 164 |
if(jend=4): % on the line that allow 2 square move |
| 165 |
if(((jstart=3) or (jstart=2)) and (iend=istart)): |
| 166 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 167 |
output:=true; |
| 168 |
fi |
| 169 |
fi |
| 170 |
if(((jstart=3) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture |
| 171 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 172 |
output:=true; |
| 173 |
fi |
| 174 |
fi |
| 175 |
else: |
| 176 |
if((jstart=jend-1) and (istart=iend)): % straight move |
| 177 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 178 |
output:=true; |
| 179 |
fi |
| 180 |
fi |
| 181 |
if(((jstart=jend-1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture |
| 182 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 183 |
output:=true; |
| 184 |
fi |
| 185 |
fi |
| 186 |
fi |
| 187 |
else: |
| 188 |
if(jend=5): % on the line that allow 2 square move |
| 189 |
if(((jstart=6) or (jstart=7)) and (iend=istart)): |
| 190 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 191 |
output:=true; |
| 192 |
fi |
| 193 |
fi |
| 194 |
if(((jstart=6) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): %capture |
| 195 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 196 |
output:=true; |
| 197 |
fi |
| 198 |
fi |
| 199 |
else: |
| 200 |
if((jstart=jend+1) and (istart=iend)): % straight move |
| 201 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 202 |
output:=true; |
| 203 |
fi |
| 204 |
fi |
| 205 |
if(((jstart=jend+1) and ((istart=iend+1) or (istart=iend-1))) and (capture=1)): % capture |
| 206 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 207 |
output:=true; |
| 208 |
fi |
| 209 |
fi |
| 210 |
fi |
| 211 |
fi |
| 212 |
fi |
| 213 |
output |
| 214 |
enddef; |
| 215 |
|
| 216 |
vardef _bishop_candidate(expr istart, jstart,iend,jend,wb,ambiguity)= |
| 217 |
% i int from letter, column |
| 218 |
% j int for line |
| 219 |
% return true if (istart,jstart) to (iend,jend) |
| 220 |
% is a possible mvt |
| 221 |
% wb is "white" or "black" |
| 222 |
save output; |
| 223 |
boolean output; |
| 224 |
output = false; |
| 225 |
if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)): |
| 226 |
if(abs(iend-istart)=abs(jend-jstart)): |
| 227 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 228 |
output:=true; |
| 229 |
fi |
| 230 |
fi |
| 231 |
fi |
| 232 |
output |
| 233 |
enddef; |
| 234 |
|
| 235 |
vardef _rook_candidate(expr istart, jstart,iend,jend,wb,ambiguity)= |
| 236 |
% i int from letter, column |
| 237 |
% j int for line |
| 238 |
% return true if (istart,jstart) to (iend,jend) |
| 239 |
% is a possible mvt |
| 240 |
% wb is "white" or "black" |
| 241 |
save output; |
| 242 |
boolean output; |
| 243 |
output = false; |
| 244 |
if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)): |
| 245 |
if((iend=istart) or (jend=jstart)): |
| 246 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 247 |
output:=true; |
| 248 |
fi |
| 249 |
fi |
| 250 |
fi |
| 251 |
output |
| 252 |
enddef; |
| 253 |
|
| 254 |
|
| 255 |
vardef _queen_candidate(expr istart, jstart,iend,jend,wb)= |
| 256 |
% i int from letter, column |
| 257 |
% j int for line |
| 258 |
% return true if (istart,jstart) to (iend,jend) |
| 259 |
% is a possible mvt |
| 260 |
% wb is "white" or "black" |
| 261 |
save output; |
| 262 |
boolean output; |
| 263 |
output = false; |
| 264 |
if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)): |
| 265 |
if((iend=istart) or (jend=jstart) or (abs(iend-istart)=abs(jend-jstart))): |
| 266 |
output:=true; |
| 267 |
fi |
| 268 |
fi |
| 269 |
output |
| 270 |
enddef; |
| 271 |
|
| 272 |
vardef _king_candidate(expr istart, jstart,iend,jend,wb)= |
| 273 |
% i int from letter, column |
| 274 |
% j int for line |
| 275 |
% return true if (istart,jstart) to (iend,jend) |
| 276 |
% is a possible mvt |
| 277 |
% wb is "white" or "black" |
| 278 |
save output; |
| 279 |
boolean output; |
| 280 |
output = false; |
| 281 |
if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)): |
| 282 |
if((abs(iend-istart)<=1) and (abs(jend-jstart)<=1)): |
| 283 |
output:=true; |
| 284 |
fi |
| 285 |
fi |
| 286 |
output |
| 287 |
enddef; |
| 288 |
|
| 289 |
vardef _knight_candidate(expr istart, jstart,iend,jend,wb,ambiguity)= |
| 290 |
% i int from letter, column |
| 291 |
% j int for line |
| 292 |
% return true if (istart,jstart) to (iend,jend) |
| 293 |
% is a possible mvt |
| 294 |
% wb is "white" or "black" |
| 295 |
save output; |
| 296 |
boolean output; |
| 297 |
output = false; |
| 298 |
if((iend<=_chessSize) and (iend>=1) and (iend<=_chessSize) and (iend>=1)): |
| 299 |
if(((abs(iend-istart)=2) and (abs(jend-jstart)=1)) or |
| 300 |
((abs(iend-istart)=1) and (abs(jend-jstart)=2))): |
| 301 |
if((ambiguity=0) or ((ambiguity<>0) and (ambiguity=istart))): |
| 302 |
output:=true; |
| 303 |
fi |
| 304 |
fi |
| 305 |
fi |
| 306 |
output |
| 307 |
enddef; |
| 308 |
|
| 309 |
vardef _chess_candidate(expr type, istart, jstart, iend, jend,wb,capture,ambiguity)= |
| 310 |
numeric _pm; |
| 311 |
_pm:=1; |
| 312 |
if(wb="black"): |
| 313 |
_pm:=-1; |
| 314 |
fi |
| 315 |
if(type=(_pm*_intWPawn)): |
| 316 |
_pawn_candidate(istart,jstart,iend,jend,wb,capture,ambiguity) |
| 317 |
elseif(type=_pm*_intWBishop): |
| 318 |
_bishop_candidate(istart,jstart,iend,jend,wb,ambiguity) |
| 319 |
elseif(type=_pm*_intWKing): |
| 320 |
_king_candidate(istart,jstart,iend,jend,wb) |
| 321 |
elseif(type=_pm*_intWKnight): |
| 322 |
_knight_candidate(istart,jstart,iend,jend,wb,ambiguity) |
| 323 |
elseif(type=_pm*_intWQueen): |
| 324 |
_queen_candidate(istart,jstart,iend,jend,wb) |
| 325 |
elseif(type=_pm*_intWRook): |
| 326 |
_rook_candidate(istart,jstart,iend,jend,wb,ambiguity) |
| 327 |
fi |
| 328 |
enddef; |
| 329 |
|
| 330 |
def _chess_copy_chessboard(expr step)= |
| 331 |
save i,j; |
| 332 |
for i:=1 upto _chessSize: |
| 333 |
for j:=1 upto _chessSize: |
| 334 |
chessboard_table[step][i][j]:=chessboard_table[step-1][i][j]; |
| 335 |
endfor; |
| 336 |
endfor; |
| 337 |
enddef; |
| 338 |
|
| 339 |
def _chess_copy_captures(expr step)= |
| 340 |
save i,j; |
| 341 |
%show _chess_wcaptures_nbr[step-1]; |
| 342 |
if(_chess_wcaptures_nbr[step-1]>0): |
| 343 |
for i:=0 upto _chess_wcaptures_nbr[step-1]-1: |
| 344 |
_chess_white_captures[step][i]:=_chess_white_captures[step-1][i]; |
| 345 |
endfor; |
| 346 |
fi |
| 347 |
%show _chess_bcaptures_nbr[step-1]; |
| 348 |
if(_chess_bcaptures_nbr[step-1]>0): |
| 349 |
for i:=0 upto _chess_bcaptures_nbr[step-1]-1: |
| 350 |
_chess_black_captures[step][i]:=_chess_black_captures[step-1][i]; |
| 351 |
endfor; |
| 352 |
fi |
| 353 |
enddef; |
| 354 |
|
| 355 |
numeric _chess_white_captures[][],_chess_black_captures[][]; |
| 356 |
numeric _chess_wcaptures_nbr[], _chess_bcaptures_nbr[]; |
| 357 |
_chess_wcaptures_nbr[0]:=0; |
| 358 |
_chess_bcaptures_nbr[0]:=0; |
| 359 |
|
| 360 |
def _build_all_chessboards= |
| 361 |
save _wb; |
| 362 |
string _wb; |
| 363 |
init_chessboard; % k=0 |
| 364 |
for k:=1 upto (_chess_step_numtable): |
| 365 |
_chess_copy_chessboard(k); |
| 366 |
_chess_copy_captures(k); |
| 367 |
_piecetype:=_chess_moves_table_numeric[k-1][0]; |
| 368 |
_pm:=_chess_moves_table_numeric[k-1][1]; |
| 369 |
_iend:=_chess_moves_table_numeric[k-1][2]; |
| 370 |
_jend:=_chess_moves_table_numeric[k-1][3]; |
| 371 |
_capture:=_chess_moves_table_numeric[k-1][4]; |
| 372 |
_promotion:=_chess_moves_table_numeric[k-1][5]; |
| 373 |
_ambiguity:=_chess_moves_table_numeric[k-1][6]; |
| 374 |
if(_pm=1): |
| 375 |
_wb:="white"; |
| 376 |
else: |
| 377 |
_wb:="black"; |
| 378 |
fi |
| 379 |
% castling |
| 380 |
if(_piecetype=100): % short castling |
| 381 |
if(_pm=1): |
| 382 |
_tmp_line:=1; |
| 383 |
else: |
| 384 |
_tmp_line:=8; |
| 385 |
fi |
| 386 |
if((chessboard_table[k][5][_tmp_line]=_pm*_intWKing) and |
| 387 |
(chessboard_table[k][8][_tmp_line]=_pm*_intWRook)): % if king and |
| 388 |
% in the right places |
| 389 |
chessboard_table[k][5][_tmp_line]:=0; |
| 390 |
chessboard_table[k][8][_tmp_line]:=0; |
| 391 |
chessboard_table[k][6][_tmp_line]:=_pm*_intWRook; |
| 392 |
chessboard_table[k][7][_tmp_line]:=_pm*_intWKing; |
| 393 |
fi |
| 394 |
fi |
| 395 |
if(_piecetype=1000): % long castling |
| 396 |
if(_pm=1): |
| 397 |
_tmp_line:=1; |
| 398 |
else: |
| 399 |
_tmp_line:=8; |
| 400 |
fi |
| 401 |
if((chessboard_table[k][5][_tmp_line]=_pm*_intWKing) and |
| 402 |
(chessboard_table[k][1][_tmp_line]=_pm*_intWRook)): % if king and |
| 403 |
% in the right places |
| 404 |
chessboard_table[k][5][_tmp_line]:=0; |
| 405 |
chessboard_table[k][1][_tmp_line]:=0; |
| 406 |
chessboard_table[k][4][_tmp_line]:=_pm*_intWRook; |
| 407 |
chessboard_table[k][3][_tmp_line]:=_pm*_intWKing; |
| 408 |
fi |
| 409 |
fi |
| 410 |
for i:=1 upto _chessSize: |
| 411 |
for j:=1 upto _chessSize: |
| 412 |
if(chessboard_table[k][i][j]=_piecetype): |
| 413 |
if(_chess_candidate(_piecetype, i, j, _iend, _jend,_wb,_capture,_ambiguity)=true): |
| 414 |
if(_capture<>0): |
| 415 |
if(_pm=1): |
| 416 |
_chess_white_captures[k][_chess_wcaptures_nbr[k-1]]:=chessboard_table[k][i][j]; |
| 417 |
_chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1]+1; |
| 418 |
_chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1]; |
| 419 |
else: |
| 420 |
_chess_black_captures[k][_chess_bcaptures_nbr[k-1]]:=chessboard_table[k][i][j]; |
| 421 |
_chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1]+1; |
| 422 |
_chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1]; |
| 423 |
fi |
| 424 |
else: |
| 425 |
_chess_wcaptures_nbr[k]:=_chess_wcaptures_nbr[k-1]; |
| 426 |
_chess_bcaptures_nbr[k]:=_chess_bcaptures_nbr[k-1]; |
| 427 |
fi |
| 428 |
|
| 429 |
chessboard_table[k][i][j]:=0; %erase piece |
| 430 |
if(_promotion<>0): |
| 431 |
chessboard_table[k][_iend][_jend]:=_promotion; |
| 432 |
else: |
| 433 |
chessboard_table[k][_iend][_jend]:=_piecetype; |
| 434 |
fi |
| 435 |
fi |
| 436 |
fi |
| 437 |
endfor; |
| 438 |
endfor; |
| 439 |
endfor; |
| 440 |
chessboard_number:=_chess_step_numtable; |
| 441 |
enddef; |
| 442 |
|
| 443 |
|
| 444 |
% set the starting empty board |
| 445 |
def set_empty_chessboard = |
| 446 |
for i:=0 upto 500: |
| 447 |
_chess_wcaptures_nbr[i]:=0; |
| 448 |
_chess_bcaptures_nbr[i]:=0; |
| 449 |
endfor |
| 450 |
for i:=1 upto _chessSize: |
| 451 |
for j:=1 upto _chessSize: |
| 452 |
chessboard_table[0][i][j]:=0; |
| 453 |
endfor; |
| 454 |
endfor; |
| 455 |
chessboard_number:=1; |
| 456 |
enddef; |
| 457 |
|
| 458 |
|
| 459 |
def add_white_pieces(text plists) = |
| 460 |
save _str,_piecetype, _letter, _nbr,wb; |
| 461 |
string wb; |
| 462 |
wb:="white"; |
| 463 |
for _str = plists: |
| 464 |
if(_is_cap(_get_char(_str,0))): % if the first char is a cap |
| 465 |
_piecetype:=_cap_to_int(_get_char(_str,0),wb); |
| 466 |
if(length(_str)=3): % type Nf6 |
| 467 |
_letter:=_lettre_to_int(_get_char(_str,1)); |
| 468 |
_nbr:=_str_to_int(_get_char(_str,2)); |
| 469 |
fi |
| 470 |
else: |
| 471 |
_piecetype:=_intWPawn; |
| 472 |
if(length(_str)=2): % type f6 |
| 473 |
_letter:=_lettre_to_int(_get_char(_str,0)); |
| 474 |
_nbr:=_str_to_int(_get_char(_str,1)); |
| 475 |
fi |
| 476 |
fi |
| 477 |
chessboard_table[0][_letter][_nbr]:=_piecetype; |
| 478 |
endfor |
| 479 |
enddef; |
| 480 |
|
| 481 |
def add_black_pieces(text plists) = |
| 482 |
save _str,_piecetype, _letter, _nbr,wb; |
| 483 |
string wb; |
| 484 |
wb:="black"; |
| 485 |
for _str = plists: |
| 486 |
if(_is_cap(_get_char(_str,0))): % if the first char is a cap |
| 487 |
_piecetype:=_cap_to_int(_get_char(_str,0),wb); |
| 488 |
if(length(_str)=3): % type Nf6 |
| 489 |
_letter:=_lettre_to_int(_get_char(_str,1)); |
| 490 |
_nbr:=_str_to_int(_get_char(_str,2)); |
| 491 |
fi |
| 492 |
else: |
| 493 |
_piecetype:=_intBPawn; |
| 494 |
if(length(_str)=2): % type f6 |
| 495 |
_letter:=_lettre_to_int(_get_char(_str,0)); |
| 496 |
_nbr:=_str_to_int(_get_char(_str,1)); |
| 497 |
fi |
| 498 |
fi |
| 499 |
chessboard_table[0][_letter][_nbr]:=_piecetype; |
| 500 |
endfor |
| 501 |
enddef; |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
def _draw_main_lines(expr _k)(expr wb)(text slist)= |
| 507 |
save _str; |
| 508 |
_chess_arrow_width_save:=_chess_arrow_width; |
| 509 |
_step:=1.4; |
| 510 |
for _str = slist: |
| 511 |
_chess_arrow_width:=(0.1*_step)*_chessSquareU; |
| 512 |
_extract_single_move(_str,wb); |
| 513 |
_piecetype:=_chess_extract_move_tab[0]; |
| 514 |
_pm:=_chess_extract_move_tab[1]; |
| 515 |
_iend:=_chess_extract_move_tab[2]; |
| 516 |
_jend:=_chess_extract_move_tab[3]; |
| 517 |
_capture:=_chess_extract_move_tab[4]; |
| 518 |
_promotion:=_chess_extract_move_tab[5]; |
| 519 |
_ambiguity:=_chess_extract_move_tab[6]; |
| 520 |
for i:=1 upto _chessSize: |
| 521 |
for j:=1 upto _chessSize: |
| 522 |
if(chessboard_table[_k][i][j]=_piecetype): |
| 523 |
if(_chess_candidate(_piecetype, i, j, _iend, _jend,wb,_capture,_ambiguity)=true): |
| 524 |
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)); |
| 525 |
fi |
| 526 |
fi |
| 527 |
endfor; |
| 528 |
endfor; |
| 529 |
_step:=max(_step-0.3,0.7); |
| 530 |
endfor; |
| 531 |
_chess_arrow_width:=_chess_arrow_width_save; |
| 532 |
enddef; |
| 533 |
|
| 534 |
|
| 535 |
def draw_white_main_lines(text slist)= |
| 536 |
_draw_main_lines(0)("white")(slist); |
| 537 |
enddef; |
| 538 |
|
| 539 |
|
| 540 |
def draw_black_main_lines(text slist)= |
| 541 |
_draw_main_lines(0)("black")(slist); |
| 542 |
enddef; |
| 543 |
|
| 544 |
|
| 545 |
def draw_white_main_lines_step(expr k)(text slist)= |
| 546 |
_draw_main_lines(k)("white")(slist); |
| 547 |
enddef; |
| 548 |
|
| 549 |
|
| 550 |
def draw_black_main_lines_step(expr k)(text slist)= |
| 551 |
_draw_main_lines(k)("black")(slist); |
| 552 |
enddef; |
| 553 |
|
| 554 |
color _circle_move_c; |
| 555 |
_circle_move_c:=0.4[green,black]; |
| 556 |
|
| 557 |
def set_possible_moves_color(expr c)= |
| 558 |
_circle_move_c:=c; |
| 559 |
enddef; |
| 560 |
|
| 561 |
def _circle_move(expr i,j)= |
| 562 |
path ss; |
| 563 |
if( _chess_view_wb): %white view |
| 564 |
ss:=(fullcircle scaled (0.25*_chessSquareU)) shifted |
| 565 |
((i-0.5)*_chessSquareU,(j-0.5)*_chessSquareU); |
| 566 |
else: % black view |
| 567 |
ss:=(fullcircle scaled (0.25*_chessSquareU)) shifted |
| 568 |
((_chessSize-i+0.5)*_chessSquareU,(_chessSize-j+0.5)*_chessSquareU); |
| 569 |
; |
| 570 |
fi |
| 571 |
fill ss withcolor _circle_move_c; |
| 572 |
enddef; |
| 573 |
|
| 574 |
def show_possible_moves(expr s)= |
| 575 |
show_possible_moves_step(0)(s) |
| 576 |
enddef; |
| 577 |
|
| 578 |
def show_possible_moves_step(expr k)(expr s)= |
| 579 |
string _first_letter,_first_nbr; |
| 580 |
pair _shiftPiece; |
| 581 |
boolean _meet_piece; |
| 582 |
_first_letter:=substring (0,1) of s; |
| 583 |
_first_nbr:=substring (1,2) of s; |
| 584 |
_fl:=_lettre_to_int(_first_letter); |
| 585 |
_fn:=_str_to_int(_first_nbr); |
| 586 |
_piecetype:=chessboard_table[k][_fl][_fn]; |
| 587 |
%color background of square and draw again the piece |
| 588 |
color_square(_circle_move_c)(s); |
| 589 |
if(_chess_view_wb): |
| 590 |
_shiftPiece:=((_fl-1)*_chessSquareU,(_fn-1)*_chessSquareU); |
| 591 |
else: |
| 592 |
_shiftPiece:=((_chessSize-_fl)*_chessSquareU,(_chessSize-_fn)*_chessSquareU); |
| 593 |
fi |
| 594 |
draw ( _int_to_piece(chessboard_table[k][_fl][_fn]) shifted _shiftPiece); |
| 595 |
if(_piecetype=_intBPawn): % |
| 596 |
if(_fn=7): % 2 steps allowed |
| 597 |
if(chessboard_table[k][_fl][_fn-2]=0): % two step |
| 598 |
_circle_move(_fl,_fn-2); |
| 599 |
fi |
| 600 |
fi |
| 601 |
if(_fn-1>=1): |
| 602 |
if(chessboard_table[k][_fl][_fn-1]=0): % one step |
| 603 |
_circle_move(_fl,_fn-1); |
| 604 |
fi |
| 605 |
if((chessboard_table[k][_fl-1][_fn-1]<>0) and (_fl-1>0)): % capture |
| 606 |
_circle_move(_fl-1,_fn-1); |
| 607 |
fi |
| 608 |
if((chessboard_table[k][_fl+1][_fn-1]<>0) and (_fl+1<=8)): % capture |
| 609 |
_circle_move(_fl+1,_fn-1); |
| 610 |
fi |
| 611 |
fi |
| 612 |
fi |
| 613 |
if(_piecetype=_intWPawn): % |
| 614 |
if(_fn=2): % 2 steps allowed |
| 615 |
if(chessboard_table[k][_fl][_fn+2]=0): % two step |
| 616 |
_circle_move(_fl,_fn+2); |
| 617 |
fi |
| 618 |
fi |
| 619 |
if(_fn-1>=1): |
| 620 |
if(chessboard_table[k][_fl][_fn+1]=0): % one step |
| 621 |
_circle_move(_fl,_fn+1); |
| 622 |
fi |
| 623 |
if((chessboard_table[k][_fl-1][_fn+1]<>0) and (_fl-1>0)): % capture |
| 624 |
_circle_move(_fl-1,_fn+1); |
| 625 |
fi |
| 626 |
if((chessboard_table[k][_fl+1][_fn+1]<>0) and (_fl+1<=8)): % capture |
| 627 |
_circle_move(_fl+1,_fn+1); |
| 628 |
fi |
| 629 |
fi |
| 630 |
fi |
| 631 |
if((_piecetype=_intBKnight) or (_piecetype=_intWKnight)): % night |
| 632 |
if(_fl-2>=1): |
| 633 |
if(_fn+1<=8): |
| 634 |
if((_piecetype*chessboard_table[k][_fl-2][_fn+1])<=0): |
| 635 |
_circle_move(_fl-2,_fn+1); |
| 636 |
fi |
| 637 |
fi |
| 638 |
if(_fn-1>=1): |
| 639 |
if((_piecetype*chessboard_table[k][_fl-2][_fn-1])<=0): |
| 640 |
_circle_move(_fl-2,_fn-1); |
| 641 |
fi |
| 642 |
fi |
| 643 |
fi |
| 644 |
if(_fl+2<=8): |
| 645 |
if(_fn+1<=8): |
| 646 |
if((_piecetype*chessboard_table[k][_fl+2][_fn+1])<=0): |
| 647 |
_circle_move(_fl+2,_fn+1); |
| 648 |
fi |
| 649 |
fi |
| 650 |
if(_fn-1>=1): |
| 651 |
if((_piecetype*chessboard_table[k][_fl+2][_fn-1])<=0): |
| 652 |
_circle_move(_fl+2,_fn-1); |
| 653 |
fi |
| 654 |
fi |
| 655 |
fi |
| 656 |
if(_fn-2>=1): |
| 657 |
if(_fl+1<=8): |
| 658 |
if((_piecetype*chessboard_table[k][_fl+1][_fn-2])<=0): |
| 659 |
_circle_move(_fl+1,_fn-2); |
| 660 |
fi |
| 661 |
fi |
| 662 |
if(_fl-1>=1): |
| 663 |
if((_piecetype*chessboard_table[k][_fl-1][_fn-2])<=0): |
| 664 |
_circle_move(_fl-1,_fn-2); |
| 665 |
fi |
| 666 |
fi |
| 667 |
fi |
| 668 |
if(_fn+2<=8): |
| 669 |
if(_fl+1<=8): |
| 670 |
if((_piecetype*chessboard_table[k][_fl+1][_fn+2])<=0): |
| 671 |
_circle_move(_fl+1,_fn+2); |
| 672 |
fi |
| 673 |
fi |
| 674 |
if(_fl-1>=1): |
| 675 |
if((_piecetype*chessboard_table[k][_fl-1][_fn+2])<=0): |
| 676 |
_circle_move(_fl-1,_fn+2); |
| 677 |
fi |
| 678 |
fi |
| 679 |
fi |
| 680 |
fi |
| 681 |
if((_piecetype=_intBBishop) or (_piecetype=_intWBishop)or (_piecetype=_intBQueen) or (_piecetype=_intWQueen)): % bishop |
| 682 |
_ii:=_fl; |
| 683 |
_jj:=_fn; |
| 684 |
_meet_piece:=false; |
| 685 |
forever: |
| 686 |
_ii:=_ii+1; |
| 687 |
_jj:=_jj+1; |
| 688 |
exitif((_ii>8) or (_jj>8)); |
| 689 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 690 |
_circle_move(_ii,_jj); |
| 691 |
fi |
| 692 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 693 |
_meet_piece:=true; |
| 694 |
fi |
| 695 |
endfor; |
| 696 |
_ii:=_fl; |
| 697 |
_jj:=_fn; |
| 698 |
_meet_piece:=false; |
| 699 |
forever: |
| 700 |
_ii:=_ii-1; |
| 701 |
_jj:=_jj-1; |
| 702 |
exitif((_ii<1) or (_jj<1)); |
| 703 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 704 |
_circle_move(_ii,_jj); |
| 705 |
fi |
| 706 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 707 |
_meet_piece:=true; |
| 708 |
fi |
| 709 |
endfor; |
| 710 |
_ii:=_fl; |
| 711 |
_jj:=_fn; |
| 712 |
_meet_piece:=false; |
| 713 |
forever: |
| 714 |
_ii:=_ii-1; |
| 715 |
_jj:=_jj+1; |
| 716 |
exitif((_ii<1) or (_jj>8)); |
| 717 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 718 |
_circle_move(_ii,_jj); |
| 719 |
fi |
| 720 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 721 |
_meet_piece:=true; |
| 722 |
fi |
| 723 |
endfor; |
| 724 |
_ii:=_fl; |
| 725 |
_jj:=_fn; |
| 726 |
_meet_piece:=false; |
| 727 |
forever: |
| 728 |
_ii:=_ii+1; |
| 729 |
_jj:=_jj-1; |
| 730 |
exitif((_ii>8) or (_jj<1)); |
| 731 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 732 |
_circle_move(_ii,_jj); |
| 733 |
fi |
| 734 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 735 |
_meet_piece:=true; |
| 736 |
fi |
| 737 |
endfor; |
| 738 |
fi |
| 739 |
if((_piecetype=_intBRook) or (_piecetype=_intWRook)or (_piecetype=_intBQueen) or (_piecetype=_intWQueen)): % rook |
| 740 |
_ii:=_fl; |
| 741 |
_jj:=_fn; |
| 742 |
_meet_piece:=false; |
| 743 |
forever: |
| 744 |
_ii:=_ii+1; |
| 745 |
exitif((_ii>8)); |
| 746 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 747 |
_circle_move(_ii,_jj); |
| 748 |
fi |
| 749 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 750 |
_meet_piece:=true; |
| 751 |
fi |
| 752 |
endfor; |
| 753 |
_ii:=_fl; |
| 754 |
_jj:=_fn; |
| 755 |
_meet_piece:=false; |
| 756 |
forever: |
| 757 |
_ii:=_ii-1; |
| 758 |
exitif((_ii<1)); |
| 759 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 760 |
_circle_move(_ii,_jj); |
| 761 |
fi |
| 762 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 763 |
_meet_piece:=true; |
| 764 |
fi |
| 765 |
endfor; |
| 766 |
_ii:=_fl; |
| 767 |
_jj:=_fn; |
| 768 |
_meet_piece:=false; |
| 769 |
forever: |
| 770 |
_jj:=_jj+1; |
| 771 |
exitif((_jj>8)); |
| 772 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 773 |
_circle_move(_ii,_jj); |
| 774 |
fi |
| 775 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 776 |
_meet_piece:=true; |
| 777 |
fi |
| 778 |
endfor; |
| 779 |
_ii:=_fl; |
| 780 |
_jj:=_fn; |
| 781 |
_meet_piece:=false; |
| 782 |
forever: |
| 783 |
_jj:=_jj-1; |
| 784 |
exitif((_jj<1)); |
| 785 |
if(((_piecetype*chessboard_table[k][_ii][_jj])<=0) and (_meet_piece=false)): |
| 786 |
_circle_move(_ii,_jj); |
| 787 |
fi |
| 788 |
if(chessboard_table[k][_ii][_jj]<>0): |
| 789 |
_meet_piece:=true; |
| 790 |
fi |
| 791 |
endfor; |
| 792 |
fi |
| 793 |
if((_piecetype=_intBKing) or (_piecetype=_intWKing)): % king |
| 794 |
for _ii:=-1 upto 1: |
| 795 |
for _jj:=-1 upto 1: |
| 796 |
if((_fl+_ii>0) and (_fl+_ii<9) and (_fn+_jj>0) and (_fn+_jj<9)): |
| 797 |
if((_piecetype*chessboard_table[k][_fl+_ii][_fn+_jj])<=0): |
| 798 |
_circle_move(_fl+_ii,_fn+_jj); |
| 799 |
fi |
| 800 |
fi |
| 801 |
endfor; |
| 802 |
endfor; |
| 803 |
fi |
| 804 |
enddef; |