]> git.sesse.net Git - remoteglot/commitdiff
Add some routines that are useful to parse PGNs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 15 Jun 2014 14:29:59 +0000 (16:29 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 15 Jun 2014 14:29:59 +0000 (16:29 +0200)
Board.pm
Position.pm

index 6af89f39819d16e520d4ff753f0b3db161b901e2..b925a388952ce6c5cc6e5ae1d35d00a5898d6171 100644 (file)
--- a/Board.pm
+++ b/Board.pm
@@ -109,7 +109,7 @@ sub make_move {
                                }
                        }
                } else {
-                       if ($promo ne '') {
+                       if (defined($promo) && $promo ne '') {
                                if ($piece eq 'p') {
                                        $piece = $promo;
                                } else {
@@ -131,12 +131,78 @@ sub _pos_to_square {
        return sprintf("%c%d", ord('a') + $col, 8 - $row);
 }
 
+sub _col_letter_to_num {
+        return ord(shift) - ord('a');
+}
+
+sub _row_letter_to_num {
+        return 7 - (ord(shift) - ord('1'));
+}
+
+sub _square_to_pos {
+       my ($square) = @_;
+       $square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";  
+       return (_row_letter_to_num($2), _col_letter_to_num($1));
+}
+
 sub _move_to_uci_notation {
        my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
        $promo //= "";
        return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
 }
 
+sub parse_pretty_move {
+       my ($board, $move, $toplay) = @_;
+
+       # Strip check or mate
+       $move =~ s/[+#]$//;
+
+       if ($move eq '0-0' or $move eq 'O-O') {
+               if ($toplay eq 'W') {
+                       return (_square_to_pos('e1'), _square_to_pos('g1'));
+               } else {
+                       return (_square_to_pos('e8'), _square_to_pos('g8'));
+               }
+       } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
+               if ($toplay eq 'W') {
+                       return (_square_to_pos('e1'), _square_to_pos('c1'));
+               } else {
+                       return (_square_to_pos('e8'), _square_to_pos('c8'));
+               }
+       }
+
+       # Parse promo
+       my $promo;
+       if ($move =~ s/=([QRNB])$//) {
+               $promo = $1;
+       }
+
+       $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
+       my $piece = $1 // 'P';
+       my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
+       my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
+       my ($to_row, $to_col) = _square_to_pos($4);
+       
+       # Find all possible from-squares that could have been meant.
+       my @squares = ();
+       if ($toplay eq 'B') {
+               $piece = lc($piece);
+       }
+       for my $row (0..7) {
+               next if (defined($from_row) && $from_row != $row);
+               for my $col (0..7) {
+                       next if (defined($from_col) && $from_col != $col);
+                       next if ($board->[$row][$col] ne $piece);
+                       next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
+                       push @squares, [ $row, $col ];
+               }
+       }
+       if (scalar @squares != 1) {
+               die "Ambigious or impossible move $move";
+       }
+       return (@{$squares[0]}, $to_row, $to_col, $promo);
+}
+
 sub fen {
        my ($board) = @_;
        my @rows = ();
index e2c9801d7c5957abbaf5eca6eb4437424c6e9887..7834ca065185ad005be02cf06d252756b08dd3ae 100644 (file)
@@ -34,6 +34,11 @@ sub new {
        return $pos;
 }
 
+sub start_pos {
+       my ($class, $white, $black) = @_;
+       return $class->new("<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 dummygamenum $white $black -2 dummytime dummyincrement 39 39 dummytime dummytime 1 none (0:00) none 0 0 0");
+}
+
 sub fen {
        my $pos = shift;
 
@@ -101,4 +106,74 @@ sub to_json_hash {
        return { %$pos, board => undef, fen => $pos->fen() };
 }
 
+sub parse_pretty_move {
+        my ($pos, $move) = @_;
+       return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
+}
+
+# Returns a new Position object.
+sub make_move {
+        my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
+
+       my $from_square = _pos_to_square($from_row, $from_col);
+       my $to_square = _pos_to_square($to_row, $to_col);
+
+       my $np = {};
+       $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
+       if ($pos->{'toplay'} eq 'W') {
+               $np->{'toplay'} = 'B';
+               $np->{'move_num'} = $pos->{'move_num'};
+       } else {
+               $np->{'toplay'} = 'W';
+               $np->{'move_num'} = $pos->{'move_num'} + 1;
+       }
+
+       my $piece = $pos->{'board'}[$from_row][$from_col];
+       my $dest_piece = $pos->{'board'}[$to_row][$to_col];
+
+       # Find out if this was a two-step pawn move.
+       if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
+               $np->{'ep_file_num'} = $from_col;
+       } else {
+               $np->{'ep_file_num'} = -1;
+       }
+
+       # Castling rights.
+       $np->{'white_castle_k'} = $pos->{'white_castle_k'};
+       $np->{'white_castle_q'} = $pos->{'white_castle_q'};
+       $np->{'black_castle_k'} = $pos->{'black_castle_k'};
+       $np->{'black_castle_q'} = $pos->{'black_castle_q'};
+       if ($piece eq 'K') {
+               $np->{'white_castle_k'} = 0;
+               $np->{'white_castle_q'} = 0;
+       } elsif ($piece eq 'k') {
+               $np->{'black_castle_k'} = 0;
+               $np->{'black_castle_q'} = 0;
+       } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
+               $np->{'white_castle_q'} = 0;
+       } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
+               $np->{'white_castle_k'} = 0;
+       } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
+               $np->{'black_castle_q'} = 0;
+       } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
+               $np->{'black_castle_k'} = 0;
+       }
+
+       # 50-move rule.
+       if (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;
+       }
+       $np->{'player_w'} = $pos->{'player_w'};
+       $np->{'player_b'} = $pos->{'player_b'};
+       $np->{'last_move'} = '(move)';  # FIXME
+       return bless $np;
+}
+
+sub _pos_to_square {
+        my ($row, $col) = @_;
+        return sprintf("%c%d", ord('a') + $col, 8 - $row);
+}
+
 1;