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);
225 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
227 # can't eat your own piece
228 my $dest_piece = $board->[$to_row][$to_col];
229 if ($dest_piece ne '-') {
230 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
235 if ($to_col == $from_col && $to_row == $from_row + 1) {
236 return ($dest_piece eq '-');
238 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
239 my $middle_piece = $board->[2][$to_col];
240 return ($dest_piece eq '-' && $middle_piece eq '-');
242 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
243 if ($dest_piece eq '-') {
244 # En passant. TODO: check that the last move was indeed an EP move
245 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
254 if ($to_col == $from_col && $to_row == $from_row - 1) {
255 return ($dest_piece eq '-');
257 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
258 my $middle_piece = $board->[5][$to_col];
259 return ($dest_piece eq '-' && $middle_piece eq '-');
261 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
262 if ($dest_piece eq '-') {
263 # En passant. TODO: check that the last move was indeed an EP move
264 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
272 if (lc($piece) eq 'r') {
273 return 0 unless ($from_row == $to_row || $from_col == $to_col);
275 # check that there's a clear passage
276 if ($from_row == $to_row) {
277 if ($from_col > $to_col) {
278 ($to_col, $from_col) = ($from_col, $to_col);
281 for my $c (($from_col+1)..($to_col-1)) {
282 my $middle_piece = $board->[$to_row][$c];
283 return 0 if ($middle_piece ne '-');
288 if ($from_row > $to_row) {
289 ($to_row, $from_row) = ($from_row, $to_row);
292 for my $r (($from_row+1)..($to_row-1)) {
293 my $middle_piece = $board->[$r][$to_col];
294 return 0 if ($middle_piece ne '-');
300 if (lc($piece) eq 'b') {
301 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
303 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
304 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
306 my $r = $from_row + $dr;
307 my $c = $from_col + $dc;
309 while ($r != $to_row) {
310 my $middle_piece = $board->[$r][$c];
311 return 0 if ($middle_piece ne '-');
319 if (lc($piece) eq 'n') {
320 my $diff_r = abs($from_row - $to_row);
321 my $diff_c = abs($from_col - $to_col);
322 return 1 if ($diff_r == 2 && $diff_c == 1);
323 return 1 if ($diff_r == 1 && $diff_c == 2);
327 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
328 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
331 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
332 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
334 if (lc($piece) eq 'k') {
335 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
342 my %pieces_against_side = (
343 k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
344 K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
347 # Returns whether the given side (given as k or K for black and white) is in check.
349 my ($board, $side) = @_;
350 my ($kr, $kc) = _find_piece($board, $side);
352 # check all pieces for the possibility of threatening this king
354 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
356 my $piece = $board->[$row][$col];
357 next if ($piece eq '-');
358 return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
366 my ($board, $piece) = @_;
369 next unless grep { $_ eq $piece } @{$board->[$row]};
371 if ($board->[$row][$col] eq $piece) {
377 return (undef, undef);
380 # Returns if the given side (given as k or K) is in mate.
382 my ($board, $side, $in_check) = @_;
383 return 0 if (!$in_check);
385 # try all possible moves for the side in check
388 my $piece = $board->[$row][$col];
389 next if ($piece eq '-');
392 next if ($piece eq lc($piece));
394 next if ($piece eq uc($piece));
397 for my $dest_row (0..7) {
398 for my $dest_col (0..7) {
399 next if ($row == $dest_row && $col == $dest_col);
400 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
402 my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
403 return 0 if (!$nb->in_check($side));
409 # nothing to do; mate
413 # Returns the short algebraic form of the move, as well as the new position.
414 sub prettyprint_move {
415 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
416 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
418 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
420 my $piece = $board->[$from_row][$from_col];
421 my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
422 my $in_check = $nb->in_check($other_side);
423 if ($nb->in_mate($other_side, $in_check)) {
425 } elsif ($in_check) {
428 return ($pretty, $nb);
437 my $piece = $board->[$row][$col];
438 ++$num if ($piece ne '-');
444 sub _prettyprint_move_no_check_or_mate {
445 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
446 my $piece = $board->[$from_row][$from_col];
447 my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
450 die "Invalid move $move";
453 # white short castling
454 if ($move eq 'e1g1' && $piece eq 'K') {
458 # white long castling
459 if ($move eq 'e1c1' && $piece eq 'K') {
463 # black short castling
464 if ($move eq 'e8g8' && $piece eq 'k') {
468 # black long castling
469 if ($move eq 'e8c8' && $piece eq 'k') {
475 # check if the from-piece is a pawn
476 if (lc($piece) eq 'p') {
478 if ($from_col != $to_col) {
479 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
481 $pretty = _pos_to_square($to_row, $to_col);
483 if (defined($promo) && $promo ne '') {
486 $pretty .= uc($promo);
492 $pretty = uc($piece);
494 # see how many of these pieces could go here, in all
498 next unless ($board->[$row][$col] eq $piece);
499 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
503 # see how many of these pieces from the given row could go here
506 next unless ($board->[$from_row][$col] eq $piece);
507 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
510 # and same for columns
513 next unless ($board->[$row][$from_col] eq $piece);
514 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
517 # see if we need to disambiguate
518 if ($num_total > 1) {
520 $pretty .= substr($move, 0, 1);
521 } elsif ($num_row == 1) {
522 $pretty .= substr($move, 1, 1);
524 $pretty .= substr($move, 0, 2);
529 if ($board->[$to_row][$to_col] ne '-') {
533 $pretty .= _pos_to_square($to_row, $to_col);