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'};
137 my $json = { %$pos, fen => $pos->fen() };
138 delete $json->{'board'};
139 delete $json->{'prettyprint_cache'};
140 delete $json->{'tbprobe_cache'};
141 delete $json->{'black_castle_k'};
142 delete $json->{'black_castle_q'};
143 delete $json->{'white_castle_k'};
144 delete $json->{'white_castle_q'};
145 delete $json->{'time_since_100move_rule_reset'};
146 delete $json->{'chess960'} if (!$json->{'chess960'});
147 if ($json->{'player_w'} =~ /^base64:(.*)$/) {
148 $json->{'player_w'} = MIME::Base64::decode_base64($1);
150 if ($json->{'player_b'} =~ /^base64:(.*)$/) {
151 $json->{'player_b'} = MIME::Base64::decode_base64($1);
156 sub parse_pretty_move {
157 my ($pos, $move) = @_;
158 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
163 return $pos->{'board'}->num_pieces();
166 # Returns a new Position object.
168 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo, $pretty_move) = @_;
170 my $from_square = _pos_to_square($from_row, $from_col);
171 my $to_square = _pos_to_square($to_row, $to_col);
174 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
175 if ($pos->{'toplay'} eq 'W') {
176 $np->{'toplay'} = 'B';
177 $np->{'move_num'} = $pos->{'move_num'};
179 $np->{'toplay'} = 'W';
180 $np->{'move_num'} = $pos->{'move_num'} + 1;
183 my $piece = $pos->{'board'}[$from_row][$from_col];
184 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
186 # Find out if this was a two-step pawn move.
187 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
188 $np->{'ep_file_num'} = $from_col;
190 $np->{'ep_file_num'} = -1;
194 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
195 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
196 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
197 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
199 $np->{'white_castle_k'} = 0;
200 $np->{'white_castle_q'} = 0;
201 } elsif ($piece eq 'k') {
202 $np->{'black_castle_k'} = 0;
203 $np->{'black_castle_q'} = 0;
204 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
205 $np->{'white_castle_q'} = 0;
206 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
207 $np->{'white_castle_k'} = 0;
208 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
209 $np->{'black_castle_q'} = 0;
210 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
211 $np->{'black_castle_k'} = 0;
215 if (lc($piece) eq 'p' || $dest_piece ne '-') {
216 $np->{'time_since_100move_rule_reset'} = 0;
218 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
220 $np->{'player_w'} = $pos->{'player_w'};
221 $np->{'player_b'} = $pos->{'player_b'};
222 $np->{'chess960'} = $pos->{'chess960'};
223 if (exists($pos->{'start_fen'})) {
224 $np->{'start_fen'} = $pos->{'start_fen'};
226 if (defined($pretty_move)) {
227 $np->{'last_move'} = $pretty_move;
229 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
230 $np->{'last_move'} = $move;
232 $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
237 # Returns a new Position object, and the parsed UCI move.
238 sub make_pretty_move {
239 my ($pos, $move) = @_;
241 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
242 my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
243 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
244 return ($pos, $uci_move);
248 my ($row, $col) = @_;
249 return sprintf("%c%d", ord('a') + $col, 8 - $row);
256 for my $pv_move (@pv) {
257 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
258 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
264 sub _col_letter_to_num {
265 return ord(shift) - ord('a');
268 sub _row_letter_to_num {
269 return 7 - (ord(shift) - ord('1'));
272 sub _parse_uci_move {
274 my $from_col = _col_letter_to_num(substr($move, 0, 1));
275 my $from_row = _row_letter_to_num(substr($move, 1, 1));
276 my $to_col = _col_letter_to_num(substr($move, 2, 1));
277 my $to_row = _row_letter_to_num(substr($move, 3, 1));
278 my $promo = substr($move, 4, 1);
279 return ($from_row, $from_col, $to_row, $to_col, $promo);