Random starting position in chess

Random starting position in chess

Click here for a random case from cases or click here to suffling the cards and select
8 cards (marked with symbols: ) for the pieces on empty squares. Blacks eventually turn into whites.

             
     Selection:

If the bishops are on squares of different colors (4/7) we go to the last step, otherwise (3/7) we go to the next step:
if the white knight is (in square Nw) before/after the black knight (in square Nb),
then the black bishop (in square Bb) is swapped with the piece which is respectively in the
next/second next square of a different color (if it does not exist then the 8 squares are considered in a circle)
[new square Bb] = mod(Bb+1+2*(Nw>Nb);8).

Last step. If the king must be between the two rooks, then he is swapped with the nearest rook.

a

b

c

d

e

f

g

h

Click here for all cases in pgn text.