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 # Convert to Chess960 king-takes-rook.
47 $to_col = 7 if ($move eq 'e1g1');
48 $to_col = 0 if ($move eq 'e1c1');
50 my $dst_piece = $board->[$to_row][$to_col];
52 # White short castling.
53 if ($dst_piece eq 'R' && $to_col > $from_col) {
55 $nb->[7][$from_col] = '-';
56 $nb->[7][$to_col] = '-';
63 # Black short castling.
64 if ($dst_piece eq 'R' && $to_col < $from_col) {
65 $nb->[7][$from_col] = '-';
66 $nb->[7][$to_col] = '-';
72 } elsif ($piece eq 'k') {
73 # Convert to Chess960 king-takes-rook.
74 $to_col = 7 if ($move eq 'e8g8');
75 $to_col = 0 if ($move eq 'e8c8');
77 my $dst_piece = $board->[$to_row][$to_col];
79 # Black short castling.
80 if ($dst_piece eq 'r' && $to_col > $from_col) {
81 $nb->[0][$from_col] = '-';
82 $nb->[0][$to_col] = '-';
89 # Black long castling.
90 if ($dst_piece eq 'r' && $to_col < $from_col) {
92 $nb->[0][$from_col] = '-';
93 $nb->[0][$to_col] = '-';
101 # check if the from-piece is a pawn
102 if (lc($piece) eq 'p') {
104 if ($from_col != $to_col) {
106 if ($board->[$to_row][$to_col] eq '-') {
108 $nb->[$to_row - 1][$to_col] = '-';
110 $nb->[$to_row + 1][$to_col] = '-';
114 if (defined($promo) && $promo ne '') {
124 $nb->[$from_row][$from_col] = '-';
125 $nb->[$to_row][$to_col] = $piece;
131 my ($row, $col) = @_;
132 return sprintf("%c%d", ord('a') + $col, 8 - $row);
135 sub _col_letter_to_num {
136 return ord(shift) - ord('a');
139 sub _row_letter_to_num {
140 return 7 - (ord(shift) - ord('1'));
147 #$square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
148 $square =~ /^([a-h])([1-8])$/ or Carp::confess("Invalid square $square");
149 return (_row_letter_to_num($2), _col_letter_to_num($1));
152 sub move_to_uci_notation {
153 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
155 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
158 sub _find_piece_col {
159 my ($row, $piece) = @_;
161 return $col if ($row->[$col] eq $piece);
163 die "Could not find piece $piece";
166 # Note: This is in general not a validation that the move is actually allowed
167 # (e.g. you can castle even though you're in check).
168 sub parse_pretty_move {
169 my ($board, $move, $toplay, $chess960, $white_castle_k, $white_castle_q, $black_castle_k, $black_castle_q) = @_;
171 # Strip check or mate
174 if ($move eq '0-0' or $move eq 'O-O') {
175 if ($toplay eq 'W') {
178 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_k . '1'));
180 return (_square_to_pos('e1'), _square_to_pos('g1'));
185 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_k . '8'));
187 return (_square_to_pos('e8'), _square_to_pos('g8'));
190 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
191 if ($toplay eq 'W') {
194 return (7, _find_piece_col($board->[7], 'K'), _square_to_pos($white_castle_q . '1'));
196 return (_square_to_pos('e1'), _square_to_pos('c1'));
201 return (0, _find_piece_col($board->[0], 'k'), _square_to_pos($black_castle_q . '8'));
203 return (_square_to_pos('e8'), _square_to_pos('c8'));
210 if ($move =~ s/=?([QRNB])$//) {
214 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
216 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
217 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
218 if (!defined($piece) && (!defined($from_col) || !defined($from_row))) {
221 my ($to_row, $to_col) = _square_to_pos($4);
223 # Find all possible from-squares that could have been meant.
226 if ($toplay eq 'B') {
227 $piece = lc($piece) if defined($piece);
231 next if (defined($from_row) && $from_row != $row);
233 next if (defined($from_col) && $from_col != $col);
234 next if (defined($piece) && $board->[$row][$col] ne $piece);
235 push @squares, [ $row, $col ];
238 if (scalar @squares > 1) {
239 # Filter out pieces which cannot reach this square.
240 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
242 if (scalar @squares > 1) {
243 # See if doing this move would put us in check
244 # (yes, there are clients that expect us to do this).
245 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
247 if (scalar @squares == 0) {
248 die "Impossible move $move";
250 if (scalar @squares != 1) {
251 die "Ambigious move $move";
253 return (@{$squares[0]}, $to_row, $to_col, $promo);
260 my $str = join('', @{$board->[$row]});
261 $str =~ s/(-+)/length($1)/ge;
265 return join('/', @rows);
269 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
271 # Can't eat your own piece (Chess960 uses king-takes-rook for castling,
272 # but castling is irrelevant for reachability)
273 my $dest_piece = $board->[$to_row][$to_col];
274 if ($dest_piece ne '-') {
275 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
280 if ($to_col == $from_col && $to_row == $from_row + 1) {
281 return ($dest_piece eq '-');
283 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
284 my $middle_piece = $board->[2][$to_col];
285 return ($dest_piece eq '-' && $middle_piece eq '-');
287 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
288 if ($dest_piece eq '-') {
289 # En passant. TODO: check that the last move was indeed an EP move
290 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
299 if ($to_col == $from_col && $to_row == $from_row - 1) {
300 return ($dest_piece eq '-');
302 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
303 my $middle_piece = $board->[5][$to_col];
304 return ($dest_piece eq '-' && $middle_piece eq '-');
306 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
307 if ($dest_piece eq '-') {
308 # En passant. TODO: check that the last move was indeed an EP move
309 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
317 if (lc($piece) eq 'r') {
318 return 0 unless ($from_row == $to_row || $from_col == $to_col);
320 # check that there's a clear passage
321 if ($from_row == $to_row) {
322 if ($from_col > $to_col) {
323 ($to_col, $from_col) = ($from_col, $to_col);
326 for my $c (($from_col+1)..($to_col-1)) {
327 my $middle_piece = $board->[$to_row][$c];
328 return 0 if ($middle_piece ne '-');
333 if ($from_row > $to_row) {
334 ($to_row, $from_row) = ($from_row, $to_row);
337 for my $r (($from_row+1)..($to_row-1)) {
338 my $middle_piece = $board->[$r][$to_col];
339 return 0 if ($middle_piece ne '-');
345 if (lc($piece) eq 'b') {
346 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
348 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
349 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
351 my $r = $from_row + $dr;
352 my $c = $from_col + $dc;
354 while ($r != $to_row) {
355 my $middle_piece = $board->[$r][$c];
356 return 0 if ($middle_piece ne '-');
364 if (lc($piece) eq 'n') {
365 my $diff_r = abs($from_row - $to_row);
366 my $diff_c = abs($from_col - $to_col);
367 return 1 if ($diff_r == 2 && $diff_c == 1);
368 return 1 if ($diff_r == 1 && $diff_c == 2);
372 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
373 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
376 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
377 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
379 if (lc($piece) eq 'k') {
380 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
387 # Like can_reach, but also checks the move doesn't put the side in check.
388 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
389 # needs moves to be in minimally disambiguated form.
390 sub can_legally_reach {
391 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
393 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
395 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
396 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
398 return !in_check($nb, $side);
401 my %pieces_against_side = (
402 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
403 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
406 # Returns whether the given side (given as k or K for black and white) is in check.
408 my ($board, $side) = @_;
409 my ($kr, $kc) = _find_piece($board, $side);
411 # check all pieces for the possibility of threatening this king
413 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
415 my $piece = $board->[$row][$col];
416 next if ($piece eq '-');
417 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
425 my ($board, $piece) = @_;
428 next unless grep { $_ eq $piece } @{$board->[$row]};
430 if ($board->[$row][$col] eq $piece) {
436 return (undef, undef);
439 # Returns if the given side (given as k or K) is in mate.
441 my ($board, $side, $in_check) = @_;
442 return 0 if (!$in_check);
444 # try all possible moves for the side in check
447 my $piece = $board->[$row][$col];
448 next if ($piece eq '-');
451 next if ($piece eq lc($piece));
453 next if ($piece eq uc($piece));
456 for my $dest_row (0..7) {
457 for my $dest_col (0..7) {
458 next if ($row == $dest_row && $col == $dest_col);
459 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
461 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
462 return 0 if (!$nb->in_check($side));
468 # nothing to do; mate
472 # Returns the short algebraic form of the move, as well as the new position.
473 sub prettyprint_move {
474 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
475 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
477 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
479 my $piece = $board->[$from_row][$from_col];
480 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
481 my $in_check = $nb->in_check($other_side);
482 if ($nb->in_mate($other_side, $in_check)) {
484 } elsif ($in_check) {
487 return ($pretty, $nb);
496 my $piece = $board->[$row][$col];
497 ++$num if ($piece ne '-');
503 sub _prettyprint_move_no_check_or_mate {
504 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
505 my $piece = $board->[$from_row][$from_col];
506 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
509 die "Invalid move $move";
513 # white short/long castling
514 return 'O-O' if ($move eq 'e1g1');
515 return 'O-O-O' if ($move eq 'e1c1');
517 # white short/long chess960-style castling (king takes own rook)
518 my $dst_piece = $board->[$to_row][$to_col];
519 if ($dst_piece eq 'R') {
520 return ($to_col > $from_col) ? 'O-O' : 'O-O-O';
522 } elsif ($piece eq 'k') {
523 # black short/long castling
524 return 'O-O' if ($move eq 'e8g8');
525 return 'O-O-O' if ($move eq 'e8c8');
527 # black short/long chess960-style castling (king takes own rook)
528 my $dst_piece = $board->[$to_row][$to_col];
529 if ($dst_piece eq 'r') {
530 return ($to_col > $from_col) ? 'O-O' : 'O-O-O';
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);