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";
46 my $dst_piece = $board->[$to_row][$to_col];
48 # White short castling (regular or Chess960 king-takes-rook)
49 if ($move eq 'e1g1' || ($dst_piece eq 'R' && $to_col > $from_col)) {
51 $nb->[7][$from_col] = '-';
52 $nb->[7][$to_col] = '-';
59 # White long castling (regular or Chess960 king-takes-rook)
60 if ($move eq 'e1c1' || ($dst_piece eq 'R' && $to_col < $from_col)) {
61 $nb->[7][$from_col] = '-';
62 $nb->[7][$to_col] = '-';
68 } elsif ($piece eq 'k') {
69 my $dst_piece = $board->[$to_row][$to_col];
71 # Black short castling (regular or Chess960 king-takes-rook)
72 if ($move eq 'e8g8' || ($dst_piece eq 'r' && $to_col > $from_col)) {
73 $nb->[0][$from_col] = '-';
74 $nb->[0][$to_col] = '-';
82 if ($move eq 'e8c8' || ($dst_piece eq 'r' && $to_col < $from_col)) {
84 $nb->[0][$from_col] = '-';
85 $nb->[0][$to_col] = '-';
93 # check if the from-piece is a pawn
94 if (lc($piece) eq 'p') {
96 if ($from_col != $to_col) {
98 if ($board->[$to_row][$to_col] eq '-') {
100 $nb->[$to_row - 1][$to_col] = '-';
102 $nb->[$to_row + 1][$to_col] = '-';
106 if (defined($promo) && $promo ne '') {
116 $nb->[$from_row][$from_col] = '-';
117 $nb->[$to_row][$to_col] = $piece;
123 my ($row, $col) = @_;
124 return sprintf("%c%d", ord('a') + $col, 8 - $row);
127 sub _col_letter_to_num {
128 return ord(shift) - ord('a');
131 sub _row_letter_to_num {
132 return 7 - (ord(shift) - ord('1'));
139 #$square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
140 $square =~ /^([a-h])([1-8])$/ or Carp::confess("Invalid square $square");
141 return (_row_letter_to_num($2), _col_letter_to_num($1));
144 sub move_to_uci_notation {
145 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
147 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
150 sub _find_piece_col {
151 my ($row, $piece) = @_;
153 return $col if ($row->[$col] eq $piece);
155 die "Could not find piece $piece";
158 # Note: This is in general not a validation that the move is actually allowed
159 # (e.g. you can castle even though you're in check).
160 sub parse_pretty_move {
161 my ($board, $move, $toplay, $chess960, $white_castle_k, $white_castle_q, $black_castle_k, $black_castle_q) = @_;
163 # Strip check or mate
166 if ($move eq '0-0' or $move eq 'O-O') {
167 if ($toplay eq 'W') {
170 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_k . '1'));
172 return (_square_to_pos('e1'), _square_to_pos('g1'));
177 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_k . '8'));
179 return (_square_to_pos('e8'), _square_to_pos('g8'));
182 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
183 if ($toplay eq 'W') {
186 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_q . '1'));
188 return (_square_to_pos('e1'), _square_to_pos('c1'));
193 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_q . '8'));
195 return (_square_to_pos('e8'), _square_to_pos('c8'));
202 if ($move =~ s/=?([QRNB])$//) {
206 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
208 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
209 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
210 if (!defined($piece) && (!defined($from_col) || !defined($from_row))) {
213 my ($to_row, $to_col) = _square_to_pos($4);
215 # Find all possible from-squares that could have been meant.
218 if ($toplay eq 'B') {
219 $piece = lc($piece) if defined($piece);
223 next if (defined($from_row) && $from_row != $row);
225 next if (defined($from_col) && $from_col != $col);
226 next if (defined($piece) && $board->[$row][$col] ne $piece);
227 push @squares, [ $row, $col ];
230 if (scalar @squares > 1) {
231 # Filter out pieces which cannot reach this square.
232 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
234 if (scalar @squares > 1) {
235 # See if doing this move would put us in check
236 # (yes, there are clients that expect us to do this).
237 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
239 if (scalar @squares == 0) {
240 die "Impossible move $move";
242 if (scalar @squares != 1) {
243 die "Ambigious move $move";
245 return (@{$squares[0]}, $to_row, $to_col, $promo);
252 my $str = join('', @{$board->[$row]});
253 $str =~ s/(-+)/length($1)/ge;
257 return join('/', @rows);
261 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
263 # Can't eat your own piece (Chess960 uses king-takes-rook for castling,
264 # but castling is irrelevant for reachability)
265 my $dest_piece = $board->[$to_row][$to_col];
266 if ($dest_piece ne '-') {
267 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
272 if ($to_col == $from_col && $to_row == $from_row + 1) {
273 return ($dest_piece eq '-');
275 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
276 my $middle_piece = $board->[2][$to_col];
277 return ($dest_piece eq '-' && $middle_piece eq '-');
279 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
280 if ($dest_piece eq '-') {
281 # En passant. TODO: check that the last move was indeed an EP move
282 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
291 if ($to_col == $from_col && $to_row == $from_row - 1) {
292 return ($dest_piece eq '-');
294 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
295 my $middle_piece = $board->[5][$to_col];
296 return ($dest_piece eq '-' && $middle_piece eq '-');
298 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
299 if ($dest_piece eq '-') {
300 # En passant. TODO: check that the last move was indeed an EP move
301 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
309 if (lc($piece) eq 'r') {
310 return 0 unless ($from_row == $to_row || $from_col == $to_col);
312 # check that there's a clear passage
313 if ($from_row == $to_row) {
314 if ($from_col > $to_col) {
315 ($to_col, $from_col) = ($from_col, $to_col);
318 for my $c (($from_col+1)..($to_col-1)) {
319 my $middle_piece = $board->[$to_row][$c];
320 return 0 if ($middle_piece ne '-');
325 if ($from_row > $to_row) {
326 ($to_row, $from_row) = ($from_row, $to_row);
329 for my $r (($from_row+1)..($to_row-1)) {
330 my $middle_piece = $board->[$r][$to_col];
331 return 0 if ($middle_piece ne '-');
337 if (lc($piece) eq 'b') {
338 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
340 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
341 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
343 my $r = $from_row + $dr;
344 my $c = $from_col + $dc;
346 while ($r != $to_row) {
347 my $middle_piece = $board->[$r][$c];
348 return 0 if ($middle_piece ne '-');
356 if (lc($piece) eq 'n') {
357 my $diff_r = abs($from_row - $to_row);
358 my $diff_c = abs($from_col - $to_col);
359 return 1 if ($diff_r == 2 && $diff_c == 1);
360 return 1 if ($diff_r == 1 && $diff_c == 2);
364 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
365 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
368 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
369 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
371 if (lc($piece) eq 'k') {
372 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
379 # Like can_reach, but also checks the move doesn't put the side in check.
380 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
381 # needs moves to be in minimally disambiguated form.
382 sub can_legally_reach {
383 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
385 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
387 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
388 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
390 return !in_check($nb, $side);
393 my %pieces_against_side = (
394 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
395 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
398 # Returns whether the given side (given as k or K for black and white) is in check.
400 my ($board, $side) = @_;
401 my ($kr, $kc) = _find_piece($board, $side);
403 # check all pieces for the possibility of threatening this king
405 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
407 my $piece = $board->[$row][$col];
408 next if ($piece eq '-');
409 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
417 my ($board, $piece) = @_;
420 next unless grep { $_ eq $piece } @{$board->[$row]};
422 if ($board->[$row][$col] eq $piece) {
428 return (undef, undef);
431 # Returns if the given side (given as k or K) is in mate.
433 my ($board, $side, $in_check) = @_;
434 return 0 if (!$in_check);
436 # try all possible moves for the side in check
439 my $piece = $board->[$row][$col];
440 next if ($piece eq '-');
443 next if ($piece eq lc($piece));
445 next if ($piece eq uc($piece));
448 for my $dest_row (0..7) {
449 for my $dest_col (0..7) {
450 next if ($row == $dest_row && $col == $dest_col);
451 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
453 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
454 return 0 if (!$nb->in_check($side));
460 # nothing to do; mate
464 # Returns the short algebraic form of the move, as well as the new position.
465 sub prettyprint_move {
466 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
467 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
469 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
471 my $piece = $board->[$from_row][$from_col];
472 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
473 my $in_check = $nb->in_check($other_side);
474 if ($nb->in_mate($other_side, $in_check)) {
476 } elsif ($in_check) {
479 return ($pretty, $nb);
488 my $piece = $board->[$row][$col];
489 ++$num if ($piece ne '-');
495 sub _prettyprint_move_no_check_or_mate {
496 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
497 my $piece = $board->[$from_row][$from_col];
498 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
501 die "Invalid move $move";
505 # white short/long castling
506 return 'O-O' if ($move eq 'e1g1');
507 return 'O-O-O' if ($move eq 'e1c1');
509 # white short/long chess960-style castling (king takes own rook)
510 my $dst_piece = $board->[$to_row][$to_col];
511 if ($dst_piece eq 'R') {
512 return ($to_col > $from_col) ? 'O-O' : 'O-O-O';
514 } elsif ($piece eq 'k') {
515 # black short/long castling
516 return 'O-O' if ($move eq 'e8g8');
517 return 'O-O-O' if ($move eq 'e8c8');
519 # black short/long chess960-style castling (king takes own rook)
520 my $dst_piece = $board->[$to_row][$to_col];
521 if ($dst_piece eq 'r') {
522 return ($to_col > $from_col) ? 'O-O' : 'O-O-O';
528 # check if the from-piece is a pawn
529 if (lc($piece) eq 'p') {
531 if ($from_col != $to_col) {
532 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
534 $pretty = _pos_to_square($to_row, $to_col);
537 if (defined($promo) && $promo ne '') {
540 $pretty .= uc($promo);
545 $pretty = uc($piece);
547 # see how many of these pieces could go here, in all
551 next unless ($board->[$row][$col] eq $piece);
552 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
556 # see how many of these pieces from the given row could go here
559 next unless ($board->[$from_row][$col] eq $piece);
560 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
563 # and same for columns
566 next unless ($board->[$row][$from_col] eq $piece);
567 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
570 # see if we need to disambiguate
571 if ($num_total > 1) {
573 $pretty .= substr($move, 0, 1);
574 } elsif ($num_row == 1) {
575 $pretty .= substr($move, 1, 1);
577 $pretty .= substr($move, 0, 2);
582 if ($board->[$to_row][$to_col] ne '-') {
586 $pretty .= _pos_to_square($to_row, $to_col);