Find Chess960 starting position identifier

From Rosetta Code
Revision as of 19:44, 11 September 2021 by Chunes (talk | contribs) (Add Factor)
Task
Find Chess960 starting position identifier
You are encouraged to solve this task according to the task description, using any language you may know.

As described on the Chess960 task, Chess960 (a.k.a Fischer Random Chess, Chess9LX) is a variant of chess where the array of pieces behind the pawns is randomized at the start of the game to minimize the value of opening theory "book knowledge". That task is to generate legal starting positions, and some of the solutions accept a standard Starting Position Identifier number ("SP-ID"), and generate the corresponding position.

Task

This task is to go the other way: given a starting array of pieces (provided in any form that suits your implementation, whether string or list or array, of letters or Unicode chess symbols or enum values, etc.), derive its unique SP-ID. For example, given the starting array QNRBBNKR (or ♕♘♖♗♗♘♔♖ or ♛♞♜♝♝♞♚♜), your (sub)program should return 105; given the starting lineup of standard chess, it should return 518.

You may assume the input is a valid Chess960 position; detecting invalid input (including illegal characters or starting arrays with the bishops on the same color square or the king not between the two rooks) is optional.

Algorithm

The derivation is the inverse of the algorithm given at Wikipedia, and goes like this (we'll use the standard chess setup as an example):

1. Ignoring the Queen and Bishops, find the positions of the Knights within the remaining five spaces (in the standard array they're in the second and fourth positions), and then find the index number of that combination. There's a table at the above Wikipedia article, but it's just the possible positions sorted left to right and numbered 0 to 9: 0=NN---, 1=N-N--, 2=N--N-, 3=N---N, 4=-NN--, etc; our pair is combination number 5. Call this number N. N=5

2. Now ignoring the Knights (but including the Queen and Bishops), find the position of the Queen in the remaining 6 spaces; number them 0..5 from left to right and call the index of the Queen's position Q. In our example, Q=2.

3. Finally, find the positions of the two bishops within their respective sets of four like-colored squares. It's important to note here that the board in chess is placed such that the leftmost position on the home row is on a dark square and the rightmost a light. So if we number the dark squares 0..3, the dark bishop in the standard position is on square 1 (D=1), and the light bishop is on square 2 (L=2).

4. Then the position number is given by 4(4(6N + Q)+D)+L, which reduces to 96N + 16Q + 4D + L. In our example, that's 96×5 + 16×2 + 4×1 + 2 = 480 + 32 + 4 + 2 = 518.

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: assocs assocs.extras combinators formatting kernel literals math math.combinatorics sequences sequences.extras sets strings ;


! ====== optional error-checking ======

check-length ( str -- )
   length 8 = [ "Must have 8 pieces." throw ] unless ;
check-one ( str -- )
   "KQ" counts [ nip 1 = not ] assoc-find nip
   [ 1string "Must have one %s." sprintf throw ] [ drop ] if ;
check-two ( str -- )
   "BNR" counts [ nip 2 = not ] assoc-find nip
   [ 1string "Must have two %s." sprintf throw ] [ drop ] if ;
check-king ( str -- )
   "QBN" without "RKR" =
   [ "King must be between rooks." throw ] unless ;
check-bishops ( str -- )
   CHAR: B swap indices sum odd?
   [ "Bishops must be on opposite colors." throw ] unless ;
check-sp ( str -- )
   {
       [ check-length ]
       [ check-one ]
       [ check-two ]
       [ check-king ]
       [ check-bishops ]
   } cleave ;

! ====== end optional error-checking ======


CONSTANT: convert $[ "RNBQK" "♖♘♗♕♔" zip ]

CONSTANT: table $[ "NN---" all-unique-permutations ]

knightify ( str -- newstr )
   [ dup CHAR: N = [ drop CHAR: - ] unless ] map ;
n ( str -- n ) "QB" without knightify table index ;
q ( str -- q ) "N" without CHAR: Q swap index ;
d ( str -- d ) CHAR: B swap <evens> index ;
l ( str -- l ) CHAR: B swap <odds> index ;
sp-id ( str -- n )
   dup check-sp
   { [ n 96 * ] [ q 16 * + ] [ d 4 * + ] [ l + ] } cleave ;
sp-id. ( str -- )
   dup [ convert substitute ] [ sp-id ] bi
   "%s / %s: %d\n" printf ;

"QNRBBNKR" sp-id. "RNBQKBNR" sp-id.</lang>

Output:
QNRBBNKR / ♕♘♖♗♗♘♔♖: 105
RNBQKBNR / ♖♘♗♕♔♗♘♖: 518

Raku

<lang raku>#!/usr/bin/env raku

  1. derive a chess960 position's SP-ID

unit sub MAIN($array = "♖♘♗♕♔♗♘♖");

  1. standardize on letters for easier processing

my $ascii = $array.trans("♜♞♝♛♚♖♘♗♕♔" => "RNBQKRNBQK");

  1. (optional error-checking)

if $ascii.chars != 8 {

   die "Illegal position: should have exactly eight pieces\n";

}

for «K Q» -> $one {

 if +$ascii.indices($one) != 1 {
   die "Illegal position: should have exactly one $one\n";
 }

}

for «B N R» -> $two {

 if +$ascii.indices($two) != 2 {
   die "Illegal position: should have exactly two $two\'s\n";
 }

}

if $ascii !~~ /'R' .* 'K' .* 'R'/ {

 die "Illegal position: King not between rooks.";

}

if [+]($ascii.indices('B').map(* % 2)) != 1 {

 die "Illegal position: Bishops not on opposite colors.";

}

  1. (end optional error-checking)
  1. Work backwards through the placement rules.
  2. King and rooks are forced during placement, so ignore them.
  1. 1. Figure out which knight combination was used:

my @knights = $ascii

 .subst(/<[QB]>/,,:g)
 .indices('N');

my $knight = combinations(5,2).kv.grep(

   -> $i,@c { @c eq @knights }
 )[0][0]; 
  1. 2. Then which queen position:

my $queen = $ascii

 .subst(/<[B]>/,,:g)
 .index('Q');
  1. 3. Finally the two bishops:

my @bishops = $ascii.indices('B'); my $dark = @bishops.grep({ $_ % 2 == 0 })[0] div 2; my $light = @bishops.grep({ $_ % 2 == 1 })[0] div 2;

my $sp-id = 4*(4*(6*$knight + $queen)+$dark)+$light;

  1. standardize output

my $display = $ascii.trans("RNBQK" => "♖♘♗♕♔");

say "$display: $sp-id";</lang>

Output:
$ c960spid QNRBBNKR
♕♘♖♗♗♘♔♖: 105
$ c960spid
♖♘♗♕♔♗♘♖: 518