Find Chess960 starting position identifier: Difference between revisions

→‎{{header|Ruby}}: Fix algorithm, add Unicode piece output and troublesome examples.
(→‎{{header|Raku}}: Add troublesome examples.)
(→‎{{header|Ruby}}: Fix algorithm, add Unicode piece output and troublesome examples.)
Line 806:
 
=={{header|Ruby}}==
<lang ruby>CHESS_PIECES = %w<♖♘♗♕♔ ♜♞♝♛♚>
<lang ruby>def chess960_to_spid(pos)
start_str = pos.tr("♖♘♗♕♔"CHESS_PIECES.join, "RNBQKRNBQKRNBQK")
#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("NB").index("Q")
#3 bishops
bs = start_str.index("B"), start_str.rindex("B")
Line 821 ⟶ 822:
end
 
%w<QNRBBNKR RNBQKBNR RQNBBKRN RNQBBKRN>.each_with_index do |array, i|
positions = ["QNRBBNKR", "♖♘♗♕♔♗♘♖"]
pieces = array.tr("RNBQK", CHESS_PIECES[i%2])
positions.each{|pos| puts "#{pos}: #{chess960_to_spid(pos)}" }
puts "#{pieces} (#{array}): #{chess960_to_spid array}"
end
</lang>
{{out}}
<pre>♕♘♖♗♗♘♔♖ (QNRBBNKR): 105
♜♞♝♛♚♝♞♜ (RNBQKBNR): 518
♖♘♗♕♔♗♘♖: 518
♖♕♘♗♗♔♖♘ (RQNBBKRN): 601
♜♞♛♝♝♚♜♞ (RNQBBKRN): 617
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-trait}}
1,479

edits