Author: Steffan Westcott
Date: 13:58:05 02/23/05
Go up one level in this thread
On February 23, 2005 at 03:01:50, Uri Blass wrote:
>I plan to implement something more complex(generating all fen with 4 pieces)
>
>I want to have a loop on all the possible combination of 4 pieces and for every
>combination to have function that generate all the possible fen.
Uri,
The program below will do this for you, I hope you find it useful.
I generalised my earlier Perl program in two respects to increase flexibility :
(Example 1) It will generate all positions using a given collection of pieces.
Duplicates in the collection are catered for eg. KPPk
(Example 2) It will generate all positions using a mandatory collection, plus
all possible selections from another collection, up to a given number of pieces
e.g. Kk (mandatory) plus any 2 selections from QRNBPqrnbp. Again, duplicates are
catered for e.g. In this example, KPPk will be included as one possible
selection.
Example 2 in the code below will generate all 4 piece FENs, as you wanted. The
output is *very* large!
Note that this program also filters out two classes of illegal position :
(1) Pawns on 1st or 8th rank
(2) Adjacent kings
Cheers,
Steffan
##################
sub perm {
my ($p, $a) = @_;
if (length $p) {
$a .= (my $x = chop $p);
do perm($p,$a) until (($a=(chop $a).$a) =~ /$x$/);
} else {
$_ = $a ;
s/.{8}(?=.)/$&\//g ; #insert '/' between ranks
return if /^.{0,7}[Pp]/ || /[Pp].{0,7}$/ ; #no pawns on 1st or 8th rank
return if /[Kk][Kk]/ || /[Kk].{7,9}[Kk]/ ; #no adjacent kings
s/ +(?{$e=length $&})/$e/g ; #coalesce spaces to digits
print "$_ w - - 0 1\n";
}
}
sub place { perm($e = shift, " " x (64-length $e)); }
sub sel {
my ($q, $r, $i) = @_; # copy and add $i more pieces from $r to $q
if ($i) {
my $t = chop $r;
sel($q.$t, $r.$t, $i-1);
sel($q, $r, $i) if length $r;
} else {
place($q); # print all FENs for this choice of pieces
};
}
##################
#Example 1 : All KPPk positions
place("KPPk");
#Example 2 : All positions with both kings, and 2 other pieces
# i.e. Kkxx, KXkx and KXXk
sel("Kk", "QRNBPqrnbp", 2);
#NB: The only illegal positions filtered are those with pawns on 1st
# or 8th rank, or both kings adjacent to each other
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.