-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves-to-fen.pl
75 lines (73 loc) · 2.16 KB
/
moves-to-fen.pl
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
#!perl -w
use Chess::Rep;
use Getopt::Long;
GetOptions('fen'=>\$fen,
'list'=>\$dolist,
'moves'=>\$moves,
'dump'=>\$dodump,
'status'=>\$dostatus,
'fifty'=>\$dofifty,
'san'=>\$dosan,
);
$list='';
$san='';
$pos=Chess::Rep->new;
for(@ARGV){
die unless /^\S+$/;
my$details;
die unless defined($details=$pos->go_move($_));
my$construct=$details->{from}.$details->{to};
if(defined$details->{promote}){
$construct.=$details->{promote};
}
$list.=" ".lc$construct;
$san.=" ".$$details{san};
}
if($fen){
$_=$pos->get_fen;
s/\s+\d+$//; #discard move count
s/\s+\d+$//; #discard halfmove count for 50-move draw
s/\s+\S+$//; #discard en passant square
# this is somewhat of a hack to work around Chess::Rep and Stockfish disagreeing on en passant square in FEN
s,/,.,g;
s/ /_/g;
print "fen $_\n";
}
print "list",$list,"\n" if $dolist;
print "san",$san,"\n" if $dosan;
if($moves){
print"moves";
$status=$pos->status;
$moves=$status->{moves};
for(@$moves){
$base=lc(Chess::Rep::get_field_id($$_{from}).Chess::Rep::get_field_id($$_{to}));
$finished=0;
if($$_{piece}&1){ #pawn
@rc=Chess::Rep::get_row_col($$_{to});
die unless @rc==2;
die unless defined($rank=$rc[0]);
if((Chess::Rep::piece_color($$_{piece})and$rank==7)or
(!Chess::Rep::piece_color($$_{piece})and$rank==0)){
for(qw(q r n b)){
print " ",$base,$_;
}
$finished=1;
}}
print" ",$base unless $finished;
}
print"\n";
}
print $pos->dump_pos,"\n" if $dodump;
if($dostatus){
print "mate\n" if $pos->status->{mate};
print "stalemate\n" if $pos->status->{stalemate};
#perl moves-to-fen.pl --status c4 h5 h4 a5 Qa4 Ra6 Qxa5 Rah6 Qxc7 f6 Qxd7+ Kf7 Qxb7 Qd3 Qxb8 Qh7 Qxc8 Kg6 Qe6
}
if($dofifty){
$_=$pos->get_fen;
my($rle,$color,$castle,$enpassant,$fifty,$movecount)=split;
die unless $color eq 'w' or $color='b';
die unless $fifty =~ /^\d+$/;
die unless $movecount =~ /^\d+$/;
print "fifty $fifty\n";
}