Jump to content

Find Chess960 starting position identifier: Difference between revisions

m
Line 621:
=={{header|Perl}}==
{{trans|Raku}}
<lang perl>use strictv5.36;
use warnings;
use feature 'say';
use List::AllUtils 'indexes';
 
sub sp_id ($setup) {
8 == length $setup =~ m/R .* K .* R/x or diereturn 'Illegal position: Kingshould have notexactly betweeneight rooks.pieces';
my $setup = shift // 'RNBQKBNR';
1 == @{[ $setup =~ /$_/g ]} or return "Illegal position: should have exactly one $_" for <K Q>;
 
82 == length@{[ $setup =~ /$_/g ]} or diereturn '"Illegal position: should have exactly eighttwo pieces$_\'s" for <B N R>;
1 == @{[ $setup =~ m/$_R .* K .* R/gx ]} or diereturn "'Illegal position: shouldKing havenot exactly one $_" for <Kbetween Q>rooks.';
index($setup,'B')%2 =!= @{[ rindex($setup =~ /$_/g ]} ,'B')%2 or diereturn "'Illegal position: shouldBishops havenot exactlyon twoopposite $_\colors.'s" for <B N R>;
$setup =~ m/R .* K .* R/x or die 'Illegal position: King not between rooks.';
index($setup,'B')%2 != rindex($setup,'B')%2 or die 'Illegal position: Bishops not on opposite colors.';
 
my @knights = indexes { 'N' eq $_ } split '', $setup =~ s/[QB]//gr;
my $knight = indexes { join('', @knights) eq $_ } <01 02 03 04 12 13 14 23 24 34>; # combinations(5,2)
my @bishops = indexes { 'B' eq $_ } split '', $setup;
my $dark = int ((grep { $_ % 2 == 0 } @bishops)[0]) / 2;
my $light = int ((grep { $_ % 2 == 1 } @bishops)[0]) / 2;
my $queen = index(($setup =~ s/B//gr), 'Q');
 
int 4*(4*(6*$knight + $queen)+$dark)+$light;
}
 
say "$_ " . sp_id($_) for <QNRBBNKR RNBQKBNR RQNBBKRN RNQBBKRN QNBRBNKR>;</lang>
{{out}}
<pre>QNRBBNKR 105
RNBQKBNR 518
RQNBBKRN 601
RNQBBKRN 617</pre>
QNBRBNKR index($setup,'B')%2 != rindex($setup,'B')%2 or die 'Illegal position: Bishops not on opposite colors.';</pre>
 
=={{header|Phix}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.