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 '') {
123 $nb->[$from_row][$from_col] = '-';
124 $nb->[$to_row][$to_col] = $piece;
130 my ($row, $col) = @_;
131 return sprintf("%c%d", ord('a') + $col, 8 - $row);
134 sub _col_letter_to_num {
135 return ord(shift) - ord('a');
138 sub _row_letter_to_num {
139 return 7 - (ord(shift) - ord('1'));
144 $square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";
145 return (_row_letter_to_num($2), _col_letter_to_num($1));
148 sub _move_to_uci_notation {
149 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
151 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
154 sub parse_pretty_move {
155 my ($board, $move, $toplay) = @_;
157 # Strip check or mate
160 if ($move eq '0-0' or $move eq 'O-O') {
161 if ($toplay eq 'W') {
162 return (_square_to_pos('e1'), _square_to_pos('g1'));
164 return (_square_to_pos('e8'), _square_to_pos('g8'));
166 } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
167 if ($toplay eq 'W') {
168 return (_square_to_pos('e1'), _square_to_pos('c1'));
170 return (_square_to_pos('e8'), _square_to_pos('c8'));
176 if ($move =~ s/=([QRNB])$//) {
180 $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
181 my $piece = $1 // 'P';
182 my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
183 my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
184 my ($to_row, $to_col) = _square_to_pos($4);
186 # Find all possible from-squares that could have been meant.
188 if ($toplay eq 'B') {
192 next if (defined($from_row) && $from_row != $row);
194 next if (defined($from_col) && $from_col != $col);
195 next if ($board->[$row][$col] ne $piece);
196 next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
198 # See if doing this move would put us in check
199 # (yes, there are clients that expect us to do this).
200 my $check = $board->make_move($row, $col, $to_row, $to_col, $promo)->in_check();
201 next if ($check eq 'both' ||
202 ($toplay eq 'W' && $check eq 'white') ||
203 ($toplay eq 'B' && $check eq 'black'));
205 push @squares, [ $row, $col ];
208 if (scalar @squares != 1) {
209 die "Ambigious or impossible move $move";
211 return (@{$squares[0]}, $to_row, $to_col, $promo);
218 my $str = join('', @{$board->[$row]});
219 $str =~ s/(-+)/length($1)/ge;
223 return join('/', @rows);
227 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
229 # can't eat your own piece
230 my $dest_piece = $board->[$to_row][$to_col];
231 if ($dest_piece ne '-') {
232 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
235 if (lc($piece) eq 'k') {
236 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
238 if (lc($piece) eq 'r') {
239 return 0 unless ($from_row == $to_row || $from_col == $to_col);
241 # check that there's a clear passage
242 if ($from_row == $to_row) {
243 if ($from_col > $to_col) {
244 ($to_col, $from_col) = ($from_col, $to_col);
247 for my $c (($from_col+1)..($to_col-1)) {
248 my $middle_piece = $board->[$to_row][$c];
249 return 0 if ($middle_piece ne '-');
254 if ($from_row > $to_row) {
255 ($to_row, $from_row) = ($from_row, $to_row);
258 for my $r (($from_row+1)..($to_row-1)) {
259 my $middle_piece = $board->[$r][$to_col];
260 return 0 if ($middle_piece ne '-');
266 if (lc($piece) eq 'b') {
267 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
269 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
270 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
272 my $r = $from_row + $dr;
273 my $c = $from_col + $dc;
275 while ($r != $to_row) {
276 my $middle_piece = $board->[$r][$c];
277 return 0 if ($middle_piece ne '-');
285 if (lc($piece) eq 'n') {
286 my $diff_r = abs($from_row - $to_row);
287 my $diff_c = abs($from_col - $to_col);
288 return 1 if ($diff_r == 2 && $diff_c == 1);
289 return 1 if ($diff_r == 1 && $diff_c == 2);
293 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
294 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
297 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
298 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
303 if ($to_col == $from_col && $to_row == $from_row + 1) {
304 return ($dest_piece eq '-');
306 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
307 my $middle_piece = $board->[2][$to_col];
308 return ($dest_piece eq '-' && $middle_piece eq '-');
310 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
311 if ($dest_piece eq '-') {
312 # En passant. TODO: check that the last move was indeed an EP move
313 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
322 if ($to_col == $from_col && $to_row == $from_row - 1) {
323 return ($dest_piece eq '-');
325 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
326 my $middle_piece = $board->[5][$to_col];
327 return ($dest_piece eq '-' && $middle_piece eq '-');
329 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
330 if ($dest_piece eq '-') {
331 # En passant. TODO: check that the last move was indeed an EP move
332 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
344 # Returns 'none', 'white', 'black' or 'both', depending on which sides are in check.
345 # The latter naturally indicates an invalid position.
348 my ($black_check, $white_check) = (0, 0);
350 my ($wkr, $wkc, $bkr, $bkc) = _find_kings($board);
352 # check all pieces for the possibility of threatening the two kings
355 my $piece = $board->[$row][$col];
356 next if ($piece eq '-');
358 if (uc($piece) eq $piece) {
360 $black_check = 1 if ($board->can_reach($piece, $row, $col, $bkr, $bkc));
363 $white_check = 1 if ($board->can_reach($piece, $row, $col, $wkr, $wkc));
368 if ($black_check && $white_check) {
370 } elsif ($black_check) {
372 } elsif ($white_check) {
381 my ($wkr, $wkc, $bkr, $bkc);
385 my $piece = $board->[$row][$col];
387 ($wkr, $wkc) = ($row, $col);
388 } elsif ($piece eq 'k') {
389 ($bkr, $bkc) = ($row, $col);
394 return ($wkr, $wkc, $bkr, $bkc);
397 # Returns if any side is in mate.
400 my $check = $board->in_check();
401 return 0 if ($check eq 'none');
403 # try all possible moves for the side in check
406 my $piece = $board->[$row][$col];
407 next if ($piece eq '-');
409 if ($check eq 'white') {
410 next if ($piece eq lc($piece));
412 next if ($piece eq uc($piece));
415 for my $dest_row (0..7) {
416 for my $dest_col (0..7) {
417 next if ($row == $dest_row && $col == $dest_col);
418 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
420 my $nb = $board->clone();
421 $nb->[$row][$col] = '-';
422 $nb->[$dest_row][$dest_col] = $piece;
423 my $new_check = $nb->in_check();
424 return 0 if ($new_check ne $check && $new_check ne 'both');
430 # nothing to do; mate
434 # Returns the short algebraic form of the move, as well as the new position.
435 sub prettyprint_move {
436 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
437 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
439 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
440 if ($nb->in_mate()) {
442 } elsif ($nb->in_check() ne 'none') {
445 return ($pretty, $nb);
448 sub _prettyprint_move_no_check_or_mate {
449 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
450 my $piece = $board->[$from_row][$from_col];
451 my $move = _move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
454 die "Invalid move $move";
457 # white short castling
458 if ($move eq 'e1g1' && $piece eq 'K') {
462 # white long castling
463 if ($move eq 'e1c1' && $piece eq 'K') {
467 # black short castling
468 if ($move eq 'e8g8' && $piece eq 'k') {
472 # black long castling
473 if ($move eq 'e8c8' && $piece eq 'k') {
479 # check if the from-piece is a pawn
480 if (lc($piece) eq 'p') {
482 if ($from_col != $to_col) {
483 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
485 $pretty = _pos_to_square($to_row, $to_col);
487 if (defined($promo) && $promo ne '') {
496 $pretty = uc($piece);
498 # see how many of these pieces could go here, in all
502 next unless ($board->[$row][$col] eq $piece);
503 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
507 # see how many of these pieces from the given row could go here
510 next unless ($board->[$from_row][$col] eq $piece);
511 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
514 # and same for columns
517 next unless ($board->[$row][$from_col] eq $piece);
518 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
521 # see if we need to disambiguate
522 if ($num_total > 1) {
524 $pretty .= substr($move, 0, 1);
525 } elsif ($num_row == 1) {
526 $pretty .= substr($move, 1, 1);
528 $pretty .= substr($move, 0, 2);
533 if ($board->[$to_row][$to_col] ne '-') {
537 $pretty .= _pos_to_square($to_row, $to_col);