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 sub parse_pretty_move {
152 my ($board, $move, $toplay) = @_;
154 # Strip check or mate
157 if ($move eq '0-0' or $move eq 'O-O') {
158 if ($toplay eq 'W') {
159 return (_square_to_pos('e1'), _square_to_pos('g1'));
161 return (_square_to_pos('e8'), _square_to_pos('g8'));
163 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
164 if ($toplay eq 'W') {
165 return (_square_to_pos('e1'), _square_to_pos('c1'));
167 return (_square_to_pos('e8'), _square_to_pos('c8'));
173 if ($move =~ s/=?([QRNB])$//) {
177 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
178 my $piece = $1 // 'P';
179 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
180 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
181 my ($to_row, $to_col) = _square_to_pos($4);
183 # Find all possible from-squares that could have been meant.
186 if ($toplay eq 'B') {
191 next if (defined($from_row) && $from_row != $row);
193 next if (defined($from_col) && $from_col != $col);
194 next if ($board->[$row][$col] ne $piece);
195 next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
197 # See if doing this move would put us in check
198 # (yes, there are clients that expect us to do this).
199 next if ($board->make_move($row, $col, $to_row, $to_col, $promo)->in_check($side));
200 push @squares, [ $row, $col ];
203 if (scalar @squares == 0) {
204 die "Impossible move $move";
206 if (scalar @squares != 1) {
207 die "Ambigious move $move";
209 return (@{$squares[0]}, $to_row, $to_col, $promo);
216 my $str = join('', @{$board->[$row]});
217 $str =~ s/(-+)/length($1)/ge;
221 return join('/', @rows);
224 # Returns a compact bit string describing the same data as fen().
225 # This is encoded using a Huffman-like encoding, and should be
226 # typically about 1/3 the number of bytes.
233 my $piece = $board->[$row][$col];
239 my $color = (lc($piece) eq $piece) ? 0 : 1;
240 $bits .= "1" . $color;
242 if (lc($piece) eq 'p') {
244 } elsif (lc($piece) eq 'n') {
246 } elsif (lc($piece) eq 'b') {
248 } elsif (lc($piece) eq 'r') {
250 } elsif (lc($piece) eq 'q') {
252 } elsif (lc($piece) eq 'k') {
255 die "Unknown piece $piece";
260 return pack('b*', $bits);
264 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
266 # can't eat your own piece
267 my $dest_piece = $board->[$to_row][$to_col];
268 if ($dest_piece ne '-') {
269 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
274 if ($to_col == $from_col && $to_row == $from_row + 1) {
275 return ($dest_piece eq '-');
277 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
278 my $middle_piece = $board->[2][$to_col];
279 return ($dest_piece eq '-' && $middle_piece eq '-');
281 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
282 if ($dest_piece eq '-') {
283 # En passant. TODO: check that the last move was indeed an EP move
284 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
293 if ($to_col == $from_col && $to_row == $from_row - 1) {
294 return ($dest_piece eq '-');
296 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
297 my $middle_piece = $board->[5][$to_col];
298 return ($dest_piece eq '-' && $middle_piece eq '-');
300 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
301 if ($dest_piece eq '-') {
302 # En passant. TODO: check that the last move was indeed an EP move
303 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
311 if (lc($piece) eq 'r') {
312 return 0 unless ($from_row == $to_row || $from_col == $to_col);
314 # check that there's a clear passage
315 if ($from_row == $to_row) {
316 if ($from_col > $to_col) {
317 ($to_col, $from_col) = ($from_col, $to_col);
320 for my $c (($from_col+1)..($to_col-1)) {
321 my $middle_piece = $board->[$to_row][$c];
322 return 0 if ($middle_piece ne '-');
327 if ($from_row > $to_row) {
328 ($to_row, $from_row) = ($from_row, $to_row);
331 for my $r (($from_row+1)..($to_row-1)) {
332 my $middle_piece = $board->[$r][$to_col];
333 return 0 if ($middle_piece ne '-');
339 if (lc($piece) eq 'b') {
340 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
342 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
343 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
345 my $r = $from_row + $dr;
346 my $c = $from_col + $dc;
348 while ($r != $to_row) {
349 my $middle_piece = $board->[$r][$c];
350 return 0 if ($middle_piece ne '-');
358 if (lc($piece) eq 'n') {
359 my $diff_r = abs($from_row - $to_row);
360 my $diff_c = abs($from_col - $to_col);
361 return 1 if ($diff_r == 2 && $diff_c == 1);
362 return 1 if ($diff_r == 1 && $diff_c == 2);
366 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
367 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
370 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
371 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
373 if (lc($piece) eq 'k') {
374 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
381 my %pieces_against_side = (
382 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
383 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
386 # Returns whether the given side (given as k or K for black and white) is in check.
388 my ($board, $side) = @_;
389 my ($kr, $kc) = _find_piece($board, $side);
391 # check all pieces for the possibility of threatening this king
393 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
395 my $piece = $board->[$row][$col];
396 next if ($piece eq '-');
397 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
405 my ($board, $piece) = @_;
408 next unless grep { $_ eq $piece } @{$board->[$row]};
410 if ($board->[$row][$col] eq $piece) {
416 return (undef, undef);
419 # Returns if the given side (given as k or K) is in mate.
421 my ($board, $side, $in_check) = @_;
422 return 0 if (!$in_check);
424 # try all possible moves for the side in check
427 my $piece = $board->[$row][$col];
428 next if ($piece eq '-');
431 next if ($piece eq lc($piece));
433 next if ($piece eq uc($piece));
436 for my $dest_row (0..7) {
437 for my $dest_col (0..7) {
438 next if ($row == $dest_row && $col == $dest_col);
439 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
441 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
442 return 0 if (!$nb->in_check($side));
448 # nothing to do; mate
452 # Returns the short algebraic form of the move, as well as the new position.
453 sub prettyprint_move {
454 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
455 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
457 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
459 my $piece = $board->[$from_row][$from_col];
460 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
461 my $in_check = $nb->in_check($other_side);
462 if ($nb->in_mate($other_side, $in_check)) {
464 } elsif ($in_check) {
467 return ($pretty, $nb);
476 my $piece = $board->[$row][$col];
477 ++$num if ($piece ne '-');
483 sub _prettyprint_move_no_check_or_mate {
484 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
485 my $piece = $board->[$from_row][$from_col];
486 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
489 die "Invalid move $move";
492 # white short castling
493 if ($move eq 'e1g1' && $piece eq 'K') {
497 # white long castling
498 if ($move eq 'e1c1' && $piece eq 'K') {
502 # black short castling
503 if ($move eq 'e8g8' && $piece eq 'k') {
507 # black long castling
508 if ($move eq 'e8c8' && $piece eq 'k') {
514 # check if the from-piece is a pawn
515 if (lc($piece) eq 'p') {
517 if ($from_col != $to_col) {
518 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
520 $pretty = _pos_to_square($to_row, $to_col);
522 if (defined($promo) && $promo ne '') {
525 $pretty .= uc($promo);
531 $pretty = uc($piece);
533 # see how many of these pieces could go here, in all
537 next unless ($board->[$row][$col] eq $piece);
538 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
542 # see how many of these pieces from the given row could go here
545 next unless ($board->[$from_row][$col] eq $piece);
546 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
549 # and same for columns
552 next unless ($board->[$row][$from_col] eq $piece);
553 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
556 # see if we need to disambiguate
557 if ($num_total > 1) {
559 $pretty .= substr($move, 0, 1);
560 } elsif ($num_row == 1) {
561 $pretty .= substr($move, 1, 1);
563 $pretty .= substr($move, 0, 2);
568 if ($board->[$to_row][$to_col] ne '-') {
572 $pretty .= _pos_to_square($to_row, $to_col);