3 # There are too many chess modules on CPAN already, so here's another one...
13 # Takes in a FICS style 12-type position.
15 my ($class, $str) = @_;
17 my (@x) = split / /, $str;
19 $pos->{'board'} = Board->new(@x[1..8]);
20 $pos->{'toplay'} = $x[9];
21 $pos->{'ep_file_num'} = $x[10];
22 $pos->{'white_castle_k'} = $x[11];
23 $pos->{'white_castle_q'} = $x[12];
24 $pos->{'black_castle_k'} = $x[13];
25 $pos->{'black_castle_q'} = $x[14];
26 $pos->{'time_since_100move_rule_reset'} = $x[15];
27 $pos->{'player_w'} = $x[17];
28 $pos->{'player_b'} = $x[18];
29 $pos->{'player_w'} =~ s/^W?[FCIG]M//;
30 $pos->{'player_b'} =~ s/^W?[FCIG]M//;
31 $pos->{'white_clock'} = $x[24];
32 $pos->{'black_clock'} = $x[25];
33 $pos->{'move_num'} = $x[26];
34 if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
35 $pos->{'last_move_uci'} = $1 . $2;
37 $pos->{'last_move_uci'} = undef;
39 $pos->{'last_move'} = $x[29];
40 $pos->{'prettyprint_cache'} = {};
47 my ($class, $white, $black) = @_;
48 $white = "base64:" . MIME::Base64::encode_base64($white);
49 $black = "base64:" . MIME::Base64::encode_base64($black);
50 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");
54 my ($class, $fen) = @_;
55 my ($board, $toplay, $castling, $ep_square, $halfmove_clock, $fullmove_clock) = split / /, $fen;
58 $board =~ s/(\d)/"-"x$1/ge;
59 $pos->{'board'} = Board->new(split /\//, $board);
60 $pos->{'toplay'} = uc($toplay);
62 if ($ep_square =~ /^([a-h])/) {
63 $pos->{'ep_file_num'} = ord($1) - ord('a');
65 $pos->{'ep_file_num'} = -1;
68 $pos->{'white_castle_k'} = ($castling =~ /K/) ? 1 : 0;
69 $pos->{'white_castle_q'} = ($castling =~ /Q/) ? 1 : 0;
70 $pos->{'black_castle_k'} = ($castling =~ /k/) ? 1 : 0;
71 $pos->{'black_castle_q'} = ($castling =~ /q/) ? 1 : 0;
72 $pos->{'time_since_100move_rule_reset'} = $halfmove_clock // 0;
73 $pos->{'player_w'} = 'white';
74 $pos->{'player_b'} = 'black';
75 $pos->{'white_clock'} = 0;
76 $pos->{'black_clock'} = 0;
77 $pos->{'move_num'} = $fullmove_clock // 0;
78 $pos->{'last_move_uci'} = undef;
79 $pos->{'last_move'} = undef;
80 $pos->{'prettyprint_cache'} = {};
90 my $fen = $pos->{'board'}->fen();
94 $fen .= lc($pos->{'toplay'});
98 $castling .= "K" if ($pos->{'white_castle_k'} == 1);
99 $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
100 $castling .= "k" if ($pos->{'black_castle_k'} == 1);
101 $castling .= "q" if ($pos->{'black_castle_q'} == 1);
102 $castling = "-" if ($castling eq "");
103 # $castling = "-"; # chess960
109 if ($pos->{'ep_file_num'} != -1) {
110 my $col = $pos->{'ep_file_num'};
111 $ep = (qw(a b c d e f g h))[$col];
113 if ($pos->{'toplay'} eq 'B') {
124 $fen .= $pos->{'time_since_100move_rule_reset'};
128 $fen .= $pos->{'move_num'};
133 # Returns a compact bit string describing the same data as fen(),
134 # except for the half-move and full-move clock.
137 my $board = $pos->{'board'}->bitpacked_fen();
140 if ($pos->{'toplay'} eq 'W') {
146 $bits .= $pos->{'white_castle_k'};
147 $bits .= $pos->{'white_castle_q'};
148 $bits .= $pos->{'black_castle_k'};
149 $bits .= $pos->{'black_castle_q'};
151 my $col = $pos->{'ep_file_num'};
156 $bits .= (qw(000 001 010 011 100 101 110 111))[$col];
159 return $board . pack('b*', $bits);
164 my $json = { %$pos, fen => $pos->fen() };
165 delete $json->{'board'};
166 delete $json->{'prettyprint_cache'};
167 delete $json->{'black_castle_k'};
168 delete $json->{'black_castle_q'};
169 delete $json->{'white_castle_k'};
170 delete $json->{'white_castle_q'};
171 delete $json->{'time_since_100move_rule_reset'};
172 if ($json->{'player_w'} =~ /^base64:(.*)$/) {
173 $json->{'player_w'} = MIME::Base64::decode_base64($1);
175 if ($json->{'player_b'} =~ /^base64:(.*)$/) {
176 $json->{'player_b'} = MIME::Base64::decode_base64($1);
181 sub parse_pretty_move {
182 my ($pos, $move) = @_;
183 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
188 return $pos->{'board'}->num_pieces();
191 # Returns a new Position object.
193 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo, $pretty_move) = @_;
195 my $from_square = _pos_to_square($from_row, $from_col);
196 my $to_square = _pos_to_square($to_row, $to_col);
199 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
200 if ($pos->{'toplay'} eq 'W') {
201 $np->{'toplay'} = 'B';
202 $np->{'move_num'} = $pos->{'move_num'};
204 $np->{'toplay'} = 'W';
205 $np->{'move_num'} = $pos->{'move_num'} + 1;
208 my $piece = $pos->{'board'}[$from_row][$from_col];
209 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
211 # Find out if this was a two-step pawn move.
212 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
213 $np->{'ep_file_num'} = $from_col;
215 $np->{'ep_file_num'} = -1;
219 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
220 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
221 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
222 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
224 $np->{'white_castle_k'} = 0;
225 $np->{'white_castle_q'} = 0;
226 } elsif ($piece eq 'k') {
227 $np->{'black_castle_k'} = 0;
228 $np->{'black_castle_q'} = 0;
229 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
230 $np->{'white_castle_q'} = 0;
231 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
232 $np->{'white_castle_k'} = 0;
233 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
234 $np->{'black_castle_q'} = 0;
235 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
236 $np->{'black_castle_k'} = 0;
240 if (lc($piece) eq 'p' || $dest_piece ne '-') {
241 $np->{'time_since_100move_rule_reset'} = 0;
243 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
245 $np->{'player_w'} = $pos->{'player_w'};
246 $np->{'player_b'} = $pos->{'player_b'};
247 if (defined($pretty_move)) {
248 $np->{'last_move'} = $pretty_move;
250 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
251 $np->{'last_move'} = $move;
253 $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
258 # Returns a new Position object, and the parsed UCI move.
259 sub make_pretty_move {
260 my ($pos, $move) = @_;
262 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
263 my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
264 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
265 return ($pos, $uci_move);
269 my ($row, $col) = @_;
270 return sprintf("%c%d", ord('a') + $col, 8 - $row);
277 for my $pv_move (@pv) {
278 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
279 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
285 sub _col_letter_to_num {
286 return ord(shift) - ord('a');
289 sub _row_letter_to_num {
290 return 7 - (ord(shift) - ord('1'));
293 sub _parse_uci_move {
295 my $from_col = _col_letter_to_num(substr($move, 0, 1));
296 my $from_row = _row_letter_to_num(substr($move, 1, 1));
297 my $to_col = _col_letter_to_num(substr($move, 2, 1));
298 my $to_row = _row_letter_to_num(substr($move, 3, 1));
299 my $promo = substr($move, 4, 1);
300 return ($from_row, $from_col, $to_row, $to_col, $promo);