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";
180 my $piece = $1 // 'P';
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 my ($to_row, $to_col) = _square_to_pos($4);
185 # Find all possible from-squares that could have been meant.
188 if ($toplay eq 'B') {
193 next if (defined($from_row) && $from_row != $row);
195 next if (defined($from_col) && $from_col != $col);
196 next if ($board->[$row][$col] ne $piece);
197 push @squares, [ $row, $col ];
200 if (scalar @squares > 1) {
201 # Filter out pieces which cannot reach this square.
202 @squares = grep { $board->can_reach($piece, $_->[0], $_->[1], $to_row, $to_col) } @squares;
204 if (scalar @squares > 1) {
205 # See if doing this move would put us in check
206 # (yes, there are clients that expect us to do this).
207 @squares = grep { !$board->make_move($_->[0], $_->[1], $to_row, $to_col, $promo)->in_check($side) } @squares;
209 if (scalar @squares == 0) {
210 die "Impossible move $move";
212 if (scalar @squares != 1) {
213 die "Ambigious move $move";
215 return (@{$squares[0]}, $to_row, $to_col, $promo);
222 my $str = join('', @{$board->[$row]});
223 $str =~ s/(-+)/length($1)/ge;
227 return join('/', @rows);
230 # Returns a compact bit string describing the same data as fen().
231 # This is encoded using a Huffman-like encoding, and should be
232 # typically about 1/3 the number of bytes.
239 my $piece = $board->[$row][$col];
245 my $color = (lc($piece) eq $piece) ? 0 : 1;
246 $bits .= "1" . $color;
248 if (lc($piece) eq 'p') {
250 } elsif (lc($piece) eq 'n') {
252 } elsif (lc($piece) eq 'b') {
254 } elsif (lc($piece) eq 'r') {
256 } elsif (lc($piece) eq 'q') {
258 } elsif (lc($piece) eq 'k') {
261 die "Unknown piece $piece";
266 return pack('b*', $bits);
270 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
272 # can't eat your own piece
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 my %pieces_against_side = (
388 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
389 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
392 # Returns whether the given side (given as k or K for black and white) is in check.
394 my ($board, $side) = @_;
395 my ($kr, $kc) = _find_piece($board, $side);
397 # check all pieces for the possibility of threatening this king
399 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
401 my $piece = $board->[$row][$col];
402 next if ($piece eq '-');
403 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
411 my ($board, $piece) = @_;
414 next unless grep { $_ eq $piece } @{$board->[$row]};
416 if ($board->[$row][$col] eq $piece) {
422 return (undef, undef);
425 # Returns if the given side (given as k or K) is in mate.
427 my ($board, $side, $in_check) = @_;
428 return 0 if (!$in_check);
430 # try all possible moves for the side in check
433 my $piece = $board->[$row][$col];
434 next if ($piece eq '-');
437 next if ($piece eq lc($piece));
439 next if ($piece eq uc($piece));
442 for my $dest_row (0..7) {
443 for my $dest_col (0..7) {
444 next if ($row == $dest_row && $col == $dest_col);
445 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
447 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
448 return 0 if (!$nb->in_check($side));
454 # nothing to do; mate
458 # Returns the short algebraic form of the move, as well as the new position.
459 sub prettyprint_move {
460 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
461 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
463 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
465 my $piece = $board->[$from_row][$from_col];
466 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
467 my $in_check = $nb->in_check($other_side);
468 if ($nb->in_mate($other_side, $in_check)) {
470 } elsif ($in_check) {
473 return ($pretty, $nb);
482 my $piece = $board->[$row][$col];
483 ++$num if ($piece ne '-');
489 sub _prettyprint_move_no_check_or_mate {
490 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
491 my $piece = $board->[$from_row][$from_col];
492 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
495 die "Invalid move $move";
498 # white short castling
499 if ($move eq 'e1g1' && $piece eq 'K') {
503 # white long castling
504 if ($move eq 'e1c1' && $piece eq 'K') {
508 # black short castling
509 if ($move eq 'e8g8' && $piece eq 'k') {
513 # black long castling
514 if ($move eq 'e8c8' && $piece eq 'k') {
520 # check if the from-piece is a pawn
521 if (lc($piece) eq 'p') {
523 if ($from_col != $to_col) {
524 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
526 $pretty = _pos_to_square($to_row, $to_col);
528 if (defined($promo) && $promo ne '') {
531 $pretty .= uc($promo);
537 $pretty = uc($piece);
539 # see how many of these pieces could go here, in all
543 next unless ($board->[$row][$col] eq $piece);
544 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
548 # see how many of these pieces from the given row could go here
551 next unless ($board->[$from_row][$col] eq $piece);
552 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
555 # and same for columns
558 next unless ($board->[$row][$from_col] eq $piece);
559 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
562 # see if we need to disambiguate
563 if ($num_total > 1) {
565 $pretty .= substr($move, 0, 1);
566 } elsif ($num_row == 1) {
567 $pretty .= substr($move, 1, 1);
569 $pretty .= substr($move, 0, 2);
574 if ($board->[$to_row][$to_col] ne '-') {
578 $pretty .= _pos_to_square($to_row, $to_col);