3 # There are too many chess modules on CPAN already, so here's another one...
11 my ($class, @rows) = @_;
16 $board->[$row][$col] = substr($rows[$row], $col, 1);
28 $nb->[$row] = [ @{$board->[$row]} ];
34 # Returns a new board.
36 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
37 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
38 my $piece = $board->[$from_row][$from_col];
39 my $nb = $board->clone();
42 die "Invalid move $move";
45 # white short castling
46 if ($move eq 'e1g1' && $piece eq 'K') {
59 if ($move eq 'e1c1' && $piece eq 'K') {
71 # black short castling
72 if ($move eq 'e8g8' && $piece eq 'k') {
85 if ($move eq 'e8c8' && $piece eq 'k') {
97 # check if the from-piece is a pawn
98 if (lc($piece) eq 'p') {
100 if ($from_col != $to_col) {
102 if ($board->[$to_row][$to_col] eq '-') {
104 $nb->[$to_row - 1][$to_col] = '-';
106 $nb->[$to_row + 1][$to_col] = '-';
110 if (defined($promo) && $promo ne '') {
120 $nb->[$from_row][$from_col] = '-';
121 $nb->[$to_row][$to_col] = $piece;
127 my ($row, $col) = @_;
128 return sprintf("%c%d", ord('a') + $col, 8 - $row);
131 sub _col_letter_to_num {
132 return ord(shift) - ord('a');
135 sub _row_letter_to_num {
136 return 7 - (ord(shift) - ord('1'));
141 $square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
142 return (_row_letter_to_num($2), _col_letter_to_num($1));
145 sub move_to_uci_notation {
146 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
148 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
151 sub parse_pretty_move {
152 my ($board, $move, $toplay) = @_;
154 # Strip check or mate
157 if ($move eq '0-0' or $move eq 'O-O') {
158 if ($toplay eq 'W') {
159 return (_square_to_pos('e1'), _square_to_pos('g1'));
161 return (_square_to_pos('e8'), _square_to_pos('g8'));
163 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
164 if ($toplay eq 'W') {
165 return (_square_to_pos('e1'), _square_to_pos('c1'));
167 return (_square_to_pos('e8'), _square_to_pos('c8'));
173 if ($move =~ s/=?([QRNB])$//) {
177 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
178 my $piece = $1 // 'P';
179 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
180 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
181 my ($to_row, $to_col) = _square_to_pos($4);
183 # Find all possible from-squares that could have been meant.
186 if ($toplay eq 'B') {
191 next if (defined($from_row) && $from_row != $row);
193 next if (defined($from_col) && $from_col != $col);
194 next if ($board->[$row][$col] ne $piece);
195 next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
197 # See if doing this move would put us in check
198 # (yes, there are clients that expect us to do this).
199 next if ($board->make_move($row, $col, $to_row, $to_col, $promo)->in_check($side));
200 push @squares, [ $row, $col ];
203 if (scalar @squares != 1) {
204 die "Ambigious or impossible move $move";
206 return (@{$squares[0]}, $to_row, $to_col, $promo);
213 my $str = join('', @{$board->[$row]});
214 $str =~ s/(-+)/length($1)/ge;
218 return join('/', @rows);
222 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
224 # can't eat your own piece
225 my $dest_piece = $board->[$to_row][$to_col];
226 if ($dest_piece ne '-') {
227 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
232 if ($to_col == $from_col && $to_row == $from_row + 1) {
233 return ($dest_piece eq '-');
235 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
236 my $middle_piece = $board->[2][$to_col];
237 return ($dest_piece eq '-' && $middle_piece eq '-');
239 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
240 if ($dest_piece eq '-') {
241 # En passant. TODO: check that the last move was indeed an EP move
242 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
251 if ($to_col == $from_col && $to_row == $from_row - 1) {
252 return ($dest_piece eq '-');
254 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
255 my $middle_piece = $board->[5][$to_col];
256 return ($dest_piece eq '-' && $middle_piece eq '-');
258 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
259 if ($dest_piece eq '-') {
260 # En passant. TODO: check that the last move was indeed an EP move
261 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
269 if (lc($piece) eq 'r') {
270 return 0 unless ($from_row == $to_row || $from_col == $to_col);
272 # check that there's a clear passage
273 if ($from_row == $to_row) {
274 if ($from_col > $to_col) {
275 ($to_col, $from_col) = ($from_col, $to_col);
278 for my $c (($from_col+1)..($to_col-1)) {
279 my $middle_piece = $board->[$to_row][$c];
280 return 0 if ($middle_piece ne '-');
285 if ($from_row > $to_row) {
286 ($to_row, $from_row) = ($from_row, $to_row);
289 for my $r (($from_row+1)..($to_row-1)) {
290 my $middle_piece = $board->[$r][$to_col];
291 return 0 if ($middle_piece ne '-');
297 if (lc($piece) eq 'b') {
298 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
300 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
301 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
303 my $r = $from_row + $dr;
304 my $c = $from_col + $dc;
306 while ($r != $to_row) {
307 my $middle_piece = $board->[$r][$c];
308 return 0 if ($middle_piece ne '-');
316 if (lc($piece) eq 'n') {
317 my $diff_r = abs($from_row - $to_row);
318 my $diff_c = abs($from_col - $to_col);
319 return 1 if ($diff_r == 2 && $diff_c == 1);
320 return 1 if ($diff_r == 1 && $diff_c == 2);
324 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
325 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
328 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
329 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
331 if (lc($piece) eq 'k') {
332 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
339 my %pieces_against_side = (
340 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
341 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
344 # Returns whether the given side (given as k or K for black and white) is in check.
346 my ($board, $side) = @_;
347 my ($kr, $kc) = _find_piece($board, $side);
349 # check all pieces for the possibility of threatening this king
351 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
353 my $piece = $board->[$row][$col];
354 next if ($piece eq '-');
355 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
363 my ($board, $piece) = @_;
366 next unless grep { $_ eq $piece } @{$board->[$row]};
368 if ($board->[$row][$col] eq $piece) {
374 return (undef, undef);
377 # Returns if the given side (given as k or K) is in mate.
379 my ($board, $side, $in_check) = @_;
380 return 0 if (!$in_check);
382 # try all possible moves for the side in check
385 my $piece = $board->[$row][$col];
386 next if ($piece eq '-');
389 next if ($piece eq lc($piece));
391 next if ($piece eq uc($piece));
394 for my $dest_row (0..7) {
395 for my $dest_col (0..7) {
396 next if ($row == $dest_row && $col == $dest_col);
397 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
399 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
400 return 0 if (!$nb->in_check($side));
406 # nothing to do; mate
410 # Returns the short algebraic form of the move, as well as the new position.
411 sub prettyprint_move {
412 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
413 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
415 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
417 my $piece = $board->[$from_row][$from_col];
418 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
419 my $in_check = $nb->in_check($other_side);
420 if ($nb->in_mate($other_side, $in_check)) {
422 } elsif ($in_check) {
425 return ($pretty, $nb);
434 my $piece = $board->[$row][$col];
435 ++$num if ($piece ne '-');
441 sub _prettyprint_move_no_check_or_mate {
442 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
443 my $piece = $board->[$from_row][$from_col];
444 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
447 die "Invalid move $move";
450 # white short castling
451 if ($move eq 'e1g1' && $piece eq 'K') {
455 # white long castling
456 if ($move eq 'e1c1' && $piece eq 'K') {
460 # black short castling
461 if ($move eq 'e8g8' && $piece eq 'k') {
465 # black long castling
466 if ($move eq 'e8c8' && $piece eq 'k') {
472 # check if the from-piece is a pawn
473 if (lc($piece) eq 'p') {
475 if ($from_col != $to_col) {
476 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
478 $pretty = _pos_to_square($to_row, $to_col);
480 if (defined($promo) && $promo ne '') {
483 $pretty .= uc($promo);
489 $pretty = uc($piece);
491 # see how many of these pieces could go here, in all
495 next unless ($board->[$row][$col] eq $piece);
496 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
500 # see how many of these pieces from the given row could go here
503 next unless ($board->[$from_row][$col] eq $piece);
504 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
507 # and same for columns
510 next unless ($board->[$row][$from_col] eq $piece);
511 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
514 # see if we need to disambiguate
515 if ($num_total > 1) {
517 $pretty .= substr($move, 0, 1);
518 } elsif ($num_row == 1) {
519 $pretty .= substr($move, 1, 1);
521 $pretty .= substr($move, 0, 2);
526 if ($board->[$to_row][$to_col] ne '-') {
530 $pretty .= _pos_to_square($to_row, $to_col);