Find Chess960 starting position identifier: Difference between revisions
Find Chess960 starting position identifier (view source)
Revision as of 12:48, 27 August 2022
, 1 year agosyntax highlighting fixup automation
(Added the last two missing positions in last "for" loop.) |
Thundergnat (talk | contribs) 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.
<
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</
{{Out}}
Line 97:
==={{header|FreeBASIC}}===
<
Dim As String pieza
Dim As Integer pQ(), pK(), pB(), pN(), pR(), i, j
Line 164:
Print
SP_ID("RNQBBKRN")
Sleep</
{{out}}
<pre>QNRBBNKR has SP_ID of 105
Line 178:
{{works with|QBasic|1.1}}
{{trans|Commodore BASIC}}
<
Print "Enter start array as seen by white."
120 Print
Line 238:
End
</syntaxhighlight>
{{out}}
<pre>Enter start array as seen by White.
Line 260:
=={{header|Common Lisp}}==
<
(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>
{{Out}}
Line 301:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
literals math math.combinatorics sequences sequences.extras sets
strings ;
Line 366:
"RNBQKBNR" sp-id.
"RQNBBKRN" sp-id.
"RNQBBKRN" sp-id.</
{{out}}
<pre>
Line 377:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 468:
fmt.Printf("%s or %s has SP-ID of %d\n", pieces, g2l(pieces), spid(pieces))
}
}</
{{out}}
Line 479:
=={{header|J}}==
Implementation:<
'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 }}</
Examples:
<
105
c960'♛♞♜♝♝♞♚♜'
Line 499:
601
c960'RNQBBKRN'
617</
=={{header|Julia}}==
<
const whitechars = "rnbqkp"
const blackpieces = "♜♞♝♛♚♝♞♜♟"
Line 542:
println(collect(position), " => ", chess960spid(position))
end
</
<pre>
['♕', '♘', '♖', '♗', '♗', '♘', '♔', '♖'] => 105
Line 552:
=={{header|Nim}}==
{{trans|Wren}}
<
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()}"</
{{out}}
Line 621:
=={{header|Perl}}==
{{trans|Raku}}
<
use List::AllUtils 'indexes';
Line 641:
}
say "$_ " . sp_id($_) for <QNRBBNKR RNBQKBNR RQNBBKRN RNQBBKRN QNBRBNKR>;</
{{out}}
<pre>QNRBBNKR 105
Line 650:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 681:
RNQBBKRN : 617</pre>
To support all those crazy unicode characters just change the start of spid() to:
<!--<
<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>
<!--</
{{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}}
<
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)}')</
{{out}}
<pre>
Line 790:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
# standardize on letters for easier processing
my $ascii = $array.trans('♜♞♝♛♚♖♘♗♕♔' => 'RNBQK');
Line 820:
}
say .&c960-spid for <♖♘♗♕♔♗♘♖ ♛♞♜♝♝♞♚♜ RQNBBKRN RNQBBKRN QNBRBNKR>;</
{{out}}
<pre>♖♘♗♕♔♗♘♖ 518
Line 829:
=={{header|Ruby}}==
<
def chess960_to_spid(pos)
start_str = pos.tr(CHESS_PIECES.join, "RNBQKRNBQK")
Line 849:
puts "#{pieces} (#{array}): #{chess960_to_spid array}"
end
</syntaxhighlight>
{{out}}
<pre>♕♘♖♗♗♘♔♖ (QNRBBNKR): 105
Line 859:
=={{header|Wren}}==
{{libheader|Wren-trait}}
<
var glyphs = "♜♞♝♛♚♖♘♗♕♔".toList
Line 912:
for (pieces in ["♕♘♖♗♗♘♔♖", "♖♘♗♕♔♗♘♖", "♜♛♞♝♝♚♜♞", "♜♞♛♝♝♚♜♞"]) {
System.print("%(pieces) or %(g2l.call(pieces)) has SP-ID of %(spid.call(pieces))")
}</
{{out}}
|