}
}
} else {
- if ($promo ne '') {
+ if (defined($promo) && $promo ne '') {
if ($piece eq 'p') {
$piece = $promo;
} else {
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 = ();
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;
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;