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 ];
203 if (scalar @squares > 1) {
204 # Filter out pieces which cannot reach this square.
205 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
207 if (scalar @squares > 1) {
208 # See if doing this move would put us in check
209 # (yes, there are clients that expect us to do this).
210 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
212 if (scalar @squares == 0) {
213 die "Impossible move $move";
215 if (scalar @squares != 1) {
216 die "Ambigious move $move";
218 return (@{$squares[0]}, $to_row, $to_col, $promo);
225 my $str = join('', @{$board->[$row]});
226 $str =~ s/(-+)/length($1)/ge;
230 return join('/', @rows);
233 # Returns a compact bit string describing the same data as fen().
234 # This is encoded using a Huffman-like encoding, and should be
235 # typically about 1/3 the number of bytes.
242 my $piece = $board->[$row][$col];
248 my $color = (lc($piece) eq $piece) ? 0 : 1;
249 $bits .= "1" . $color;
251 if (lc($piece) eq 'p') {
253 } elsif (lc($piece) eq 'n') {
255 } elsif (lc($piece) eq 'b') {
257 } elsif (lc($piece) eq 'r') {
259 } elsif (lc($piece) eq 'q') {
261 } elsif (lc($piece) eq 'k') {
264 die "Unknown piece $piece";
269 return pack('b*', $bits);
273 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
275 # can't eat your own piece
276 my $dest_piece = $board->[$to_row][$to_col];
277 if ($dest_piece ne '-') {
278 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
283 if ($to_col == $from_col && $to_row == $from_row + 1) {
284 return ($dest_piece eq '-');
286 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
287 my $middle_piece = $board->[2][$to_col];
288 return ($dest_piece eq '-' && $middle_piece eq '-');
290 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
291 if ($dest_piece eq '-') {
292 # En passant. TODO: check that the last move was indeed an EP move
293 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
302 if ($to_col == $from_col && $to_row == $from_row - 1) {
303 return ($dest_piece eq '-');
305 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
306 my $middle_piece = $board->[5][$to_col];
307 return ($dest_piece eq '-' && $middle_piece eq '-');
309 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
310 if ($dest_piece eq '-') {
311 # En passant. TODO: check that the last move was indeed an EP move
312 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
320 if (lc($piece) eq 'r') {
321 return 0 unless ($from_row == $to_row || $from_col == $to_col);
323 # check that there's a clear passage
324 if ($from_row == $to_row) {
325 if ($from_col > $to_col) {
326 ($to_col, $from_col) = ($from_col, $to_col);
329 for my $c (($from_col+1)..($to_col-1)) {
330 my $middle_piece = $board->[$to_row][$c];
331 return 0 if ($middle_piece ne '-');
336 if ($from_row > $to_row) {
337 ($to_row, $from_row) = ($from_row, $to_row);
340 for my $r (($from_row+1)..($to_row-1)) {
341 my $middle_piece = $board->[$r][$to_col];
342 return 0 if ($middle_piece ne '-');
348 if (lc($piece) eq 'b') {
349 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
351 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
352 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
354 my $r = $from_row + $dr;
355 my $c = $from_col + $dc;
357 while ($r != $to_row) {
358 my $middle_piece = $board->[$r][$c];
359 return 0 if ($middle_piece ne '-');
367 if (lc($piece) eq 'n') {
368 my $diff_r = abs($from_row - $to_row);
369 my $diff_c = abs($from_col - $to_col);
370 return 1 if ($diff_r == 2 && $diff_c == 1);
371 return 1 if ($diff_r == 1 && $diff_c == 2);
375 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
376 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
379 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
380 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
382 if (lc($piece) eq 'k') {
383 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
390 # Like can_reach, but also checks the move doesn't put the side in check.
391 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
392 # needs moves to be in minimally disambiguated form.
393 sub can_legally_reach {
394 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
396 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
398 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
399 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
401 return !in_check($nb, $side);
404 my %pieces_against_side = (
405 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
406 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
409 # Returns whether the given side (given as k or K for black and white) is in check.
411 my ($board, $side) = @_;
412 my ($kr, $kc) = _find_piece($board, $side);
414 # check all pieces for the possibility of threatening this king
416 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
418 my $piece = $board->[$row][$col];
419 next if ($piece eq '-');
420 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
428 my ($board, $piece) = @_;
431 next unless grep { $_ eq $piece } @{$board->[$row]};
433 if ($board->[$row][$col] eq $piece) {
439 return (undef, undef);
442 # Returns if the given side (given as k or K) is in mate.
444 my ($board, $side, $in_check) = @_;
445 return 0 if (!$in_check);
447 # try all possible moves for the side in check
450 my $piece = $board->[$row][$col];
451 next if ($piece eq '-');
454 next if ($piece eq lc($piece));
456 next if ($piece eq uc($piece));
459 for my $dest_row (0..7) {
460 for my $dest_col (0..7) {
461 next if ($row == $dest_row && $col == $dest_col);
462 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
464 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
465 return 0 if (!$nb->in_check($side));
471 # nothing to do; mate
475 # Returns the short algebraic form of the move, as well as the new position.
476 sub prettyprint_move {
477 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
478 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
480 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
482 my $piece = $board->[$from_row][$from_col];
483 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
484 my $in_check = $nb->in_check($other_side);
485 if ($nb->in_mate($other_side, $in_check)) {
487 } elsif ($in_check) {
490 return ($pretty, $nb);
499 my $piece = $board->[$row][$col];
500 ++$num if ($piece ne '-');
506 sub _prettyprint_move_no_check_or_mate {
507 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
508 my $piece = $board->[$from_row][$from_col];
509 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
512 die "Invalid move $move";
515 # white short castling
516 if ($move eq 'e1g1' && $piece eq 'K') {
520 # white long castling
521 if ($move eq 'e1c1' && $piece eq 'K') {
525 # black short castling
526 if ($move eq 'e8g8' && $piece eq 'k') {
530 # black long castling
531 if ($move eq 'e8c8' && $piece eq 'k') {
537 # check if the from-piece is a pawn
538 if (lc($piece) eq 'p') {
540 if ($from_col != $to_col) {
541 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
543 $pretty = _pos_to_square($to_row, $to_col);
546 if (defined($promo) && $promo ne '') {
549 $pretty .= uc($promo);
554 $pretty = uc($piece);
556 # see how many of these pieces could go here, in all
560 next unless ($board->[$row][$col] eq $piece);
561 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
565 # see how many of these pieces from the given row could go here
568 next unless ($board->[$from_row][$col] eq $piece);
569 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
572 # and same for columns
575 next unless ($board->[$row][$from_col] eq $piece);
576 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
579 # see if we need to disambiguate
580 if ($num_total > 1) {
582 $pretty .= substr($move, 0, 1);
583 } elsif ($num_row == 1) {
584 $pretty .= substr($move, 1, 1);
586 $pretty .= substr($move, 0, 2);
591 if ($board->[$to_row][$to_col] ne '-') {
595 $pretty .= _pos_to_square($to_row, $to_col);