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.
237 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
239 # can't eat your own piece
240 my $dest_piece = $board->[$to_row][$to_col];
241 if ($dest_piece ne '-') {
242 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
247 if ($to_col == $from_col && $to_row == $from_row + 1) {
248 return ($dest_piece eq '-');
250 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
251 my $middle_piece = $board->[2][$to_col];
252 return ($dest_piece eq '-' && $middle_piece eq '-');
254 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
255 if ($dest_piece eq '-') {
256 # En passant. TODO: check that the last move was indeed an EP move
257 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
266 if ($to_col == $from_col && $to_row == $from_row - 1) {
267 return ($dest_piece eq '-');
269 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
270 my $middle_piece = $board->[5][$to_col];
271 return ($dest_piece eq '-' && $middle_piece eq '-');
273 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
274 if ($dest_piece eq '-') {
275 # En passant. TODO: check that the last move was indeed an EP move
276 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
284 if (lc($piece) eq 'r') {
285 return 0 unless ($from_row == $to_row || $from_col == $to_col);
287 # check that there's a clear passage
288 if ($from_row == $to_row) {
289 if ($from_col > $to_col) {
290 ($to_col, $from_col) = ($from_col, $to_col);
293 for my $c (($from_col+1)..($to_col-1)) {
294 my $middle_piece = $board->[$to_row][$c];
295 return 0 if ($middle_piece ne '-');
300 if ($from_row > $to_row) {
301 ($to_row, $from_row) = ($from_row, $to_row);
304 for my $r (($from_row+1)..($to_row-1)) {
305 my $middle_piece = $board->[$r][$to_col];
306 return 0 if ($middle_piece ne '-');
312 if (lc($piece) eq 'b') {
313 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
315 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
316 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
318 my $r = $from_row + $dr;
319 my $c = $from_col + $dc;
321 while ($r != $to_row) {
322 my $middle_piece = $board->[$r][$c];
323 return 0 if ($middle_piece ne '-');
331 if (lc($piece) eq 'n') {
332 my $diff_r = abs($from_row - $to_row);
333 my $diff_c = abs($from_col - $to_col);
334 return 1 if ($diff_r == 2 && $diff_c == 1);
335 return 1 if ($diff_r == 1 && $diff_c == 2);
339 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
340 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
343 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
344 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
346 if (lc($piece) eq 'k') {
347 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
354 # Like can_reach, but also checks the move doesn't put the side in check.
355 # We use this in prettyprint_move to reduce the disambiguation, because Chess.js
356 # needs moves to be in minimally disambiguated form.
357 sub can_legally_reach {
358 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
360 return 0 if (!can_reach($board, $piece, $from_row, $from_col, $to_row, $to_col));
362 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col);
363 my $side = ($piece eq lc($piece)) ? 'k' : 'K';
365 return !in_check($nb, $side);
368 my %pieces_against_side = (
369 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
370 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
373 # Returns whether the given side (given as k or K for black and white) is in check.
375 my ($board, $side) = @_;
376 my ($kr, $kc) = _find_piece($board, $side);
378 # check all pieces for the possibility of threatening this king
380 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
382 my $piece = $board->[$row][$col];
383 next if ($piece eq '-');
384 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
392 my ($board, $piece) = @_;
395 next unless grep { $_ eq $piece } @{$board->[$row]};
397 if ($board->[$row][$col] eq $piece) {
403 return (undef, undef);
406 # Returns if the given side (given as k or K) is in mate.
408 my ($board, $side, $in_check) = @_;
409 return 0 if (!$in_check);
411 # try all possible moves for the side in check
414 my $piece = $board->[$row][$col];
415 next if ($piece eq '-');
418 next if ($piece eq lc($piece));
420 next if ($piece eq uc($piece));
423 for my $dest_row (0..7) {
424 for my $dest_col (0..7) {
425 next if ($row == $dest_row && $col == $dest_col);
426 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
428 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
429 return 0 if (!$nb->in_check($side));
435 # nothing to do; mate
439 # Returns the short algebraic form of the move, as well as the new position.
440 sub prettyprint_move {
441 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
442 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
444 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
446 my $piece = $board->[$from_row][$from_col];
447 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
448 my $in_check = $nb->in_check($other_side);
449 if ($nb->in_mate($other_side, $in_check)) {
451 } elsif ($in_check) {
454 return ($pretty, $nb);
463 my $piece = $board->[$row][$col];
464 ++$num if ($piece ne '-');
470 sub _prettyprint_move_no_check_or_mate {
471 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
472 my $piece = $board->[$from_row][$from_col];
473 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
476 die "Invalid move $move";
479 # white short castling
480 if ($move eq 'e1g1' && $piece eq 'K') {
484 # white long castling
485 if ($move eq 'e1c1' && $piece eq 'K') {
489 # black short castling
490 if ($move eq 'e8g8' && $piece eq 'k') {
494 # black long castling
495 if ($move eq 'e8c8' && $piece eq 'k') {
501 # check if the from-piece is a pawn
502 if (lc($piece) eq 'p') {
504 if ($from_col != $to_col) {
505 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
507 $pretty = _pos_to_square($to_row, $to_col);
510 if (defined($promo) && $promo ne '') {
513 $pretty .= uc($promo);
518 $pretty = uc($piece);
520 # see how many of these pieces could go here, in all
524 next unless ($board->[$row][$col] eq $piece);
525 ++$num_total if ($board->can_legally_reach($piece, $row, $col, $to_row, $to_col));
529 # see how many of these pieces from the given row could go here
532 next unless ($board->[$from_row][$col] eq $piece);
533 ++$num_row if ($board->can_legally_reach($piece, $from_row, $col, $to_row, $to_col));
536 # and same for columns
539 next unless ($board->[$row][$from_col] eq $piece);
540 ++$num_col if ($board->can_legally_reach($piece, $row, $from_col, $to_row, $to_col));
543 # see if we need to disambiguate
544 if ($num_total > 1) {
546 $pretty .= substr($move, 0, 1);
547 } elsif ($num_row == 1) {
548 $pretty .= substr($move, 1, 1);
550 $pretty .= substr($move, 0, 2);
555 if ($board->[$to_row][$to_col] ne '-') {
559 $pretty .= _pos_to_square($to_row, $to_col);