X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=Position.pm;h=9333bcd58af514badb9a7b31463a4c00763da609;hp=777571e121d1ba5fc7301d54a6a6b7e1926cb202;hb=41591b89435e7b052b4f953dc78d074662803677;hpb=7463b32c4c0a899e591a90d365761ee670c6eb0f diff --git a/Position.pm b/Position.pm index 777571e..9333bcd 100644 --- a/Position.pm +++ b/Position.pm @@ -4,6 +4,7 @@ # use strict; use warnings; +use MIME::Base64; require 'Board.pm'; @@ -28,7 +29,13 @@ sub new { $pos->{'player_w'} =~ s/^W?[FCIG]M//; $pos->{'player_b'} =~ s/^W?[FCIG]M//; $pos->{'move_num'} = $x[26]; + if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) { + $pos->{'last_move_uci'} = $1 . $2; + } else { + $pos->{'last_move_uci'} = undef; + } $pos->{'last_move'} = $x[29]; + $pos->{'prettyprint_cache'} = {}; bless $pos, $class; return $pos; @@ -36,6 +43,8 @@ sub new { sub start_pos { my ($class, $white, $black) = @_; + $white = "base64:" . MIME::Base64::encode_base64($white); + $black = "base64:" . MIME::Base64::encode_base64($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"); } @@ -103,7 +112,21 @@ sub fen { sub to_json_hash { my $pos = shift; - return { %$pos, board => undef, fen => $pos->fen() }; + my $json = { %$pos, fen => $pos->fen() }; + delete $json->{'board'}; + delete $json->{'prettyprint_cache'}; + delete $json->{'black_castle_k'}; + delete $json->{'black_castle_q'}; + delete $json->{'white_castle_k'}; + delete $json->{'white_castle_q'}; + delete $json->{'time_since_100move_rule_reset'}; + if ($json->{'player_w'} =~ /^base64:(.*)$/) { + $json->{'player_w'} = MIME::Base64::decode_base64($1); + } + if ($json->{'player_b'} =~ /^base64:(.*)$/) { + $json->{'player_b'} = MIME::Base64::decode_base64($1); + } + return $json; } sub parse_pretty_move { @@ -111,6 +134,11 @@ sub parse_pretty_move { return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'}); } +sub num_pieces { + my ($pos) = @_; + return $pos->{'board'}->num_pieces(); +} + # Returns a new Position object. sub make_move { my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_; @@ -169,9 +197,21 @@ sub make_move { $np->{'player_b'} = $pos->{'player_b'}; my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo); $np->{'last_move'} = $move; + $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo); + return bless $np; } +# Returns a new Position object, and the parsed UCI move. +sub make_pretty_move { + my ($pos, $move) = @_; + + my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move); + my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo); + $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo); + return ($pos, $uci_move); +} + sub _pos_to_square { my ($row, $col) = @_; return sprintf("%c%d", ord('a') + $col, 8 - $row);