]> git.sesse.net Git - remoteglot/blobdiff - Board.pm
Tiny optimization in _find_kings.
[remoteglot] / Board.pm
index ff2828af82b03c60f6e2fcef4dd341e8d2d6ed0e..8cf603d0f5be052c762d3f127d7bba87dc047196 100644 (file)
--- a/Board.pm
+++ b/Board.pm
@@ -25,9 +25,7 @@ sub clone {
        my $nb = [];
 
        for my $row (0..7) {
-               for my $col (0..7) {
-                       $nb->[$row][$col] = $board->[$row][$col];
-               }
+               $nb->[$row] = [ @{$board->[$row]} ];
        }
 
        return bless $nb;
@@ -380,6 +378,7 @@ sub _find_kings {
        my ($wkr, $wkc, $bkr, $bkc);
 
        for my $row (0..7) {
+               next unless grep { $_ eq 'K' || $_ eq 'k' } @{$board->[$row]};
                for my $col (0..7) {
                        my $piece = $board->[$row][$col];
                        if ($piece eq 'K') {
@@ -395,8 +394,7 @@ sub _find_kings {
 
 # Returns if any side is in mate.
 sub in_mate {
-       my $board = shift;
-       my $check = $board->in_check();
+       my ($board, $check) = @_;
        return 0 if ($check eq 'none');
 
        # try all possible moves for the side in check
@@ -436,9 +434,10 @@ sub prettyprint_move {
        my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
 
        my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
-       if ($nb->in_mate()) {
+       my $check = $nb->in_check();
+       if ($nb->in_mate($check)) {
                $pretty .= '#';
-       } elsif ($nb->in_check() ne 'none') {
+       } elsif ($check ne 'none') {
                $pretty .= '+';
        }
        return ($pretty, $nb);