I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# Find Chess960 starting position identifier

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 page, 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.

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 squares of each color 0..3 from left to right, 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.

## BASIC

### Commodore BASIC

Works with: Commodore BASIC version 2.0

Unlike the solution for the reverse task, which uses DO/LOOP and so requires at least Commodore BASIC 3.5, this should work on any version.

`100 REM DERIVE SP-ID FROM CHESS960 POS110 PRINT "ENTER START ARRAY AS SEEN BY WHITE."120 PRINT: PRINT "STARTING ARRAY:";130 OPEN 1,0: INPUT#1, AR\$: CLOSE 1: PRINT140 IF LEN(AR\$)=0 THEN END150 IF LEN(AR\$)=8 THEN 170160 PRINT "ARRAY MUST BE 8 PIECES.": GOTO 120170 FOR I=1 TO 8180 : P\$=MID\$(AR\$,I,1)190 : IF P\$="Q" THEN Q(Q)=I: Q=Q+1:  GOTO 250200 : IF P\$="K" THEN K(K)=I: K=K+1:  GOTO 250210 : IF P\$="B" THEN B(B)=I: B=B+1:  GOTO 250220 : IF P\$="N" THEN N(N)=I: N=N+1:  GOTO 250230 : IF P\$="R" THEN R(R)=I: R=R+1:  GOTO 250240 : PRINT "ILLEGAL PIECE '"P\$"'.": GOTO 120250 NEXT I260 IF K<>1 THEN PRINT "THERE MUST BE EXACTLY ONE KING.":    GOTO 120270 IF Q<>1 THEN PRINT "THERE MUST BE EXACTLY ONE QUEEN.":   GOTO 120280 IF B<>2 THEN PRINT "THERE MUST BE EXACTLY TWO BISHOPS.": GOTO 120290 IF N<>2 THEN PRINT "THERE MUST BE EXACTLY TWO KNIGHTS.": GOTO 120300 IF R<>2 THEN PRINT "THERE MUST BE EXACTLY TWO ROOKS.":   GOTO 120310 IF (K(0) > R(0)) AND (K(0) < R(1)) THEN 330320 PRINT "KING MUST BE BETWEEN THE ROOKS.": GOTO 120330 IF (B(0) AND 1) <> (B(1) AND 1) THEN 350340 PRINT "BISHOPS MUST BE ON OPPOSITE COLORS.": GOTO 120350 FOR I=0 TO 1360 : N=N(I)370 : IF N(I)>Q(I) THEN N=N-1380 : FOR J=0 TO 1390 :  IF N(I)>B(J) THEN N=N-1400 : NEXT J410 : N(I)=N420 NEXT I430 N0=1: N1=2440 FOR N=0 TO 9450 : IF N0=N(0) AND N1=N(1) THEN 490460 : N1=N1+1470 : IF N1>5 THEN N0=N0+1: N1=N0+1480 NEXT N490 Q=Q(0)-1500 FOR I=0 TO 1510 : IF Q(0)>N(I) THEN Q=Q-1520 NEXT I530 FOR I=0 TO 1540 : B=B(I)-1550 : IF B AND 1 THEN L=INT(B/2)560 : IF (B AND 1)=0 THEN D=B/2570 NEXT I580 PRINT "SPID ="; 96*N+16*Q+4*D+L`
Output:
```READY.
RUN
ENTER START ARRAY AS SEEN BY WHITE.

STARTING ARRAY:RNBQKBNR
SPID = 518

RUN
ENTER START ARRAY AS SEEN BY WHITE.

STARTING ARRAY:QNRBBNKR
SPID = 105

### FreeBASIC

`Sub SP_ID(PosicPiezas As String)    Dim As String pieza    Dim As Integer pQ(), pK(), pB(), pN(), pR(), i, j    Dim As Integer Q, K, B, N, R, L, D     For i = 1 To 8        pieza = Mid(PosicPiezas, i, 1)        Select Case pieza        Case "Q"            Redim Preserve pQ(Q) : pQ(Q) = i: Q += 1        Case "K"            Redim Preserve pK(K) : pK(K) = i: K += 1        Case "B"            Redim Preserve pB(B) : pB(B) = i: B += 1        Case "N"            Redim Preserve pN(N) : pN(N) = i: N += 1        Case "R"            Redim Preserve pR(R) : pR(R) = i: R += 1        Case Else             Print "ILLEGAL PIECE '"; pieza; "'.": Exit Sub        End Select    Next i     If K <> 1 Then Print "THERE MUST BE EXACTLY ONE KING."    If Q <> 1 Then Print "THERE MUST BE EXACTLY ONE QUEEN."    If B <> 2 Then Print "THERE MUST BE EXACTLY TWO BISHOPS."    If N <> 2 Then Print "THERE MUST BE EXACTLY TWO KNIGHTS."    If R <> 2 Then Print "THERE MUST BE EXACTLY TWO ROOKS."    If Not (pK(0) > pR(0)) And (pK(0) < pR(1)) Then Print "KING MUST BE BETWEEN THE ROOKS."    If Not (pB(0) And 1) <> (pB(1) And 1) Then Print "BISHOPS MUST BE ON OPPOSITE COLORS."     For i = 0 To 1        N = pN(i)        If pN(i) > pQ(i) Then N -= 1        For j = 0 To 1            If pN(i) > pB(j) Then N -= 1        Next j        pN(i) = N    Next i     Dim As Integer N0 = 1, N1 = 2    For i = 0 To 9        If N0 = pN(0) And N1 = pN(1) Then Exit For        N1 += 1        If N1 > 5 Then N0 += 1: N1 = N0 + 1    Next i     Q = pQ(0) - 1    For i = 0 To 1        If pQ(0) > pN(i) Then Q -= 1    Next i    For i = 0 To 1        B = pB(i) - 1        If B And 1 Then L = Int(B / 2)        If (B And 1) = 0 Then D = B / 2    Next i     Print PosicPiezas; " has SP_ID of"; 96 * N + 16 * Q + 4 * D + LEnd Sub SP_ID("QNRBBNKR")PrintSP_ID("RNBQKBNR")Sleep`
Output:
```QNRBBNKR has SP_ID of 105

