Find Chess960 starting position identifier: Difference between revisions
Content added Content deleted
(→{{header|Raku}}: Add troublesome examples.) |
(→{{header|Ruby}}: Fix algorithm, add Unicode piece output and troublesome examples.) |
||
Line 806: | Line 806: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>CHESS_PIECES = %w<♖♘♗♕♔ ♜♞♝♛♚> |
|||
def chess960_to_spid(pos) |
|||
start_str = pos.tr( |
start_str = pos.tr(CHESS_PIECES.join, "RNBQKRNBQK") |
||
#1 knights score |
#1 knights score |
||
s = start_str.delete("QB") |
s = start_str.delete("QB") |
||
n = [0,1,2,3,4].combination(2).to_a.index( [s.index("N"), s.rindex("N")] ) |
n = [0,1,2,3,4].combination(2).to_a.index( [s.index("N"), s.rindex("N")] ) |
||
#2 queen score |
#2 queen score |
||
q = start_str.delete(" |
q = start_str.delete("B").index("Q") |
||
#3 bishops |
#3 bishops |
||
bs = start_str.index("B"), start_str.rindex("B") |
bs = start_str.index("B"), start_str.rindex("B") |
||
Line 821: | Line 822: | ||
end |
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> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre>QNRBBNKR: 105 |
<pre>♕♘♖♗♗♘♔♖ (QNRBBNKR): 105 |
||
♜♞♝♛♚♝♞♜ (RNBQKBNR): 518 |
|||
♖♘♗♕♔♗♘♖: 518 |
|||
♖♕♘♗♗♔♖♘ (RQNBBKRN): 601 |
|||
♜♞♛♝♝♚♜♞ (RNQBBKRN): 617 |
|||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-trait}} |
{{libheader|Wren-trait}} |