Find Chess960 starting position identifier: Difference between revisions

m
syntax highlighting fixup automation
(Added the last two missing positions in last "for" loop.)
m (syntax highlighting fixup automation)
Line 29:
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.
 
<langsyntaxhighlight lang="basic">100 REM DERIVE SP-ID FROM CHESS960 POS
110 READ A$: IF A$="" THEN END
120 PRINT A$":";
Line 84:
630 NEXT I
640 SP = 96*N+16*Q+4*D+L
650 RETURN</langsyntaxhighlight>
 
{{Out}}
Line 97:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Sub SP_ID(PosicPiezas As String)
Dim As String pieza
Dim As Integer pQ(), pK(), pB(), pN(), pR(), i, j
Line 164:
Print
SP_ID("RNQBBKRN")
Sleep</langsyntaxhighlight>
{{out}}
<pre>QNRBBNKR has SP_ID of 105
Line 178:
{{works with|QBasic|1.1}}
{{trans|Commodore BASIC}}
<langsyntaxhighlight lang="qbasic">Cls
Print "Enter start array as seen by white."
120 Print
Line 238:
End
 
</syntaxhighlight>
</lang>
{{out}}
<pre>Enter start array as seen by White.
Line 260:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">; make sure string is a valid Chess960 starting array
(defun valid-array-p (start-array)
(and (string-equal (sort (copy-seq start-array) #'string-lessp) "BBKNNQRR") ; right pieces
Line 291:
(loop for ary in '("RNBQKBNR" "QNRBBNKR" "RQNBBKRN" "RNQBBKRN") doing
(format t "~a: ~a~%" ary (sp-id ary)))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 301:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs assocs.extras combinators formatting kernel
literals math math.combinatorics sequences sequences.extras sets
strings ;
Line 366:
"RNBQKBNR" sp-id.
"RQNBBKRN" sp-id.
"RNQBBKRN" sp-id.</langsyntaxhighlight>
{{out}}
<pre>
Line 377:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 468:
fmt.Printf("%s or %s has SP-ID of %d\n", pieces, g2l(pieces), spid(pieces))
}
}</langsyntaxhighlight>
 
{{out}}
Line 479:
 
=={{header|J}}==
Implementation:<langsyntaxhighlight Jlang="j">REF=: {{
'N Q B0 B1'=. 0 6 4 4 #: y
s=. 'B' (0 1+2*B0,B1)} 8#' '
Line 487:
}}"0 i.960
 
c960=: {{ r=. REF i. rplc&((u:9812+i.12);&>12$'KQRBNP') 7 u:deb y assert. r<#REF }}</langsyntaxhighlight>
 
Examples:
<langsyntaxhighlight Jlang="j"> c960'♕♘♖♗♗♘♔♖'
105
c960'♛♞♜♝♝♞♚♜'
Line 499:
601
c960'RNQBBKRN'
617</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const whitepieces = "♖♘♗♕♔♗♘♖♙"
const whitechars = "rnbqkp"
const blackpieces = "♜♞♝♛♚♝♞♜♟"
Line 542:
println(collect(position), " => ", chess960spid(position))
end
</langsyntaxhighlight>{{out}}
<pre>
['♕', '♘', '♖', '♗', '♗', '♘', '♔', '♖'] => 105
Line 552:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat, strutils, sugar, tables, unicode
 
type Piece {.pure.} = enum Rook = "R", Knight = "N", Bishop = "B", Queen = "Q", King = "K"
Line 611:
 
for glyphs in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖", "♖♕♘♗♗♔♖♘", "♖♘♕♗♗♔♖♘"]:
echo &"{glyphs} or {glyphs.toPieces().join()} has SP-ID of {glyphs.spid()}"</langsyntaxhighlight>
 
{{out}}
Line 621:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use v5.36;
use List::AllUtils 'indexes';
 
Line 641:
}
 
say "$_ " . sp_id($_) for <QNRBBNKR RNBQKBNR RQNBBKRN RNQBBKRN QNBRBNKR>;</langsyntaxhighlight>
{{out}}
<pre>QNRBBNKR 105
Line 650:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">spid</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 673:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RQNBBKRN"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RNQBBKRN"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 681:
RNQBBKRN : 617</pre>
To support all those crazy unicode characters just change the start of spid() to:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">spid</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">u32</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf8_to_utf32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">),</span>
Line 693:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"♜♛♞♝♝♚♜♞"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"♜♞♛♝♝♚♜♞"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
Note that output on a windows terminal is as expected far from pretty, this is from pwa/p2js
Line 710:
{{works with|Python|3.10.5 2022-06-28}}
 
<langsyntaxhighlight lang="python"># optional, but task function depends on it as written
def validate_position(candidate: str):
assert (
Line 780:
if __name__ == '__main__':
for example in ["QNRBBNKR", "RNBQKBNR", "RQNBBKRN", "RNQBBKRN"]:
print(f'Position: {example}; Chess960 PID= {calc_position(example)}')</langsyntaxhighlight>
{{out}}
<pre>
Line 790:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub c960-spid($array) {
# standardize on letters for easier processing
my $ascii = $array.trans('♜♞♝♛♚♖♘♗♕♔' => 'RNBQK');
Line 820:
}
 
say .&c960-spid for <♖♘♗♕♔♗♘♖ ♛♞♜♝♝♞♚♜ RQNBBKRN RNQBBKRN QNBRBNKR>;</langsyntaxhighlight>
{{out}}
<pre>♖♘♗♕♔♗♘♖ 518
Line 829:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">CHESS_PIECES = %w<♖♘♗♕♔ ♜♞♝♛♚>
def chess960_to_spid(pos)
start_str = pos.tr(CHESS_PIECES.join, "RNBQKRNBQK")
Line 849:
puts "#{pieces} (#{array}): #{chess960_to_spid array}"
end
</syntaxhighlight>
</lang>
{{out}}
<pre>♕♘♖♗♗♘♔♖ (QNRBBNKR): 105
Line 859:
=={{header|Wren}}==
{{libheader|Wren-trait}}
<langsyntaxhighlight lang="ecmascript">import "/trait" for Indexed
 
var glyphs = "♜♞♝♛♚♖♘♗♕♔".toList
Line 912:
for (pieces in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖", "♜♛♞♝♝♚♜♞", "♜♞♛♝♝♚♜♞"]) {
System.print("%(pieces) or %(g2l.call(pieces)) has SP-ID of %(spid.call(pieces))")
}</langsyntaxhighlight>
 
{{out}}
10,327

edits