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 # Note: This is in general not a validation that the move is actually allowed
152 # (e.g. you can castle even though you're in check).
153 sub parse_pretty_move {
154 my ($board, $move, $toplay) = @_;
156 # Strip check or mate
159 if ($move eq '0-0' or $move eq 'O-O') {
160 if ($toplay eq 'W') {
161 return (_square_to_pos('e1'), _square_to_pos('g1'));
163 return (_square_to_pos('e8'), _square_to_pos('g8'));
165 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
166 if ($toplay eq 'W') {
167 return (_square_to_pos('e1'), _square_to_pos('c1'));
169 return (_square_to_pos('e8'), _square_to_pos('c8'));
175 if ($move =~ s/=?([QRNB])$//) {
179 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
181 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
182 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
183 if (!defined($piece) && (!defined($from_col) || !defined($from_row))) {
186 my ($to_row, $to_col) = _square_to_pos($4);
188 # Find all possible from-squares that could have been meant.
191 if ($toplay eq 'B') {
192 $piece = lc($piece) if defined($piece);
196 next if (defined($from_row) && $from_row != $row);
198 next if (defined($from_col) && $from_col != $col);
199 next if (defined($piece) && $board->[$row][$col] ne $piece);
200 push @squares, [ $row, $col ];
204 # Filter out pieces which cannot reach this square.
205 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
207 # See if doing this move would put us in check
208 # (yes, there are clients that expect us to do this).
209 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
211 if (scalar @squares == 0) {
212 die "Impossible move $move";
214 if (scalar @squares != 1) {
215 die "Ambigious move $move";
217 return (@{$squares[0]}, $to_row, $to_col, $promo);
224 my $str = join('', @{$board->[$row]});
225 $str =~ s/(-+)/length($1)/ge;
229 return join('/', @rows);
232 # Returns a compact bit string describing the same data as fen().
233 # This is encoded using a Huffman-like encoding, and should be
234 # typically about 1/3 the number of bytes.
241 my $piece = $board->[$row][$col];
247 my $color = (lc($piece) eq $piece) ? 0 : 1;
248 $bits .= "1" . $color;
250 if (lc($piece) eq 'p') {
252 } elsif (lc($piece) eq 'n') {
254 } elsif (lc($piece) eq 'b') {
256 } elsif (lc($piece) eq 'r') {
258 } elsif (lc($piece) eq 'q') {
260 } elsif (lc($piece) eq 'k') {
263 die "Unknown piece $piece";
268 return pack('b*', $bits);
272 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
274 # can't eat your own piece
275 my $dest_piece = $board->[$to_row][$to_col];
276 if ($dest_piece ne '-') {
277 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
282 if ($to_col == $from_col && $to_row == $from_row + 1) {
283 return ($dest_piece eq '-');
285 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
286 my $middle_piece = $board->[2][$to_col];
287 return ($dest_piece eq '-' && $middle_piece eq '-');
289 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
290 if ($dest_piece eq '-') {
291 # En passant. TODO: check that the last move was indeed an EP move
292 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
301 if ($to_col == $from_col && $to_row == $from_row - 1) {
302 return ($dest_piece eq '-');
304 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
305 my $middle_piece = $board->[5][$to_col];
306 return ($dest_piece eq '-' && $middle_piece eq '-');
308 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
309 if ($dest_piece eq '-') {
310 # En passant. TODO: check that the last move was indeed an EP move
311 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
319 if (lc($piece) eq 'r') {
320 return 0 unless ($from_row == $to_row || $from_col == $to_col);
322 # check that there's a clear passage
323 if ($from_row == $to_row) {
324 if ($from_col > $to_col) {
325 ($to_col, $from_col) = ($from_col, $to_col);
328 for my $c (($from_col+1)..($to_col-1)) {
329 my $middle_piece = $board->[$to_row][$c];
330 return 0 if ($middle_piece ne '-');
335 if ($from_row > $to_row) {
336 ($to_row, $from_row) = ($from_row, $to_row);
339 for my $r (($from_row+1)..($to_row-1)) {
340 my $middle_piece = $board->[$r][$to_col];
341 return 0 if ($middle_piece ne '-');
347 if (lc($piece) eq 'b') {
348 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
350 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
351 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
353 my $r = $from_row + $dr;
354 my $c = $from_col + $dc;
356 while ($r != $to_row) {
357 my $middle_piece = $board->[$r][$c];
358 return 0 if ($middle_piece ne '-');
366 if (lc($piece) eq 'n') {
367 my $diff_r = abs($from_row - $to_row);
368 my $diff_c = abs($from_col - $to_col);
369 return 1 if ($diff_r == 2 && $diff_c == 1);
370 return 1 if ($diff_r == 1 && $diff_c == 2);
374 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
375 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
378 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
379 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
381 if (lc($piece) eq 'k') {
382 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
389 # Like can_reach, but also checks the move doesn't put the side in check.
390 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
391 # needs moves to be in minimally disambiguated form.
392 sub can_legally_reach {
393 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
395 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
397 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
398 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
400 return !in_check($nb, $side);
403 my %pieces_against_side = (
404 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
405 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
408 # Returns whether the given side (given as k or K for black and white) is in check.
410 my ($board, $side) = @_;
411 my ($kr, $kc) = _find_piece($board, $side);
413 # check all pieces for the possibility of threatening this king
415 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
417 my $piece = $board->[$row][$col];
418 next if ($piece eq '-');
419 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
427 my ($board, $piece) = @_;
430 next unless grep { $_ eq $piece } @{$board->[$row]};
432 if ($board->[$row][$col] eq $piece) {
438 return (undef, undef);
441 # Returns if the given side (given as k or K) is in mate.
443 my ($board, $side, $in_check) = @_;
444 return 0 if (!$in_check);
446 # try all possible moves for the side in check
449 my $piece = $board->[$row][$col];
450 next if ($piece eq '-');
453 next if ($piece eq lc($piece));
455 next if ($piece eq uc($piece));
458 for my $dest_row (0..7) {
459 for my $dest_col (0..7) {
460 next if ($row == $dest_row && $col == $dest_col);
461 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
463 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
464 return 0 if (!$nb->in_check($side));
470 # nothing to do; mate
474 # Returns the short algebraic form of the move, as well as the new position.
475 sub prettyprint_move {
476 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
477 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
479 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
481 my $piece = $board->[$from_row][$from_col];
482 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
483 my $in_check = $nb->in_check($other_side);
484 if ($nb->in_mate($other_side, $in_check)) {
486 } elsif ($in_check) {
489 return ($pretty, $nb);
498 my $piece = $board->[$row][$col];
499 ++$num if ($piece ne '-');
505 sub _prettyprint_move_no_check_or_mate {
506 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
507 my $piece = $board->[$from_row][$from_col];
508 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
511 die "Invalid move $move";
514 # white short castling
515 if ($move eq 'e1g1' && $piece eq 'K') {
519 # white long castling
520 if ($move eq 'e1c1' && $piece eq 'K') {
524 # black short castling
525 if ($move eq 'e8g8' && $piece eq 'k') {
529 # black long castling
530 if ($move eq 'e8c8' && $piece eq 'k') {
536 # check if the from-piece is a pawn
537 if (lc($piece) eq 'p') {
539 if ($from_col != $to_col) {
540 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
542 $pretty = _pos_to_square($to_row, $to_col);
545 if (defined($promo) && $promo ne '') {
548 $pretty .= uc($promo);
553 $pretty = uc($piece);
555 # see how many of these pieces could go here, in all
559 next unless ($board->[$row][$col] eq $piece);
560 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
564 # see how many of these pieces from the given row could go here
567 next unless ($board->[$from_row][$col] eq $piece);
568 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
571 # and same for columns
574 next unless ($board->[$row][$from_col] eq $piece);
575 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
578 # see if we need to disambiguate
579 if ($num_total > 1) {
581 $pretty .= substr($move, 0, 1);
582 } elsif ($num_row == 1) {
583 $pretty .= substr($move, 1, 1);
585 $pretty .= substr($move, 0, 2);
590 if ($board->[$to_row][$to_col] ne '-') {
594 $pretty .= _pos_to_square($to_row, $to_col);