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->{'move_num'} = $x[26];
32 if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
33 $pos->{'last_move_uci'} = $1 . $2;
35 $pos->{'last_move_uci'} = undef;
37 $pos->{'last_move'} = $x[29];
38 $pos->{'prettyprint_cache'} = {};
45 my ($class, $white, $black) = @_;
46 $white = "base64:" . MIME::Base64::encode_base64($white);
47 $black = "base64:" . MIME::Base64::encode_base64($black);
48 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 $fen = $pos->{'board'}->fen();
59 $fen .= lc($pos->{'toplay'});
63 $castling .= "K" if ($pos->{'white_castle_k'} == 1);
64 $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
65 $castling .= "k" if ($pos->{'black_castle_k'} == 1);
66 $castling .= "q" if ($pos->{'black_castle_q'} == 1);
67 $castling = "-" if ($castling eq "");
68 # $castling = "-"; # chess960
74 if ($pos->{'ep_file_num'} != -1) {
75 my $col = $pos->{'ep_file_num'};
76 my $nep = (qw(a b c d e f g h))[$col];
78 if ($pos->{'toplay'} eq 'B') {
85 # Showing the en passant square when actually no capture can be made
86 # seems to confuse at least Rybka. Thus, check if there's actually
87 # a pawn of the opposite side that can do the en passant move, and if
88 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
89 # "right" thing as per the standard, though.
91 if ($pos->{'toplay'} eq 'B') {
92 $ep = $nep if ($col > 0 && $pos->{'board'}[4][$col-1] eq 'p');
93 $ep = $nep if ($col < 7 && $pos->{'board'}[4][$col+1] eq 'p');
95 $ep = $nep if ($col > 0 && $pos->{'board'}[3][$col-1] eq 'P');
96 $ep = $nep if ($col < 7 && $pos->{'board'}[3][$col+1] eq 'P');
104 $fen .= $pos->{'time_since_100move_rule_reset'};
108 $fen .= $pos->{'move_num'};
115 my $json = { %$pos, board => undef, prettyprint_cache => undef, fen => $pos->fen() };
116 if ($json->{'player_w'} =~ /^base64:(.*)$/) {
117 $json->{'player_w'} = MIME::Base64::decode_base64($1);
119 if ($json->{'player_b'} =~ /^base64:(.*)$/) {
120 $json->{'player_b'} = MIME::Base64::decode_base64($1);
125 sub parse_pretty_move {
126 my ($pos, $move) = @_;
127 return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
132 return $pos->{'board'}->num_pieces();
135 # Returns a new Position object.
137 my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
139 my $from_square = _pos_to_square($from_row, $from_col);
140 my $to_square = _pos_to_square($to_row, $to_col);
143 $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
144 if ($pos->{'toplay'} eq 'W') {
145 $np->{'toplay'} = 'B';
146 $np->{'move_num'} = $pos->{'move_num'};
148 $np->{'toplay'} = 'W';
149 $np->{'move_num'} = $pos->{'move_num'} + 1;
152 my $piece = $pos->{'board'}[$from_row][$from_col];
153 my $dest_piece = $pos->{'board'}[$to_row][$to_col];
155 # Find out if this was a two-step pawn move.
156 if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
157 $np->{'ep_file_num'} = $from_col;
159 $np->{'ep_file_num'} = -1;
163 $np->{'white_castle_k'} = $pos->{'white_castle_k'};
164 $np->{'white_castle_q'} = $pos->{'white_castle_q'};
165 $np->{'black_castle_k'} = $pos->{'black_castle_k'};
166 $np->{'black_castle_q'} = $pos->{'black_castle_q'};
168 $np->{'white_castle_k'} = 0;
169 $np->{'white_castle_q'} = 0;
170 } elsif ($piece eq 'k') {
171 $np->{'black_castle_k'} = 0;
172 $np->{'black_castle_q'} = 0;
173 } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
174 $np->{'white_castle_q'} = 0;
175 } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
176 $np->{'white_castle_k'} = 0;
177 } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
178 $np->{'black_castle_q'} = 0;
179 } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
180 $np->{'black_castle_k'} = 0;
184 if (lc($piece) eq 'p' || $dest_piece ne '-') {
185 $np->{'time_since_100move_rule_reset'} = 0;
187 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
189 $np->{'player_w'} = $pos->{'player_w'};
190 $np->{'player_b'} = $pos->{'player_b'};
191 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
192 $np->{'last_move'} = $move;
193 $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
198 # Returns a new Position object, and the parsed UCI move.
199 sub make_pretty_move {
200 my ($pos, $move) = @_;
202 my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
203 my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
204 $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
205 return ($pos, $uci_move);
209 my ($row, $col) = @_;
210 return sprintf("%c%d", ord('a') + $col, 8 - $row);
217 for my $pv_move (@pv) {
218 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
219 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
225 sub _col_letter_to_num {
226 return ord(shift) - ord('a');
229 sub _row_letter_to_num {
230 return 7 - (ord(shift) - ord('1'));
233 sub _parse_uci_move {
235 my $from_col = _col_letter_to_num(substr($move, 0, 1));
236 my $from_row = _row_letter_to_num(substr($move, 1, 1));
237 my $to_col = _col_letter_to_num(substr($move, 2, 1));
238 my $to_row = _row_letter_to_num(substr($move, 3, 1));
239 my $promo = substr($move, 4, 1);
240 return ($from_row, $from_col, $to_row, $to_col, $promo);