]> git.sesse.net Git - remoteglot/blobdiff - Position.pm
Handle streaming PGNs, like from Lichess (although this might break non-streaming...
[remoteglot] / Position.pm
index 8cfc4a6e8c6caac1b3d02d33c25ce27cc07b75cd..9e9fe29ac63af32ff5c31439c837ec3c4c7eeaae 100644 (file)
@@ -74,7 +74,7 @@ sub from_fen {
        if ($castling =~ /Q/) {
                $pos->{'white_castle_q'} = _col_num_to_letter(_find_piece_col($board->[7], 'R'));
        }
-       while ($castling =~ s/([A-H])//g) {
+       while ($castling =~ s/([A-H])//) {
                my $rook_col = lc($1);
                my $king_col = _col_num_to_letter(_find_piece_col($board->[7], 'K'));
                if ($rook_col lt $king_col) {
@@ -89,7 +89,7 @@ sub from_fen {
        if ($castling =~ /q/) {
                $pos->{'black_castle_q'} = _col_num_to_letter(_find_piece_col($board->[0], 'r'));
        }
-       while ($castling =~ s/([a-h])//g) {
+       while ($castling =~ s/([a-h])//) {
                my $rook_col = $1;
                my $king_col = _col_num_to_letter(_find_piece_col($board->[0], 'k'));
                if ($rook_col lt $king_col) {
@@ -191,6 +191,8 @@ sub fen {
 sub to_json_hash {
        my $pos = shift;
        my $json = { %$pos, fen => $pos->fen() };
+       delete $json->{'toplay'};
+       delete $json->{'move_num'};
        delete $json->{'board'};
        delete $json->{'prettyprint_cache'};
        delete $json->{'tbprobe_cache'};
@@ -275,8 +277,11 @@ sub make_move {
                $np->{'black_castle_k'} = undef;
        }
 
-       # 50-move rule.
-       if (lc($piece) eq 'p' || $dest_piece ne '-') {
+       # 50-move rule. Note that castle does not reset the counter, per FIDE rules.
+       my $castling = (lc($piece) eq 'k' && abs($from_col - $to_col) > 1) ||  # King moves two squares.
+               ($piece eq 'K' && $dest_piece eq 'R') ||  # Chess960-style king-takes-rook.
+               ($piece eq 'k' && $dest_piece eq 'r');
+       if (!$castling && (lc($piece) eq 'p' || $dest_piece ne '-')) {
                $np->{'time_since_100move_rule_reset'} = 0;
        } else {
                $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;