Find Chess960 starting position identifier: Difference between revisions

Content added Content deleted
(→‎{{header|Phix}}: Fix algorithm, add troublesome examples.)
(→‎{{header|Python}}: Fix algorithm; add demo code with troublesome examples.)
Line 656: Line 656:
{{works with|Python|3.10.5 2022-06-28}}
{{works with|Python|3.10.5 2022-06-28}}


<lang python># optional, but task function depends on it as written
<lang python>
# optional, but task function depends on it as written
def validate_position(candidate: str):
def validate_position(candidate: str):
assert (
assert (
Line 672: Line 671:
), f"piece type '{piece_type}' has invalid count"
), f"piece type '{piece_type}' has invalid count"


bishops_pos = [index for index, value in enumerate(candidate) if value == "B"]
bishops_pos = [index for index,
value in enumerate(candidate) if value == "B"]
assert (
assert (
bishops_pos[0] % 2 != bishops_pos[1] % 2
bishops_pos[0] % 2 != bishops_pos[1] % 2
Line 709: Line 709:


# step 2
# step 2
subset_step2 = [piece for piece in start_pos if piece != "N"]
subset_step2 = [piece for piece in start_pos if piece != "B"]
Q = subset_step2.index("Q")
Q = subset_step2.index("Q")


Line 723: Line 723:


return 4 * (4 * (6*N + Q) + D) + L
return 4 * (4 * (6*N + Q) + D) + L
</lang>


if __name__ == '__main__':
for example in ["QNRBBNKR", "RNBQKBNR", "RQNBBKRN", "RNQBBKRN"]:
print(f'Position: {example}; Chess960 PID= {calc_position(example)}')</lang>
{{out}}
{{out}}
<pre>
<pre>
'Position: RNBQKBNR; Chess960 PID= 518'
Position: QNRBBNKR; Chess960 PID= 105
'Position: QNRBBNKR; Chess960 PID= 105'
Position: RNBQKBNR; Chess960 PID= 518
Position: RQNBBKRN; Chess960 PID= 601
Position: RNQBBKRN; Chess960 PID= 617
</pre>
</pre>