Find Chess960 starting position identifier: Difference between revisions

Added 11l
m (syntax highlighting fixup automation)
(Added 11l)
Line 23:
RQNBBKRN = 601
RNQBBKRN = 617
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F validate_position(String candidate)
assert(candidate.len == 8, ‘candidate position has invalid len = ’candidate.len)
 
V valid_pieces = [‘R’ = 2, ‘N’ = 2, ‘B’ = 2, ‘Q’ = 1, ‘K’ = 1]
assert(Set(Array(candidate)) == Set(valid_pieces.keys()), ‘candidate position contains invalid pieces’)
L(piece_type) valid_pieces.keys()
assert(candidate.count(piece_type) == valid_pieces[piece_type], ‘piece type '’piece_type‘' has invalid count’)
 
V bishops_pos = enumerate(Array(candidate)).filter((index, value) -> value == ‘B’).map((index, value) -> index)
assert(bishops_pos[0] % 2 != bishops_pos[1] % 2, ‘candidate position has both bishops in the same color’)
 
assert(candidate.filter(piece -> piece C ‘RK’) == [‘R’, ‘K’, ‘R’], ‘candidate position has K outside of RR’)
 
F calc_position(String start_pos)
validate_position(start_pos)
V subset_step1 = start_pos.filter(piece -> piece !C ‘QB’)
V nights_positions = enumerate(subset_step1).filter((index, value) -> value == ‘N’).map((index, value) -> index)
V nights_table = [(0, 1) = 0,
(0, 2) = 1,
(0, 3) = 2,
(0, 4) = 3,
(1, 2) = 4,
(1, 3) = 5,
(1, 4) = 6,
(2, 3) = 7,
(2, 4) = 8,
(3, 4) = 9]
V n = nights_table[(nights_positions[0], nights_positions[1])]
 
V subset_step2 = start_pos.filter(piece -> piece != ‘B’)
V q = subset_step2.index(‘Q’)
 
V dark_squares = enumerate(Array(start_pos)).filter((index, piece) -> index C Array((0.<9).step(2))).map((index, piece) -> piece)
V light_squares = enumerate(Array(start_pos)).filter((index, piece) -> index C Array((1.<9).step(2))).map((index, piece) -> piece)
V d = dark_squares.index(‘B’)
V l = light_squares.index(‘B’)
 
R 4 * (4 * (6*n + q) + d) + l
 
L(example) [‘QNRBBNKR’, ‘RNBQKBNR’, ‘RQNBBKRN’, ‘RNQBBKRN’]
print(‘Position: ’example‘; Chess960 PID= ’calc_position(example))
</syntaxhighlight>
 
{{out}}
<pre>
Position: QNRBBNKR; Chess960 PID= 105
Position: RNBQKBNR; Chess960 PID= 518
Position: RQNBBKRN; Chess960 PID= 601
Position: RNQBBKRN; Chess960 PID= 617
</pre>
 
=={{header|BASIC}}==
1,480

edits