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'} = {};
41 $pos->{'tbprobe_cache'} = {};
48 my ($class, $white, $black) = @_;
49 $white = "base64:" . MIME::Base64::encode_base64($white);
50 $black = "base64:" . MIME::Base64::encode_base64($black);
51 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");
55 my ($class, $fen) = @_;
56 my ($board, $toplay, $castling, $ep_square, $halfmove_clock, $fullmove_clock) = split / /, $fen;
59 $board =~ s/(\d)/"-"x$1/ge;
60 $pos->{'board'} = Board->new(split /\//, $board);
61 $pos->{'toplay'} = uc($toplay);
63 if ($ep_square =~ /^([a-h])/) {
64 $pos->{'ep_file_num'} = ord($1) - ord('a');
66 $pos->{'ep_file_num'} = -1;
69 $pos->{'white_castle_k'} = ($castling =~ /K/) ? 1 : 0;
70 $pos->{'white_castle_q'} = ($castling =~ /Q/) ? 1 : 0;
71 $pos->{'black_castle_k'} = ($castling =~ /k/) ? 1 : 0;
72 $pos->{'black_castle_q'} = ($castling =~ /q/) ? 1 : 0;
73 $pos->{'time_since_100move_rule_reset'} = $halfmove_clock // 0;
74 $pos->{'player_w'} = 'white';
75 $pos->{'player_b'} = 'black';
76 $pos->{'white_clock'} = 0;
77 $pos->{'black_clock'} = 0;
78 $pos->{'move_num'} = $fullmove_clock // 0;
79 $pos->{'last_move_uci'} = undef;
80 $pos->{'last_move'} = undef;
81 $pos->{'prettyprint_cache'} = {};
82 $pos->{'tbprobe_cache'} = {};
92 my $fen = $pos->{'board'}->fen();
96 $fen .= lc($pos->{'toplay'});
100 $castling .= "K" if ($pos->{'white_castle_k'} == 1);
101 $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
102 $castling .= "k" if ($pos->{'black_castle_k'} == 1);
103 $castling .= "q" if ($pos->{'black_castle_q'} == 1);
104 $castling = "-" if ($castling eq "");
105 # $castling = "-"; # chess960
111 if ($pos->{'ep_file_num'} != -1) {
112 my $col = $pos->{'ep_file_num'};
113 $ep = (qw(a b c d e f g h))[$col];
115 if ($pos->{'toplay'} eq 'B') {
126 $fen .= $pos->{'time_since_100move_rule_reset'};
130 $fen .= $pos->{'move_num'};
135 # Returns a compact bit string describing the same data as fen(),
136 # except for the half-move and full-move clock.
139 my $board = $pos->{'board'}->bitpacked_fen();
142 if ($pos->{'toplay'} eq 'W') {
148 $bits .= $pos->{'white_castle_k'};
149 $bits .= $pos->{'white_castle_q'};
150 $bits .= $pos->{'black_castle_k'};
151 $bits .= $pos->{'black_castle_q'};
153 my $col = $pos->{'ep_file_num'};
158 $bits .= (qw(000 001 010 011 100 101 110 111))[$col];
161 return $board . pack('b*', $bits);
166 my $json = { %$pos, fen => $pos->fen() };
167 delete $json->{'board'};
168 delete $json->{'prettyprint_cache'};
169 delete $json->{'tbprobe_cache'};
170 delete $json->{'black_castle_k'};
171 delete $json->{'black_castle_q'};
172 delete $json->{'white_castle_k'};
173 delete $json->{'white_castle_q'};
174 delete $json->{'time_since_100move_rule_reset'};
175 if ($json->{'player_w'} =~ /^base64:(.*)$/) {
176 $json->{'player_w'} = MIME::Base64::decode_base64($1);
178 if ($json->{'player_b'} =~ /^base64:(.*)$/) {
179 $json->{'player_b'} = MIME::Base64::decode_base64($1);
184 sub parse_pretty_move {
185 my ($pos, $move) = @_;
186 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
191 return $pos->{'board'}->num_pieces();
194 # Returns a new Position object.
196 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo, $pretty_move) = @_;
198 my $from_square = _pos_to_square($from_row, $from_col);
199 my $to_square = _pos_to_square($to_row, $to_col);
202 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
203 if ($pos->{'toplay'} eq 'W') {
204 $np->{'toplay'} = 'B';
205 $np->{'move_num'} = $pos->{'move_num'};
207 $np->{'toplay'} = 'W';
208 $np->{'move_num'} = $pos->{'move_num'} + 1;
211 my $piece = $pos->{'board'}[$from_row][$from_col];
212 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
214 # Find out if this was a two-step pawn move.
215 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
216 $np->{'ep_file_num'} = $from_col;
218 $np->{'ep_file_num'} = -1;
222 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
223 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
224 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
225 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
227 $np->{'white_castle_k'} = 0;
228 $np->{'white_castle_q'} = 0;
229 } elsif ($piece eq 'k') {
230 $np->{'black_castle_k'} = 0;
231 $np->{'black_castle_q'} = 0;
232 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
233 $np->{'white_castle_q'} = 0;
234 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
235 $np->{'white_castle_k'} = 0;
236 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
237 $np->{'black_castle_q'} = 0;
238 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
239 $np->{'black_castle_k'} = 0;
243 if (lc($piece) eq 'p' || $dest_piece ne '-') {
244 $np->{'time_since_100move_rule_reset'} = 0;
246 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
248 $np->{'player_w'} = $pos->{'player_w'};
249 $np->{'player_b'} = $pos->{'player_b'};
250 if (defined($pretty_move)) {
251 $np->{'last_move'} = $pretty_move;
253 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
254 $np->{'last_move'} = $move;
256 $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
261 # Returns a new Position object, and the parsed UCI move.
262 sub make_pretty_move {
263 my ($pos, $move) = @_;
265 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
266 my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
267 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
268 return ($pos, $uci_move);
272 my ($row, $col) = @_;
273 return sprintf("%c%d", ord('a') + $col, 8 - $row);
280 for my $pv_move (@pv) {
281 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
282 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
288 sub _col_letter_to_num {
289 return ord(shift) - ord('a');
292 sub _row_letter_to_num {
293 return 7 - (ord(shift) - ord('1'));
296 sub _parse_uci_move {
298 my $from_col = _col_letter_to_num(substr($move, 0, 1));
299 my $from_row = _row_letter_to_num(substr($move, 1, 1));
300 my $to_col = _col_letter_to_num(substr($move, 2, 1));
301 my $to_row = _row_letter_to_num(substr($move, 3, 1));
302 my $promo = substr($move, 4, 1);
303 return ($from_row, $from_col, $to_row, $to_col, $promo);