Find Chess960 starting position identifier: Difference between revisions
Find Chess960 starting position identifier (view source)
Revision as of 15:49, 24 July 2022
, 1 year ago→{{header|Raku}}: streamline, style
(→{{header|Common Lisp}}: Add comments.) |
SqrtNegInf (talk | contribs) (→{{header|Raku}}: streamline, style) |
||
Line 776:
=={{header|Raku}}==
<lang perl6>
# standardize on letters for easier processing▼
▲# standardize on letters for easier processing
my %Names = <Q Queen K King R Rook N Knight B Bishop>;
▲my $ascii = $array.trans("♜♞♝♛♚♖♘♗♕♔" => "RNBQKRNBQK");
for <K 1 Q 1 B 2 N 2 R 2> -> $piece, $count {
}▼
# Work backwards through the placement rules.▼
▲# (optional error-checking)
# King and rooks are forced during placement, so ignore them. ▼
▲ die "Illegal position: should have exactly eight pieces\n";
# 1. Figure out which knight combination was used:▼
if +$ascii.indices($one) != 1 {▼
my $knight = combinations(5,2).kv.grep( -> $i, @c { @c eq @knights } ).flat.first;▼
▲ }
# 2. Then which queen position:▼
▲ die "Illegal position: should have exactly two $two\'s\n";
# 3. Finally the two bishops:▼
▲ die "Illegal position: King not between rooks.";
my ($dark,$light) = (@bishops.first %% 2 ?? @bishops !! @bishops.reverse) Xdiv 2;
$ascii.trans('RNBQK' => '♖♘♗♕♔') ~ ' ' ~ 4 × (4 × (6 × $knight + $queen) + $dark) + $light;
▲ die "Illegal position: Bishops not on opposite colors.";
}
▲# Work backwards through the placement rules.
▲# King and rooks are forced during placement, so ignore them.
▲# 1. Figure out which knight combination was used:
▲my $knight = combinations(5,2).kv.grep(
▲# 2. Then which queen position:
▲# 3. Finally the two bishops:
▲my @bishops = $ascii.indices('B');
say .&c960-spid for <♖♘♗♕♔♗♘♖ ♛♞♜♝♝♞♚♜ RQNBBKRN RNQBBKRN QNBRBNKR>;</lang>
{{
♕♘♖♗♗♘♔♖
▲♖♘♗♕♔♗♘♖: 518
♖♕♘♗♗♔♖♘
♖♘♕♗♗♔♖♘
Illegal position: Bishops not on opposite colors.</pre>
=={{header|Ruby}}==
|