| 1 |
if(unknown _chess_input_pgn): |
| 2 |
input mpchess-pgn |
| 3 |
fi |
| 4 |
|
| 5 |
def _chess_fen_is_number(expr c)= |
| 6 |
if((c="1") or (c="2") or (c="3") or (c="4") or (c="5") or (c="6") or (c="7") or (c="8")): |
| 7 |
true |
| 8 |
else: |
| 9 |
false |
| 10 |
fi |
| 11 |
enddef; |
| 12 |
|
| 13 |
def _chess_is_slash(expr c)= |
| 14 |
if(c="/"): |
| 15 |
true |
| 16 |
else: |
| 17 |
false |
| 18 |
fi |
| 19 |
enddef; |
| 20 |
|
| 21 |
def _chess_fen_letter_to_int(expr c)= |
| 22 |
if(c="B"): |
| 23 |
_intWBishop |
| 24 |
elseif(c="K"): |
| 25 |
_intWKing |
| 26 |
elseif(c="N"): |
| 27 |
_intWKnight |
| 28 |
elseif(c="Q"): |
| 29 |
_intWQueen |
| 30 |
elseif(c="R"): |
| 31 |
_intWRook |
| 32 |
elseif(c="P"): |
| 33 |
_intWPawn |
| 34 |
elseif(c="b"): |
| 35 |
_intBBishop |
| 36 |
elseif(c="k"): |
| 37 |
_intBKing |
| 38 |
elseif(c="n"): |
| 39 |
_intBKnight |
| 40 |
elseif(c="q"): |
| 41 |
_intBQueen |
| 42 |
elseif(c="r"): |
| 43 |
_intBRook |
| 44 |
elseif(c="p"): |
| 45 |
_intBPawn |
| 46 |
fi |
| 47 |
enddef; |
| 48 |
|
| 49 |
def _chess_extract_position(expr fen)= |
| 50 |
save i, j, _str,_current_char; |
| 51 |
save _len,_zero_nbr,_exit_for; |
| 52 |
boolean _exit_for; |
| 53 |
_exit_for:=false; |
| 54 |
string _str,_current_char; |
| 55 |
_str:=fen; |
| 56 |
i:=1; |
| 57 |
j:=8; |
| 58 |
_len:=length(_str); |
| 59 |
forever:exitunless(not _exit_for); |
| 60 |
_len:=length(_str); |
| 61 |
_current_char:=_get_char(_str,0); |
| 62 |
_str:=substring (1,_len) of _str; |
| 63 |
if(_chess_fen_is_number(_current_char)): % if a number |
| 64 |
_zero_nbr:=_str_to_int(_current_char); |
| 65 |
for k:=0 upto _zero_nbr-1: |
| 66 |
chessboard_table[0][i+k][j]:=0; |
| 67 |
endfor |
| 68 |
i:=i+_zero_nbr; |
| 69 |
elseif(_chess_is_slash(_current_char)): % if char / |
| 70 |
j:=j-1; % change line |
| 71 |
i:=1; % reset column |
| 72 |
elseif(_current_char=" "): |
| 73 |
% if " " |
| 74 |
_exit_for:=true; |
| 75 |
if(_get_char(_str,0)="w"): |
| 76 |
_white_to_move:=true; |
| 77 |
else: |
| 78 |
_white_to_move:=false; |
| 79 |
fi |
| 80 |
else: % a letter |
| 81 |
chessboard_table[0][i][j]:=_chess_fen_letter_to_int(_current_char); |
| 82 |
i:=i+1; |
| 83 |
fi |
| 84 |
endfor |
| 85 |
enddef; |
| 86 |
|
| 87 |
def build_chessboard_from_fen(expr s)= |
| 88 |
set_empty_chessboard; |
| 89 |
_chess_extract_position(s); |
| 90 |
enddef; |
| 91 |
|
| 92 |
def build_chessboard_from_fen_file(expr s)= |
| 93 |
set_empty_chessboard; |
| 94 |
string _rf; |
| 95 |
_rf:=readfrom s; |
| 96 |
_chess_extract_position(_rf); |
| 97 |
enddef; |
| 98 |
|