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");
57 my $fen = $pos->{'board'}->fen();
61 $fen .= lc($pos->{'toplay'});
65 $castling .= "K" if ($pos->{'white_castle_k'} == 1);
66 $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
67 $castling .= "k" if ($pos->{'black_castle_k'} == 1);
68 $castling .= "q" if ($pos->{'black_castle_q'} == 1);
69 $castling = "-" if ($castling eq "");
70 # $castling = "-"; # chess960
76 if ($pos->{'ep_file_num'} != -1) {
77 my $col = $pos->{'ep_file_num'};
78 $ep = (qw(a b c d e f g h))[$col];
80 if ($pos->{'toplay'} eq 'B') {
91 $fen .= $pos->{'time_since_100move_rule_reset'};
95 $fen .= $pos->{'move_num'};
100 # Returns a compact bit string describing the same data as fen(),
101 # except for the half-move and full-move clock.
104 my $board = $pos->{'board'}->bitpacked_fen();
107 if ($pos->{'toplay'} eq 'W') {
113 $bits .= $pos->{'white_castle_k'};
114 $bits .= $pos->{'white_castle_q'};
115 $bits .= $pos->{'black_castle_k'};
116 $bits .= $pos->{'black_castle_q'};
118 my $col = $pos->{'ep_file_num'};
123 $bits .= (qw(000 001 010 011 100 101 110 111))[$col];
126 return $board . pack('b*', $bits);
131 my $json = { %$pos, fen => $pos->fen() };
132 delete $json->{'board'};
133 delete $json->{'prettyprint_cache'};
134 delete $json->{'black_castle_k'};
135 delete $json->{'black_castle_q'};
136 delete $json->{'white_castle_k'};
137 delete $json->{'white_castle_q'};
138 delete $json->{'time_since_100move_rule_reset'};
139 if ($json->{'player_w'} =~ /^base64:(.*)$/) {
140 $json->{'player_w'} = MIME::Base64::decode_base64($1);
142 if ($json->{'player_b'} =~ /^base64:(.*)$/) {
143 $json->{'player_b'} = MIME::Base64::decode_base64($1);
148 sub parse_pretty_move {
149 my ($pos, $move) = @_;
150 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
155 return $pos->{'board'}->num_pieces();
158 # Returns a new Position object.
160 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
162 my $from_square = _pos_to_square($from_row, $from_col);
163 my $to_square = _pos_to_square($to_row, $to_col);
166 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
167 if ($pos->{'toplay'} eq 'W') {
168 $np->{'toplay'} = 'B';
169 $np->{'move_num'} = $pos->{'move_num'};
171 $np->{'toplay'} = 'W';
172 $np->{'move_num'} = $pos->{'move_num'} + 1;
175 my $piece = $pos->{'board'}[$from_row][$from_col];
176 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
178 # Find out if this was a two-step pawn move.
179 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
180 $np->{'ep_file_num'} = $from_col;
182 $np->{'ep_file_num'} = -1;
186 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
187 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
188 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
189 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
191 $np->{'white_castle_k'} = 0;
192 $np->{'white_castle_q'} = 0;
193 } elsif ($piece eq 'k') {
194 $np->{'black_castle_k'} = 0;
195 $np->{'black_castle_q'} = 0;
196 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
197 $np->{'white_castle_q'} = 0;
198 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
199 $np->{'white_castle_k'} = 0;
200 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
201 $np->{'black_castle_q'} = 0;
202 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
203 $np->{'black_castle_k'} = 0;
207 if (lc($piece) eq 'p' || $dest_piece ne '-') {
208 $np->{'time_since_100move_rule_reset'} = 0;
210 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
212 $np->{'player_w'} = $pos->{'player_w'};
213 $np->{'player_b'} = $pos->{'player_b'};
214 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
215 $np->{'last_move'} = $move;
216 $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
221 # Returns a new Position object, and the parsed UCI move.
222 sub make_pretty_move {
223 my ($pos, $move) = @_;
225 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
226 my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
227 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
228 return ($pos, $uci_move);
232 my ($row, $col) = @_;
233 return sprintf("%c%d", ord('a') + $col, 8 - $row);
240 for my $pv_move (@pv) {
241 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
242 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
248 sub _col_letter_to_num {
249 return ord(shift) - ord('a');
252 sub _row_letter_to_num {
253 return 7 - (ord(shift) - ord('1'));
256 sub _parse_uci_move {
258 my $from_col = _col_letter_to_num(substr($move, 0, 1));
259 my $from_row = _row_letter_to_num(substr($move, 1, 1));
260 my $to_col = _col_letter_to_num(substr($move, 2, 1));
261 my $to_row = _row_letter_to_num(substr($move, 3, 1));
262 my $promo = substr($move, 4, 1);
263 return ($from_row, $from_col, $to_row, $to_col, $promo);