RNBQKBNR has SP_ID of 518```

### QBasic

Works with: QBasic version 1.1
Translation of: Commodore BASIC
`CLSPRINT "ENTER START ARRAY AS SEEN BY WHITE."120 PRINTPRINT "STARTING ARRAY:";INPUT AR\$PRINTIF LEN(AR\$) = 0 THEN ENDIF LEN(AR\$) = 8 THEN 170PRINT "ARRAY MUST BE 8 PIECES.": GOTO 120170 FOR I = 1 TO 8P\$ = MID\$(AR\$, I, 1)IF P\$ = "Q" THEN Q(Q) = I: Q = Q + 1: GOTO 250IF P\$ = "K" THEN K(K) = I: K = K + 1: GOTO 250IF P\$ = "B" THEN B(B) = I: B = B + 1: GOTO 250IF P\$ = "N" THEN N(N) = I: N = N + 1: GOTO 250IF P\$ = "R" THEN R(R) = I: R = R + 1: GOTO 250PRINT "ILLEGAL PIECE '"; P\$; "'.": GOTO 120250 NEXT I IF K <> 1 THEN PRINT "THERE MUST BE EXACTLY ONE KING.":  GOTO 120IF Q <> 1 THEN PRINT "THERE MUST BE EXACTLY ONE QUEEN.": GOTO 120IF B <> 2 THEN PRINT "THERE MUST BE EXACTLY TWO BISHOPS.": GOTO 120IF N <> 2 THEN PRINT "THERE MUST BE EXACTLY TWO KNIGHTS.": GOTO 120IF R <> 2 THEN PRINT "THERE MUST BE EXACTLY TWO ROOKS.": GOTO 120IF (K(0) > R(0)) AND (K(0) < R(1)) THEN 330PRINT "KING MUST BE BETWEEN THE ROOKS.": GOTO 120330 IF (B(0) AND 1) <> (B(1) AND 1) THEN 350PRINT "BISHOPS MUST BE ON OPPOSITE COLORS.": GOTO 120350 FOR I = 0 TO 1N = N(I)IF N(I) > Q(I) THEN N = N - 1FOR J = 0 TO 1    IF N(I) > B(J) THEN N = N - 1NEXT JN(I) = NNEXT IN0 = 1: N1 = 2 FOR N = 0 TO 9    IF N0 = N(0) AND N1 = N(1) THEN 490    N1 = N1 + 1    IF N1 > 5 THEN N0 = N0 + 1: N1 = N0 + 1NEXT N490 Q = Q(0) - 1 FOR I = 0 TO 1    IF Q(0) > N(I) THEN Q = Q - 1NEXT I FOR I = 0 TO 1    B = B(I) - 1    IF B AND 1 THEN L = INT(B / 2)    IF (B AND 1) = 0 THEN D = B / 2NEXT IPRINT "SPID ="; 96 * N + 16 * Q + 4 * D + LEND`
Output:
```Igual que la entrada de Commodore BASIC.
```

## Factor

Works with: Factor version 0.99 2021-06-02
`USING: assocs assocs.extras combinators formatting kernelliterals math math.combinatorics sequences sequences.extras setsstrings ;  ! ====== 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.`
Output:
```QNRBBNKR / ♕♘♖♗♗♘♔♖: 105
RNBQKBNR / ♖♘♗♕♔♗♘♖: 518
```

## Go

Translation of: Wren
`package main import (    "fmt"    "log"    "strings") var glyphs = []rune("♜♞♝♛♚♖♘♗♕♔")var names = map[rune]string{'R': "rook", 'N': "knight", 'B': "bishop", 'Q': "queen", 'K': "king"}var g2lMap = map[rune]string{    '♜': "R", '♞': "N", '♝': "B", '♛': "Q", '♚': "K",    '♖': "R", '♘': "N", '♗': "B", '♕': "Q", '♔': "K",} var ntable = map[string]int{"01": 0, "02": 1, "03": 2, "04": 3, "12": 4, "13": 5, "14": 6, "23": 7, "24": 8, "34": 9} func g2l(pieces string) string {    lets := ""    for _, p := range pieces {        lets += g2lMap[p]    }    return lets} func spid(pieces string) int {    pieces = g2l(pieces) // convert glyphs to letters     /* check for errors */    if len(pieces) != 8 {        log.Fatal("There must be exactly 8 pieces.")    }    for _, one := range "KQ" {        count := 0        for _, p := range pieces {            if p == one {                count++            }        }        if count != 1 {            log.Fatalf("There must be one %s.", names[one])        }    }    for _, two := range "RNB" {        count := 0        for _, p := range pieces {            if p == two {                count++            }        }        if count != 2 {            log.Fatalf("There must be two %s.", names[two])        }    }    r1 := strings.Index(pieces, "R")    r2 := strings.Index(pieces[r1+1:], "R") + r1 + 1    k := strings.Index(pieces, "K")    if k < r1 || k > r2 {        log.Fatal("The king must be between the rooks.")    }    b1 := strings.Index(pieces, "B")    b2 := strings.Index(pieces[b1+1:], "B") + b1 + 1    if (b2-b1)%2 == 0 {        log.Fatal("The bishops must be on opposite color squares.")    }     /* compute SP_ID */    piecesN := strings.ReplaceAll(pieces, "Q", "")    piecesN = strings.ReplaceAll(piecesN, "B", "")    n1 := strings.Index(piecesN, "N")    n2 := strings.Index(piecesN[n1+1:], "N") + n1 + 1    np := fmt.Sprintf("%d%d", n1, n2)    N := ntable[np]     piecesQ := strings.ReplaceAll(pieces, "N", "")    Q := strings.Index(piecesQ, "Q")     D := strings.Index("0246", fmt.Sprintf("%d", b1))    L := strings.Index("1357", fmt.Sprintf("%d", b2))    if D == -1 {        D = strings.Index("0246", fmt.Sprintf("%d", b2))        L = strings.Index("1357", fmt.Sprintf("%d", b1))    }     return 96*N + 16*Q + 4*D + L} func main() {    for _, pieces := range []string{"♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖"} {        fmt.Printf("%s or %s has SP-ID of %d\n", pieces, g2l(pieces), spid(pieces))    }}`
Output:
```♕♘♖♗♗♘♔♖ or QNRBBNKR has SP-ID of 105
♖♘♗♕♔♗♘♖ or RNBQKBNR has SP-ID of 518
```

## Julia

`const whitepieces = "♖♘♗♕♔♗♘♖♙"const whitechars = "rnbqkp"const blackpieces = "♜♞♝♛♚♝♞♜♟"const blackchars = "RNBQKP"const piece2ascii = Dict(zip("♖♘♗♕♔♗♘♖♙♜♞♝♛♚♝♞♜♟", "rnbqkbnrpRNBQKBNRP")) """ Derive a chess960 position's SP-ID from its string representation. """function chess960spid(position::String = "♖♘♗♕♔♗♘♖", errorchecking = true)    if errorchecking        @assert length(position) == 8 "Need exactly 8 pieces"        @assert all(p -> p in whitepieces || p in blackpieces, position) "Invalid piece character"        @assert all(p -> p in whitepieces, position) || all(p -> p in blackpieces, position) "Side of pieces is mixed"        @assert all(p -> !(p in "♙♟"), position) "No pawns allowed"    end    a = uppercase(String([piece2ascii[c] for c in position]))     if errorchecking        @assert all(p -> count(x -> x == p, a) == 1, "KQ") "Need exactly one of each K and Q"        @assert all(p -> count(x -> x == p, a) == 2, "RNB") "Need exactly 2 of each R, N, B"        @assert findfirst(p -> p == 'R', a) < findfirst(p -> p == 'K', a) < findlast(p -> p == 'R', a) "King must be between rooks"        @assert isodd(findfirst(p -> p == 'B', a) + findlast(p -> p == 'B', a)) "Bishops must be on different colors"    end     knighttable = [12, 13, 14, 15, 23, 24, 25, 34, 35, 45]    noQB = replace(a, r"[QB]" => "")    knightpos1, knightpos2 = findfirst(c -> c =='N', noQB), findlast(c -> c =='N', noQB)    N = findfirst(s -> s == 10 * knightpos1 + knightpos2, knighttable) - 1    Q = findfirst(c -> c == 'Q', replace(a, "N" => "")) - 1    bishoppositions = [findfirst(c -> c =='B', a), findlast(c -> c =='B', a)]    if isodd(bishoppositions)        bishoppositions = reverse(bishoppositions) # dark color bishop first    end    D, L = bishoppositions ÷ 2, bishoppositions ÷ 2 - 1     return 96N + 16Q + 4D + Lend for position in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖"]    println(collect(position), " => ", chess960spid(position))end `
Output:
```['♕', '♘', '♖', '♗', '♗', '♘', '♔', '♖'] => 105
['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖'] => 518
```

## Nim

Translation of: Wren
`import sequtils, strformat, strutils, sugar, tables, unicode type Piece {.pure.} = enum Rook = "R", Knight = "N", Bishop = "B", Queen = "Q", King = "K" const  GlypthToPieces = {"♜": Rook, "♞": Knight, "♝": Bishop, "♛": Queen, "♚": King,                    "♖": Rook, "♘": Knight, "♗": Bishop, "♕": Queen, "♔": King}.toTable  Names = [Rook: "rook", Knight: "knight", Bishop: "bishop", Queen: "queen", King: "king"]  NTable = {[0, 1]: 0, [0, 2]: 1, [0, 3]: 2, [0, 4]: 3, [1, 2]: 4,            [1, 3]: 5, [1, 4]: 6, [2, 3]: 7, [2, 4]: 8, [3, 4]: 9}.toTable func toPieces(glyphs: string): seq[Piece] =  collect(newSeq, for glyph in glyphs.runes: GlypthToPieces[glyph.toUTF8]) func isEven(n: int): bool = (n and 1) == 0 func positions(pieces: seq[Piece]; piece: Piece): array[2, int] =  var idx = 0  for i, p in pieces:    if p == piece:      result[idx] = i      inc idx func spid(glyphs: string): int =   let pieces = glyphs.toPieces()   # Check for errors.  if pieces.len != 8:    raise newException(ValueError, "there must be exactly 8 pieces.")  for piece in [King, Queen]:    if pieces.count(piece) != 1:      raise newException(ValueError, &"there must be one {Names[piece]}.")  for piece in [Rook, Knight, Bishop]:    if pieces.count(piece) != 2:      raise newException(ValueError, &"there must be two {Names[piece]}s.")  let r = pieces.positions(Rook)  let k = pieces.find(King)  if k < r or k > r:    raise newException(ValueError, "the king must be between the rooks.")  var b = pieces.positions(Bishop)  if isEven(b - b):    raise newException(ValueError, "the bishops must be on opposite color squares.")   # Compute SP_ID.  let piecesN = pieces.filterIt(it notin [Queen, Bishop])  let n = NTable[piecesN.positions(Knight)]   let piecesQ = pieces.filterIt(it != Knight)  let q = piecesQ.find(Queen)   if b.isEven: swap b, b  let d = [0, 2, 4, 6].find(b)  let l = [1, 3, 5, 7].find(b)   result = 96 * n + 16 * q + 4 * d + l  for glyphs in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖"]:  echo &"{glyphs} or {glyphs.toPieces().join()} has SP-ID of {glyphs.spid()}"`
Output:
```♕♘♖♗♗♘♔♖ or QNRBBNKR has SP-ID of 105
♖♘♗♕♔♗♘♖ or RNBQKBNR has SP-ID of 518```

## Perl

Translation of: Raku
`use strict;use warnings;use feature 'say';use List::AllUtils 'indexes'; sub sp_id {    my \$setup = shift // 'RNBQKBNR';     8 == length \$setup                          or die 'Illegal position: should have exactly eight pieces';    1 == @{[ \$setup =~ /\$_/g ]}                 or die "Illegal position: should have exactly one \$_"        for <K Q>;    2 == @{[ \$setup =~ /\$_/g ]}                 or die "Illegal position: should have exactly two \$_\'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)) / 2;    my \$light = int ((grep { \$_ % 2 == 1 } @bishops)) / 2;    my \$queen = index((\$setup =~ s/B//gr), 'Q');     int 4*(4*(6*\$knight + \$queen)+\$dark)+\$light;} say "\$_ " . sp_id(\$_) for <QNRBBNKR RNBQKBNR>;`
Output:
```QNRBBNKR 105
RNBQKBNR 518```

## Phix

```with javascript_semantics
function spid(string s)
if sort(s)!="BBKNNQRR" then return -1 end if
if filter(s,"in","RK")!="RKR" then return -1 end if
sequence b = find_all('B',s)
if even(sum(b)) then return -1 end if
integer {n1,n2} = find_all('N',filter(s,"out","QB")),
N = {-2,1,3,4}[n1]+n2,
Q = find('Q',filter(s,"!=",'N'))-1,
D = filter(b,odd)-1, -- (nb not /2)
L = filter(b,even)/2-1
return 96*N + 16*Q + 2*D + L
end function

procedure test(string s)
printf(1,"%s : %d\n",{s,spid(s)})
end procedure

test("QNRBBNKR")
test("RNBQKBNR")
```
Output:
```QNRBBNKR : 105
RNBQKBNR : 518
```

To support all those crazy unicode characters just change the start of spid() to:

```function spid(string u)
sequence u32 = utf8_to_utf32(u),
c32 = utf8_to_utf32("♜♞♝♛♚♖♘♗♕♔"),
s32 = substitute_all(u32,c32,"RNBQKRNBQK")
string s = utf32_to_utf8(s32)

test("♕♘♖♗♗♘♔♖")
test("♖♘♗♕♔♗♘♖")
```
Output:

Note that output on a windows terminal is as expected far from pretty, this is from pwa/p2js

```QNRBBNKR : 105
RNBQKBNR : 518
♕♘♖♗♗♘♔♖ : 105
♖♘♗♕♔♗♘♖ : 518
```

## Raku

`#!/usr/bin/env raku# derive a chess960 position's SP-IDunit sub MAIN(\$array = "♖♘♗♕♔♗♘♖"); # standardize on letters for easier processingmy \$ascii = \$array.trans("♜♞♝♛♚♖♘♗♕♔" => "RNBQKRNBQK"); # (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.";}# (end optional error-checking) # 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 @knights = \$ascii  .subst(/<[QB]>/,'',:g)  .indices('N'); my \$knight = combinations(5,2).kv.grep(    -> \$i,@c { @c eq @knights }  );  # 2. Then which queen position:my \$queen = \$ascii  .subst(/<[B]>/,'',:g)  .index('Q'); # 3. Finally the two bishops:my @bishops = \$ascii.indices('B');my \$dark  = @bishops.grep({     \$_ %% 2 }) div 2;my \$light = @bishops.grep({ not \$_ %% 2 }) div 2; my \$sp-id = 4*(4*(6*\$knight + \$queen)+\$dark)+\$light; # standardize outputmy \$display = \$ascii.trans("RNBQK" => "♖♘♗♕♔"); say "\$display: \$sp-id";`
Output:
```\$ c960spid QNRBBNKR
♕♘♖♗♗♘♔♖: 105
\$ c960spid
♖♘♗♕♔♗♘♖: 518```

## Ruby

`def chess960_to_spid(pos)  start_str = pos.tr("♖♘♗♕♔", "RNBQK")  #1 knights score  s = start_str.delete("QB")  n = [0,1,2,3,4].combination(2).to_a.index( [s.index("N"), s.rindex("N")] )  #2 queen score  q = start_str.delete("N").index("Q")  #3 bishops  bs = start_str.index("B"), start_str.rindex("B")  d = bs.detect(&:even?).div(2)  l = bs.detect(&:odd? ).div(2)   96*n + 16*q + 4*d + lend positions = ["QNRBBNKR", "♖♘♗♕♔♗♘♖"]positions.each{|pos| puts "#{pos}:  #{chess960_to_spid(pos)}" } `
Output:
```QNRBBNKR:  105
♖♘♗♕♔♗♘♖:  518
```

## Wren

Library: Wren-trait
`import "/trait" for Indexed var glyphs  = "♜♞♝♛♚♖♘♗♕♔".toListvar letters = "RNBQKRNBQK"var names = { "R": "rook", "N": "knight", "B": "bishop", "Q": "queen", "K": "king" }var g2lMap = {}for (se in Indexed.new(glyphs)) g2lMap[glyphs[se.index]] = letters[se.index] var g2l = Fn.new { |pieces| pieces.reduce("") { |acc, p| acc + g2lMap[p] } } var ntable = { "01":0, "02":1, "03":2, "04":3, "12":4, "13":5, "14":6, "23":7, "24":8, "34":9 } var spid = Fn.new { |pieces|    pieces = g2l.call(pieces) // convert glyphs to letters     /* check for errors */    if (pieces.count != 8) Fiber.abort("There must be exactly 8 pieces.")    for (one in "KQ") {        if (pieces.count { |p| p == one } != 1 ) Fiber.abort("There must be one %(names[one]).")    }    for (two in "RNB") {        if (pieces.count { |p| p == two } != 2 ) Fiber.abort("There must be two %(names[two])s.")    }    var r1 = pieces.indexOf("R")    var r2 = pieces.indexOf("R", r1 + 1)    var k  = pieces.indexOf("K")    if (k < r1 || k > r2) Fiber.abort("The king must be between the rooks.")    var b1 = pieces.indexOf("B")    var b2 = pieces.indexOf("B", b1 + 1)    if ((b2 - b1) % 2 == 0) Fiber.abort("The bishops must be on opposite color squares.")     /* compute SP_ID */    var piecesN = pieces.replace("Q", "").replace("B", "")    var n1 = piecesN.indexOf("N")    var n2 = piecesN.indexOf("N", n1 + 1)    var np = "%(n1)%(n2)"    var N = ntable[np]     var piecesQ = pieces.replace("N", "")    var Q = piecesQ.indexOf("Q")     var D = "0246".indexOf(b1.toString)    var L = "1357".indexOf(b2.toString)    if (D == -1) {        D = "0246".indexOf(b2.toString)        L = "1357".indexOf(b1.toString)    }     return 96*N + 16*Q + 4*D + L} for (pieces in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖"]) {    System.print("%(pieces) or %(g2l.call(pieces)) has SP-ID of %(spid.call(pieces))")}`
Output:
```♕♘♖♗♗♘♔♖ or QNRBBNKR has SP-ID of 105
♖♘♗♕♔♗♘♖ or RNBQKBNR has SP-ID of 518
```