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);
29 $nb->[$row][$col] = $board->[$row][$col];
36 # Returns a new board.
38 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
39 my $move = _move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
40 my $piece = $board->[$from_row][$from_col];
41 my $nb = $board->clone();
44 die "Invalid move $move";
47 # white short castling
48 if ($move eq 'e1g1' && $piece eq 'K') {
61 if ($move eq 'e1c1' && $piece eq 'K') {
73 # black short castling
74 if ($move eq 'e8g8' && $piece eq 'k') {
87 if ($move eq 'e8c8' && $piece eq 'k') {
99 # check if the from-piece is a pawn
100 if (lc($piece) eq 'p') {
102 if ($from_col != $to_col) {
104 if ($board->[$to_row][$to_col] eq '-') {
106 $nb->[$to_row - 1][$to_col] = '-';
108 $nb->[$to_row + 1][$to_col] = '-';
112 if (defined($promo) && $promo ne '') {
122 $nb->[$from_row][$from_col] = '-';
123 $nb->[$to_row][$to_col] = $piece;
129 my ($row, $col) = @_;
130 return sprintf("%c%d", ord('a') + $col, 8 - $row);
133 sub _col_letter_to_num {
134 return ord(shift) - ord('a');
137 sub _row_letter_to_num {
138 return 7 - (ord(shift) - ord('1'));
143 $square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
144 return (_row_letter_to_num($2), _col_letter_to_num($1));
147 sub _move_to_uci_notation {
148 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
150 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
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.
187 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 my $check = $board->make_move($row, $col, $to_row, $to_col, $promo)->in_check();
200 next if ($check eq 'both' ||
201 ($toplay eq 'W' && $check eq 'white') ||
202 ($toplay eq 'B' && $check eq 'black'));
204 push @squares, [ $row, $col ];
207 if (scalar @squares != 1) {
208 die "Ambigious or impossible move $move";
210 return (@{$squares[0]}, $to_row, $to_col, $promo);
217 my $str = join('', @{$board->[$row]});
218 $str =~ s/(-+)/length($1)/ge;
222 return join('/', @rows);
226 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
228 # can't eat your own piece
229 my $dest_piece = $board->[$to_row][$to_col];
230 if ($dest_piece ne '-') {
231 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
234 if (lc($piece) eq 'k') {
235 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
237 if (lc($piece) eq 'r') {
238 return 0 unless ($from_row == $to_row || $from_col == $to_col);
240 # check that there's a clear passage
241 if ($from_row == $to_row) {
242 if ($from_col > $to_col) {
243 ($to_col, $from_col) = ($from_col, $to_col);
246 for my $c (($from_col+1)..($to_col-1)) {
247 my $middle_piece = $board->[$to_row][$c];
248 return 0 if ($middle_piece ne '-');
253 if ($from_row > $to_row) {
254 ($to_row, $from_row) = ($from_row, $to_row);
257 for my $r (($from_row+1)..($to_row-1)) {
258 my $middle_piece = $board->[$r][$to_col];
259 return 0 if ($middle_piece ne '-');
265 if (lc($piece) eq 'b') {
266 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
268 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
269 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
271 my $r = $from_row + $dr;
272 my $c = $from_col + $dc;
274 while ($r != $to_row) {
275 my $middle_piece = $board->[$r][$c];
276 return 0 if ($middle_piece ne '-');
284 if (lc($piece) eq 'n') {
285 my $diff_r = abs($from_row - $to_row);
286 my $diff_c = abs($from_col - $to_col);
287 return 1 if ($diff_r == 2 && $diff_c == 1);
288 return 1 if ($diff_r == 1 && $diff_c == 2);
292 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
293 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
296 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
297 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
302 if ($to_col == $from_col && $to_row == $from_row + 1) {
303 return ($dest_piece eq '-');
305 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
306 my $middle_piece = $board->[2][$to_col];
307 return ($dest_piece eq '-' && $middle_piece eq '-');
309 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
310 if ($dest_piece eq '-') {
311 # En passant. TODO: check that the last move was indeed an EP move
312 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
321 if ($to_col == $from_col && $to_row == $from_row - 1) {
322 return ($dest_piece eq '-');
324 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
325 my $middle_piece = $board->[5][$to_col];
326 return ($dest_piece eq '-' && $middle_piece eq '-');
328 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
329 if ($dest_piece eq '-') {
330 # En passant. TODO: check that the last move was indeed an EP move
331 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
343 # Returns 'none', 'white', 'black' or 'both', depending on which sides are in check.
344 # The latter naturally indicates an invalid position.
347 my ($black_check, $white_check) = (0, 0);
349 my ($wkr, $wkc, $bkr, $bkc) = _find_kings($board);
351 # check all pieces for the possibility of threatening the two kings
354 my $piece = $board->[$row][$col];
355 next if ($piece eq '-');
357 if (uc($piece) eq $piece) {
359 $black_check = 1 if ($board->can_reach($piece, $row, $col, $bkr, $bkc));
362 $white_check = 1 if ($board->can_reach($piece, $row, $col, $wkr, $wkc));
367 if ($black_check && $white_check) {
369 } elsif ($black_check) {
371 } elsif ($white_check) {
380 my ($wkr, $wkc, $bkr, $bkc);
384 my $piece = $board->[$row][$col];
386 ($wkr, $wkc) = ($row, $col);
387 } elsif ($piece eq 'k') {
388 ($bkr, $bkc) = ($row, $col);
393 return ($wkr, $wkc, $bkr, $bkc);
396 # Returns if any side is in mate.
399 my $check = $board->in_check();
400 return 0 if ($check eq 'none');
402 # try all possible moves for the side in check
405 my $piece = $board->[$row][$col];
406 next if ($piece eq '-');
408 if ($check eq 'white') {
409 next if ($piece eq lc($piece));
411 next if ($piece eq uc($piece));
414 for my $dest_row (0..7) {
415 for my $dest_col (0..7) {
416 next if ($row == $dest_row && $col == $dest_col);
417 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
419 my $nb = $board->clone();
420 $nb->[$row][$col] = '-';
421 $nb->[$dest_row][$dest_col] = $piece;
422 my $new_check = $nb->in_check();
423 return 0 if ($new_check ne $check && $new_check ne 'both');
429 # nothing to do; mate
433 # Returns the short algebraic form of the move, as well as the new position.
434 sub prettyprint_move {
435 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
436 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
438 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
439 if ($nb->in_mate()) {
441 } elsif ($nb->in_check() ne 'none') {
444 return ($pretty, $nb);
447 sub _prettyprint_move_no_check_or_mate {
448 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
449 my $piece = $board->[$from_row][$from_col];
450 my $move = _move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
453 die "Invalid move $move";
456 # white short castling
457 if ($move eq 'e1g1' && $piece eq 'K') {
461 # white long castling
462 if ($move eq 'e1c1' && $piece eq 'K') {
466 # black short castling
467 if ($move eq 'e8g8' && $piece eq 'k') {
471 # black long castling
472 if ($move eq 'e8c8' && $piece eq 'k') {
478 # check if the from-piece is a pawn
479 if (lc($piece) eq 'p') {
481 if ($from_col != $to_col) {
482 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
484 $pretty = _pos_to_square($to_row, $to_col);
486 if (defined($promo) && $promo ne '') {
495 $pretty = uc($piece);
497 # see how many of these pieces could go here, in all
501 next unless ($board->[$row][$col] eq $piece);
502 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
506 # see how many of these pieces from the given row could go here
509 next unless ($board->[$from_row][$col] eq $piece);
510 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
513 # and same for columns
516 next unless ($board->[$row][$from_col] eq $piece);
517 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
520 # see if we need to disambiguate
521 if ($num_total > 1) {
523 $pretty .= substr($move, 0, 1);
524 } elsif ($num_row == 1) {
525 $pretty .= substr($move, 1, 1);
527 $pretty .= substr($move, 0, 2);
532 if ($board->[$to_row][$to_col] ne '-') {
536 $pretty .= _pos_to_square($to_row, $to_col);