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] = '-';
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 _move_to_uci_notation {
135 my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
137 return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
144 my $str = join('', @{$board->[$row]});
145 $str =~ s/(-+)/length($1)/ge;
149 return join('/', @rows);
153 my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
155 # can't eat your own piece
156 my $dest_piece = $board->[$to_row][$to_col];
157 if ($dest_piece ne '-') {
158 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
161 if (lc($piece) eq 'k') {
162 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
164 if (lc($piece) eq 'r') {
165 return 0 unless ($from_row == $to_row || $from_col == $to_col);
167 # check that there's a clear passage
168 if ($from_row == $to_row) {
169 if ($from_col > $to_col) {
170 ($to_col, $from_col) = ($from_col, $to_col);
173 for my $c (($from_col+1)..($to_col-1)) {
174 my $middle_piece = $board->[$to_row][$c];
175 return 0 if ($middle_piece ne '-');
180 if ($from_row > $to_row) {
181 ($to_row, $from_row) = ($from_row, $to_row);
184 for my $r (($from_row+1)..($to_row-1)) {
185 my $middle_piece = $board->[$r][$to_col];
186 return 0 if ($middle_piece ne '-');
192 if (lc($piece) eq 'b') {
193 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
195 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
196 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
198 my $r = $from_row + $dr;
199 my $c = $from_col + $dc;
201 while ($r != $to_row) {
202 my $middle_piece = $board->[$r][$c];
203 return 0 if ($middle_piece ne '-');
211 if (lc($piece) eq 'n') {
212 my $diff_r = abs($from_row - $to_row);
213 my $diff_c = abs($from_col - $to_col);
214 return 1 if ($diff_r == 2 && $diff_c == 1);
215 return 1 if ($diff_r == 1 && $diff_c == 2);
219 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
220 can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
223 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
224 can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
230 if ($to_col == $from_col && $to_row == $from_row + 1) {
231 return ($dest_piece eq '-');
233 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
234 my $middle_piece = $board->[2][$to_col];
235 return ($dest_piece eq '-' && $middle_piece eq '-');
237 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
238 return ($dest_piece ne '-');
244 if ($to_col == $from_col && $to_row == $from_row - 1) {
245 return ($dest_piece eq '-');
247 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
248 my $middle_piece = $board->[5][$to_col];
249 return ($dest_piece eq '-' && $middle_piece eq '-');
251 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
252 return ($dest_piece ne '-');
261 # Returns 'none', 'white', 'black' or 'both', depending on which sides are in check.
262 # The latter naturally indicates an invalid position.
265 my ($black_check, $white_check) = (0, 0);
267 my ($wkr, $wkc, $bkr, $bkc) = _find_kings($board);
269 # check all pieces for the possibility of threatening the two kings
272 my $piece = $board->[$row][$col];
273 next if ($piece eq '-');
275 if (uc($piece) eq $piece) {
277 $black_check = 1 if ($board->can_reach($piece, $row, $col, $bkr, $bkc));
280 $white_check = 1 if ($board->can_reach($piece, $row, $col, $wkr, $wkc));
285 if ($black_check && $white_check) {
287 } elsif ($black_check) {
289 } elsif ($white_check) {
298 my ($wkr, $wkc, $bkr, $bkc);
302 my $piece = $board->[$row][$col];
304 ($wkr, $wkc) = ($row, $col);
305 } elsif ($piece eq 'k') {
306 ($bkr, $bkc) = ($row, $col);
311 return ($wkr, $wkc, $bkr, $bkc);
314 # Returns if any side is in mate.
317 my $check = $board->in_check();
318 return 0 if ($check eq 'none');
320 # try all possible moves for the side in check
323 my $piece = $board->[$row][$col];
324 next if ($piece eq '-');
326 if ($check eq 'white') {
327 next if ($piece eq lc($piece));
329 next if ($piece eq uc($piece));
332 for my $dest_row (0..7) {
333 for my $dest_col (0..7) {
334 next if ($row == $dest_row && $col == $dest_col);
335 next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
337 my $nb = $board->clone();
338 $nb->[$row][$col] = '-';
339 $nb->[$dest_row][$dest_col] = $piece;
340 my $new_check = $nb->in_check();
341 return 0 if ($new_check ne $check && $new_check ne 'both');
347 # nothing to do; mate
351 # Returns the short algebraic form of the move, as well as the new position.
352 sub prettyprint_move {
353 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
354 my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
356 my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
357 if ($nb->in_mate()) {
359 } elsif ($nb->in_check() ne 'none') {
362 return ($pretty, $nb);
365 sub _prettyprint_move_no_check_or_mate {
366 my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
367 my $piece = $board->[$from_row][$from_col];
368 my $move = _move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
371 die "Invalid move $move";
374 # white short castling
375 if ($move eq 'e1g1' && $piece eq 'K') {
379 # white long castling
380 if ($move eq 'e1c1' && $piece eq 'K') {
384 # black short castling
385 if ($move eq 'e8g8' && $piece eq 'k') {
389 # black long castling
390 if ($move eq 'e8c8' && $piece eq 'k') {
396 # check if the from-piece is a pawn
397 if (lc($piece) eq 'p') {
399 if ($from_col != $to_col) {
400 $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
402 $pretty = _pos_to_square($to_row, $to_col);
404 if (defined($promo) && $promo ne '') {
413 $pretty = uc($piece);
415 # see how many of these pieces could go here, in all
419 next unless ($board->[$row][$col] eq $piece);
420 ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
424 # see how many of these pieces from the given row could go here
427 next unless ($board->[$from_row][$col] eq $piece);
428 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
431 # and same for columns
434 next unless ($board->[$row][$from_col] eq $piece);
435 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
438 # see if we need to disambiguate
439 if ($num_total > 1) {
441 $pretty .= substr($move, 0, 1);
442 } elsif ($num_row == 1) {
443 $pretty .= substr($move, 1, 1);
445 $pretty .= substr($move, 0, 2);
450 if ($board->[$to_row][$to_col] ne '-') {
454 $pretty .= _pos_to_square($to_row, $to_col);