3 # There are too many chess modules on CPAN already, so here's another one...
12 # Takes in a FICS style 12-type position.
14 my ($class, $str) = @_;
16 my (@x) = split / /, $str;
18 $pos->{'board'} = Board->new(@x[1..8]);
19 $pos->{'toplay'} = $x[9];
20 $pos->{'ep_file_num'} = $x[10];
21 $pos->{'white_castle_k'} = $x[11];
22 $pos->{'white_castle_q'} = $x[12];
23 $pos->{'black_castle_k'} = $x[13];
24 $pos->{'black_castle_q'} = $x[14];
25 $pos->{'time_to_100move_rule'} = $x[15];
26 $pos->{'player_w'} = $x[17];
27 $pos->{'player_b'} = $x[18];
28 $pos->{'player_w'} =~ s/^[IG]M//;
29 $pos->{'player_b'} =~ s/^[IG]M//;
30 $pos->{'move_num'} = $x[26];
31 if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
32 $pos->{'last_move_uci'} = $1 . $2;
34 $pos->{'last_move_uci'} = undef;
36 $pos->{'last_move'} = $x[29];
46 my $fen = $pos->{'board'}->fen();
50 $fen .= lc($pos->{'toplay'});
54 $castling .= "K" if ($pos->{'white_castle_k'} == 1);
55 $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
56 $castling .= "k" if ($pos->{'black_castle_k'} == 1);
57 $castling .= "q" if ($pos->{'black_castle_q'} == 1);
58 $castling = "-" if ($castling eq "");
59 # $castling = "-"; # chess960
65 if ($pos->{'ep_file_num'} != -1) {
66 my $col = $pos->{'ep_file_num'};
67 my $nep = (qw(a b c d e f g h))[$col];
69 if ($pos->{'toplay'} eq 'B') {
76 # Showing the en passant square when actually no capture can be made
77 # seems to confuse at least Rybka. Thus, check if there's actually
78 # a pawn of the opposite side that can do the en passant move, and if
79 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
80 # "right" thing as per the standard, though.
82 if ($pos->{'toplay'} eq 'B') {
83 $ep = $nep if ($col > 0 && $pos->{'board'}[4][$col-1] eq 'p');
84 $ep = $nep if ($col < 7 && $pos->{'board'}[4][$col+1] eq 'p');
86 $ep = $nep if ($col > 0 && $pos->{'board'}[3][$col-1] eq 'P');
87 $ep = $nep if ($col < 7 && $pos->{'board'}[3][$col+1] eq 'P');
95 $fen .= $pos->{'time_to_100move_rule'};
99 $fen .= $pos->{'move_num'};
106 return { %$pos, board => undef, fen => $pos->fen() };