+sub from_fen {
+ my ($class, $fen) = @_;
+ my ($board, $toplay, $castling, $ep_square, $halfmove_clock, $fullmove_clock) = split / /, $fen;
+
+ my $pos = {};
+ $board =~ s/(\d)/"-"x$1/ge;
+ $pos->{'board'} = Board->new(split /\//, $board);
+ $board = $pos->{'board'};
+ $pos->{'toplay'} = uc($toplay);
+
+ if ($ep_square =~ /^([a-h])/) {
+ $pos->{'ep_file_num'} = ord($1) - ord('a');
+ } else {
+ $pos->{'ep_file_num'} = -1;
+ }
+
+ # X-FEN castling rights parsing.
+ if ($castling =~ /K/) {
+ $pos->{'white_castle_k'} = _col_num_to_letter(_find_piece_col_from_right($board->[7], 'R'));
+ }
+ if ($castling =~ /Q/) {
+ $pos->{'white_castle_q'} = _col_num_to_letter(_find_piece_col($board->[7], 'R'));
+ }
+ 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) {
+ $pos->{'white_castle_q'} = $rook_col;
+ } else {
+ $pos->{'white_castle_k'} = $rook_col;
+ }
+ }
+ if ($castling =~ /k/) {
+ $pos->{'black_castle_k'} = _col_num_to_letter(_find_piece_col_from_right($board->[0], 'r'));
+ }
+ if ($castling =~ /q/) {
+ $pos->{'black_castle_q'} = _col_num_to_letter(_find_piece_col($board->[0], 'r'));
+ }
+ 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) {
+ $pos->{'black_castle_q'} = $rook_col;
+ } else {
+ $pos->{'black_castle_k'} = $rook_col;
+ }
+ }
+ $pos->{'time_since_100move_rule_reset'} = $halfmove_clock // 0;
+ $pos->{'player_w'} = 'white';
+ $pos->{'player_b'} = 'black';
+ $pos->{'white_clock'} = 0;
+ $pos->{'black_clock'} = 0;
+ $pos->{'move_num'} = $fullmove_clock // 0;
+ $pos->{'last_move_uci'} = undef;
+ $pos->{'last_move'} = undef;
+ $pos->{'prettyprint_cache'} = {};
+ $pos->{'tbprobe_cache'} = {};
+
+ bless $pos, $class;
+ return $pos;
+}
+