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);
234 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
236 # can't eat your own piece
237 my $dest_piece = $board->[$to_row][$to_col];
238 if ($dest_piece ne '-') {
239 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
244 if ($to_col == $from_col && $to_row == $from_row + 1) {
245 return ($dest_piece eq '-');
247 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
248 my $middle_piece = $board->[2][$to_col];
249 return ($dest_piece eq '-' && $middle_piece eq '-');
251 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
252 if ($dest_piece eq '-') {
253 # En passant. TODO: check that the last move was indeed an EP move
254 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
263 if ($to_col == $from_col && $to_row == $from_row - 1) {
264 return ($dest_piece eq '-');
266 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
267 my $middle_piece = $board->[5][$to_col];
268 return ($dest_piece eq '-' && $middle_piece eq '-');
270 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
271 if ($dest_piece eq '-') {
272 # En passant. TODO: check that the last move was indeed an EP move
273 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
281 if (lc($piece) eq 'r') {
282 return 0 unless ($from_row == $to_row || $from_col == $to_col);
284 # check that there's a clear passage
285 if ($from_row == $to_row) {
286 if ($from_col > $to_col) {
287 ($to_col, $from_col) = ($from_col, $to_col);
290 for my $c (($from_col+1)..($to_col-1)) {
291 my $middle_piece = $board->[$to_row][$c];
292 return 0 if ($middle_piece ne '-');
297 if ($from_row > $to_row) {
298 ($to_row, $from_row) = ($from_row, $to_row);
301 for my $r (($from_row+1)..($to_row-1)) {
302 my $middle_piece = $board->[$r][$to_col];
303 return 0 if ($middle_piece ne '-');
309 if (lc($piece) eq 'b') {
310 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
312 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
313 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
315 my $r = $from_row + $dr;
316 my $c = $from_col + $dc;
318 while ($r != $to_row) {
319 my $middle_piece = $board->[$r][$c];
320 return 0 if ($middle_piece ne '-');
328 if (lc($piece) eq 'n') {
329 my $diff_r = abs($from_row - $to_row);
330 my $diff_c = abs($from_col - $to_col);
331 return 1 if ($diff_r == 2 && $diff_c == 1);
332 return 1 if ($diff_r == 1 && $diff_c == 2);
336 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
337 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
340 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
341 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
343 if (lc($piece) eq 'k') {
344 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
351 # Like can_reach, but also checks the move doesn't put the side in check.
352 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
353 # needs moves to be in minimally disambiguated form.
354 sub can_legally_reach {
355 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
357 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
359 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
360 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
362 return !in_check($nb, $side);
365 my %pieces_against_side = (
366 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
367 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
370 # Returns whether the given side (given as k or K for black and white) is in check.
372 my ($board, $side) = @_;
373 my ($kr, $kc) = _find_piece($board, $side);
375 # check all pieces for the possibility of threatening this king
377 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
379 my $piece = $board->[$row][$col];
380 next if ($piece eq '-');
381 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
389 my ($board, $piece) = @_;
392 next unless grep { $_ eq $piece } @{$board->[$row]};
394 if ($board->[$row][$col] eq $piece) {
400 return (undef, undef);
403 # Returns if the given side (given as k or K) is in mate.
405 my ($board, $side, $in_check) = @_;
406 return 0 if (!$in_check);
408 # try all possible moves for the side in check
411 my $piece = $board->[$row][$col];
412 next if ($piece eq '-');
415 next if ($piece eq lc($piece));
417 next if ($piece eq uc($piece));
420 for my $dest_row (0..7) {
421 for my $dest_col (0..7) {
422 next if ($row == $dest_row && $col == $dest_col);
423 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
425 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
426 return 0 if (!$nb->in_check($side));
432 # nothing to do; mate
436 # Returns the short algebraic form of the move, as well as the new position.
437 sub prettyprint_move {
438 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
439 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
441 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
443 my $piece = $board->[$from_row][$from_col];
444 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
445 my $in_check = $nb->in_check($other_side);
446 if ($nb->in_mate($other_side, $in_check)) {
448 } elsif ($in_check) {
451 return ($pretty, $nb);
460 my $piece = $board->[$row][$col];
461 ++$num if ($piece ne '-');
467 sub _prettyprint_move_no_check_or_mate {
468 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
469 my $piece = $board->[$from_row][$from_col];
470 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
473 die "Invalid move $move";
476 # white short castling
477 if ($move eq 'e1g1' && $piece eq 'K') {
481 # white long castling
482 if ($move eq 'e1c1' && $piece eq 'K') {
486 # black short castling
487 if ($move eq 'e8g8' && $piece eq 'k') {
491 # black long castling
492 if ($move eq 'e8c8' && $piece eq 'k') {
498 # check if the from-piece is a pawn
499 if (lc($piece) eq 'p') {
501 if ($from_col != $to_col) {
502 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
504 $pretty = _pos_to_square($to_row, $to_col);
507 if (defined($promo) && $promo ne '') {
510 $pretty .= uc($promo);
515 $pretty = uc($piece);
517 # see how many of these pieces could go here, in all
521 next unless ($board->[$row][$col] eq $piece);
522 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
526 # see how many of these pieces from the given row could go here
529 next unless ($board->[$from_row][$col] eq $piece);
530 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
533 # and same for columns
536 next unless ($board->[$row][$from_col] eq $piece);
537 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
540 # see if we need to disambiguate
541 if ($num_total > 1) {
543 $pretty .= substr($move, 0, 1);
544 } elsif ($num_row == 1) {
545 $pretty .= substr($move, 1, 1);
547 $pretty .= substr($move, 0, 2);
552 if ($board->[$to_row][$to_col] ne '-') {
556 $pretty .= _pos_to_square($to_row, $to_col);