sub pick-FEN {
my $n = (2..32).pick;
my @n = (^64).pick($n);
return pick-FEN() unless
my @kings[2] = first -> [$a, $b] {
$a !== $b && abs($a div 8 - $b div 8) | abs($a mod 8 - $b mod 8) > 1
}, (@n X @n);
my @pieces = <p P n N b B r R q Q>;
my @k = <K k>.pick(*);
return (gather for ^64 -> $sq {
if $sq == @kings.any { take @k.shift }
elsif $sq == @n.any {
my $row = 7 - $sq div 8;
take
$row == 7 ?? @pieces.grep(none('P')).pick !!
$row == 0 ?? @pieces.grep(none('p')).pick !!
@pieces.pick;
}
else { take 'ø' }
}).rotor(8)».join».subst(/ø+/,{ .chars }, :g).join('/') ~ ' w - - 0 1';
}
say pick-FEN();
Output:
q2n1n2/1Qpk3Q/1r3bP1/1b1b4/2pRBR2/4P1bN/2R3K1/N1r2rPB w - - 0 1