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') {
51 $nb->[7][$from_col] = '-';
55 $nb->[7][$to_col] = '-';
61 # White long castling (regular or Chess960 king-takes-rook)
62 if ($move eq 'e1c1' || $dst_piece eq 'R') {
64 $nb->[7][$from_col] = '-';
68 $nb->[7][$to_col] = '-';
73 } elsif ($piece eq 'k') {
74 my $dst_piece = $board->[$to_row][$to_col];
76 # Black short castling (regular or Chess960 king-takes-rook)
77 if ($move eq 'e8g8' || $dst_piece eq 'r') {
79 $nb->[0][$from_col] = '-';
83 $nb->[0][$from_col] = '-';
90 if ($move eq 'e8c8' || $dst_piece eq 'r') {
92 $nb->[0][$from_col] = '-';
96 $nb->[0][$to_col] = '-';
103 # check if the from-piece is a pawn
104 if (lc($piece) eq 'p') {
106 if ($from_col != $to_col) {
108 if ($board->[$to_row][$to_col] eq '-') {
110 $nb->[$to_row - 1][$to_col] = '-';
112 $nb->[$to_row + 1][$to_col] = '-';
116 if (defined($promo) && $promo ne '') {
126 $nb->[$from_row][$from_col] = '-';
127 $nb->[$to_row][$to_col] = $piece;
133 my ($row, $col) = @_;
134 return sprintf("%c%d", ord('a') + $col, 8 - $row);
137 sub _col_letter_to_num {
138 return ord(shift) - ord('a');
141 sub _row_letter_to_num {
142 return 7 - (ord(shift) - ord('1'));
149 #$square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
150 $square =~ /^([a-h])([1-8])$/ or Carp::confess("Invalid square $square");
151 return (_row_letter_to_num($2), _col_letter_to_num($1));
154 sub move_to_uci_notation {
155 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
157 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
160 sub _find_piece_col {
161 my ($row, $piece) = @_;
163 return $col if ($row->[$col] eq $piece);
165 die "Could not find piece $piece";
168 # Note: This is in general not a validation that the move is actually allowed
169 # (e.g. you can castle even though you're in check).
170 sub parse_pretty_move {
171 my ($board, $move, $toplay, $chess960, $white_castle_k, $white_castle_q, $black_castle_k, $black_castle_q) = @_;
173 # Strip check or mate
176 if ($move eq '0-0' or $move eq 'O-O') {
177 if ($toplay eq 'W') {
180 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_k . '1'));
182 return (_square_to_pos('e1'), _square_to_pos('g1'));
187 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_k . '8'));
189 return (_square_to_pos('e8'), _square_to_pos('g8'));
192 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
193 if ($toplay eq 'W') {
196 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_q . '1'));
198 return (_square_to_pos('e1'), _square_to_pos('c1'));
203 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_q . '8'));
205 return (_square_to_pos('e8'), _square_to_pos('c8'));
212 if ($move =~ s/=?([QRNB])$//) {
216 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
218 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
219 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
220 if (!defined($piece) && (!defined($from_col) || !defined($from_row))) {
223 my ($to_row, $to_col) = _square_to_pos($4);
225 # Find all possible from-squares that could have been meant.
228 if ($toplay eq 'B') {
229 $piece = lc($piece) if defined($piece);
233 next if (defined($from_row) && $from_row != $row);
235 next if (defined($from_col) && $from_col != $col);
236 next if (defined($piece) && $board->[$row][$col] ne $piece);
237 push @squares, [ $row, $col ];
240 if (scalar @squares > 1) {
241 # Filter out pieces which cannot reach this square.
242 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
244 if (scalar @squares > 1) {
245 # See if doing this move would put us in check
246 # (yes, there are clients that expect us to do this).
247 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
249 if (scalar @squares == 0) {
250 die "Impossible move $move";
252 if (scalar @squares != 1) {
253 die "Ambigious move $move";
255 return (@{$squares[0]}, $to_row, $to_col, $promo);
262 my $str = join('', @{$board->[$row]});
263 $str =~ s/(-+)/length($1)/ge;
267 return join('/', @rows);
271 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
273 # Can't eat your own piece (Chess960 uses king-takes-rook for castling,
274 # but castling is irrelevant for reachability)
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";
515 # white short/long castling
516 return 'O-O' if ($move eq 'e1g1');
517 return 'O-O-O' if ($move eq 'e1c1');
519 # white 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';
524 } elsif ($piece eq 'k') {
525 # black short/long castling
526 return 'O-O' if ($move eq 'e8g8');
527 return 'O-O-O' if ($move eq 'e8c8');
529 # black short/long chess960-style castling (king takes own rook)
530 my $dst_piece = $board->[$to_row][$to_col];
531 if ($dst_piece eq 'r') {
532 return ($to_col > $from_col) ? 'O-O' : 'O-O-O';
538 # check if the from-piece is a pawn
539 if (lc($piece) eq 'p') {
541 if ($from_col != $to_col) {
542 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
544 $pretty = _pos_to_square($to_row, $to_col);
547 if (defined($promo) && $promo ne '') {
550 $pretty .= uc($promo);
555 $pretty = uc($piece);
557 # see how many of these pieces could go here, in all
561 next unless ($board->[$row][$col] eq $piece);
562 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
566 # see how many of these pieces from the given row could go here
569 next unless ($board->[$from_row][$col] eq $piece);
570 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
573 # and same for columns
576 next unless ($board->[$row][$from_col] eq $piece);
577 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
580 # see if we need to disambiguate
581 if ($num_total > 1) {
583 $pretty .= substr($move, 0, 1);
584 } elsif ($num_row == 1) {
585 $pretty .= substr($move, 1, 1);
587 $pretty .= substr($move, 0, 2);
592 if ($board->[$to_row][$to_col] ne '-') {
596 $pretty .= _pos_to_square($to_row, $to_col);