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_since_100move_rule_reset'} = $x[15];
26 $pos->{'player_w'} = $x[17];
27 $pos->{'player_b'} = $x[18];
28 $pos->{'player_w'} =~ s/^W?[FCIG]M//;
29 $pos->{'player_b'} =~ s/^W?[FCIG]M//;
30 $pos->{'move_num'} = $x[26];
31 $pos->{'last_move'} = $x[29];
38 my ($class, $white, $black) = @_;
39 return $class->new("<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 dummygamenum $white $black -2 dummytime dummyincrement 39 39 dummytime dummytime 1 none (0:00) none 0 0 0");
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_since_100move_rule_reset'};
99 $fen .= $pos->{'move_num'};
106 return { %$pos, board => undef, fen => $pos->fen() };
109 sub parse_pretty_move {
110 my ($pos, $move) = @_;
111 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
114 # Returns a new Position object.
116 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
118 my $from_square = _pos_to_square($from_row, $from_col);
119 my $to_square = _pos_to_square($to_row, $to_col);
122 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
123 if ($pos->{'toplay'} eq 'W') {
124 $np->{'toplay'} = 'B';
125 $np->{'move_num'} = $pos->{'move_num'};
127 $np->{'toplay'} = 'W';
128 $np->{'move_num'} = $pos->{'move_num'} + 1;
131 my $piece = $pos->{'board'}[$from_row][$from_col];
132 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
134 # Find out if this was a two-step pawn move.
135 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
136 $np->{'ep_file_num'} = $from_col;
138 $np->{'ep_file_num'} = -1;
142 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
143 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
144 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
145 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
147 $np->{'white_castle_k'} = 0;
148 $np->{'white_castle_q'} = 0;
149 } elsif ($piece eq 'k') {
150 $np->{'black_castle_k'} = 0;
151 $np->{'black_castle_q'} = 0;
152 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
153 $np->{'white_castle_q'} = 0;
154 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
155 $np->{'white_castle_k'} = 0;
156 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
157 $np->{'black_castle_q'} = 0;
158 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
159 $np->{'black_castle_k'} = 0;
163 if (lc($piece) eq 'p' || $dest_piece ne '-') {
164 $np->{'time_since_100move_rule_reset'} = 0;
166 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
168 $np->{'player_w'} = $pos->{'player_w'};
169 $np->{'player_b'} = $pos->{'player_b'};
170 $np->{'last_move'} = '(move)'; # FIXME
175 my ($row, $col) = @_;
176 return sprintf("%c%d", ord('a') + $col, 8 - $row);
183 for my $pv_move (@pv) {
184 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
185 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
191 sub _col_letter_to_num {
192 return ord(shift) - ord('a');
195 sub _row_letter_to_num {
196 return 7 - (ord(shift) - ord('1'));
199 sub _parse_uci_move {
201 my $from_col = _col_letter_to_num(substr($move, 0, 1));
202 my $from_row = _row_letter_to_num(substr($move, 1, 1));
203 my $to_col = _col_letter_to_num(substr($move, 2, 1));
204 my $to_row = _row_letter_to_num(substr($move, 3, 1));
205 my $promo = substr($move, 4, 1);
206 return ($from_row, $from_col, $to_row, $to_col, $promo);