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.
185 if ($toplay eq 'B') {
189 next if (defined($from_row) && $from_row != $row);
191 next if (defined($from_col) && $from_col != $col);
192 next if ($board->[$row][$col] ne $piece);
193 next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
195 # See if doing this move would put us in check
196 # (yes, there are clients that expect us to do this).
197 my $check = $board->make_move($row, $col, $to_row, $to_col, $promo)->in_check();
198 next if ($check eq 'both' ||
199 ($toplay eq 'W' && $check eq 'white') ||
200 ($toplay eq 'B' && $check eq 'black'));
202 push @squares, [ $row, $col ];
205 if (scalar @squares != 1) {
206 die "Ambigious or impossible move $move";
208 return (@{$squares[0]}, $to_row, $to_col, $promo);
215 my $str = join('', @{$board->[$row]});
216 $str =~ s/(-+)/length($1)/ge;
220 return join('/', @rows);
224 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
226 # can't eat your own piece
227 my $dest_piece = $board->[$to_row][$to_col];
228 if ($dest_piece ne '-') {
229 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
232 if (lc($piece) eq 'k') {
233 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
235 if (lc($piece) eq 'r') {
236 return 0 unless ($from_row == $to_row || $from_col == $to_col);
238 # check that there's a clear passage
239 if ($from_row == $to_row) {
240 if ($from_col > $to_col) {
241 ($to_col, $from_col) = ($from_col, $to_col);
244 for my $c (($from_col+1)..($to_col-1)) {
245 my $middle_piece = $board->[$to_row][$c];
246 return 0 if ($middle_piece ne '-');
251 if ($from_row > $to_row) {
252 ($to_row, $from_row) = ($from_row, $to_row);
255 for my $r (($from_row+1)..($to_row-1)) {
256 my $middle_piece = $board->[$r][$to_col];
257 return 0 if ($middle_piece ne '-');
263 if (lc($piece) eq 'b') {
264 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
266 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
267 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
269 my $r = $from_row + $dr;
270 my $c = $from_col + $dc;
272 while ($r != $to_row) {
273 my $middle_piece = $board->[$r][$c];
274 return 0 if ($middle_piece ne '-');
282 if (lc($piece) eq 'n') {
283 my $diff_r = abs($from_row - $to_row);
284 my $diff_c = abs($from_col - $to_col);
285 return 1 if ($diff_r == 2 && $diff_c == 1);
286 return 1 if ($diff_r == 1 && $diff_c == 2);
290 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
291 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
294 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
295 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
300 if ($to_col == $from_col && $to_row == $from_row + 1) {
301 return ($dest_piece eq '-');
303 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
304 my $middle_piece = $board->[2][$to_col];
305 return ($dest_piece eq '-' && $middle_piece eq '-');
307 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
308 if ($dest_piece eq '-') {
309 # En passant. TODO: check that the last move was indeed an EP move
310 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
319 if ($to_col == $from_col && $to_row == $from_row - 1) {
320 return ($dest_piece eq '-');
322 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
323 my $middle_piece = $board->[5][$to_col];
324 return ($dest_piece eq '-' && $middle_piece eq '-');
326 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
327 if ($dest_piece eq '-') {
328 # En passant. TODO: check that the last move was indeed an EP move
329 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
341 # Returns 'none', 'white', 'black' or 'both', depending on which sides are in check.
342 # The latter naturally indicates an invalid position.
345 my ($black_check, $white_check) = (0, 0);
347 my ($wkr, $wkc, $bkr, $bkc) = _find_kings($board);
349 # check all pieces for the possibility of threatening the two kings
352 my $piece = $board->[$row][$col];
353 next if ($piece eq '-');
355 if (uc($piece) eq $piece) {
357 $black_check = 1 if ($board->can_reach($piece, $row, $col, $bkr, $bkc));
360 $white_check = 1 if ($board->can_reach($piece, $row, $col, $wkr, $wkc));
365 if ($black_check && $white_check) {
367 } elsif ($black_check) {
369 } elsif ($white_check) {
378 my ($wkr, $wkc, $bkr, $bkc);
382 my $piece = $board->[$row][$col];
384 ($wkr, $wkc) = ($row, $col);
385 } elsif ($piece eq 'k') {
386 ($bkr, $bkc) = ($row, $col);
391 return ($wkr, $wkc, $bkr, $bkc);
394 # Returns if any side is in mate.
397 my $check = $board->in_check();
398 return 0 if ($check eq 'none');
400 # try all possible moves for the side in check
403 my $piece = $board->[$row][$col];
404 next if ($piece eq '-');
406 if ($check eq 'white') {
407 next if ($piece eq lc($piece));
409 next if ($piece eq uc($piece));
412 for my $dest_row (0..7) {
413 for my $dest_col (0..7) {
414 next if ($row == $dest_row && $col == $dest_col);
415 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
417 my $nb = $board->clone();
418 $nb->[$row][$col] = '-';
419 $nb->[$dest_row][$dest_col] = $piece;
420 my $new_check = $nb->in_check();
421 return 0 if ($new_check ne $check && $new_check ne 'both');
427 # nothing to do; mate
431 # Returns the short algebraic form of the move, as well as the new position.
432 sub prettyprint_move {
433 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
434 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
436 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
437 if ($nb->in_mate()) {
439 } elsif ($nb->in_check() ne 'none') {
442 return ($pretty, $nb);
445 sub _prettyprint_move_no_check_or_mate {
446 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
447 my $piece = $board->[$from_row][$from_col];
448 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
451 die "Invalid move $move";
454 # white short castling
455 if ($move eq 'e1g1' && $piece eq 'K') {
459 # white long castling
460 if ($move eq 'e1c1' && $piece eq 'K') {
464 # black short castling
465 if ($move eq 'e8g8' && $piece eq 'k') {
469 # black long castling
470 if ($move eq 'e8c8' && $piece eq 'k') {
476 # check if the from-piece is a pawn
477 if (lc($piece) eq 'p') {
479 if ($from_col != $to_col) {
480 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
482 $pretty = _pos_to_square($to_row, $to_col);
484 if (defined($promo) && $promo ne '') {
493 $pretty = uc($piece);
495 # see how many of these pieces could go here, in all
499 next unless ($board->[$row][$col] eq $piece);
500 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
504 # see how many of these pieces from the given row could go here
507 next unless ($board->[$from_row][$col] eq $piece);
508 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
511 # and same for columns
514 next unless ($board->[$row][$from_col] eq $piece);
515 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
518 # see if we need to disambiguate
519 if ($num_total > 1) {
521 $pretty .= substr($move, 0, 1);
522 } elsif ($num_row == 1) {
523 $pretty .= substr($move, 1, 1);
525 $pretty .= substr($move, 0, 2);
530 if ($board->[$to_row][$to_col] ne '-') {
534 $pretty .= _pos_to_square($to_row, $to_col);