Generate Chess960 starting position: Difference between revisions

Content added Content deleted
Line 1,142: Line 1,142:


=={{header|J}}==
=={{header|J}}==
Build a table of the starting positions then pick one at random. There are 40320 distinct permutations of 8 items and 5040 distinct permutations of these chess pieces and (as the task name points out) only 960 permutations which also satisfy the constraints on bishop and rook position, so little memory is needed to generate the table. Also, since the table is built at "compile time", execution is fast (though "compilation" is reasonably fast also).
The simplest J approach for this task is to initially build a table of the starting positions. Then, for the task, we pick one position from the table at random. There are 40320 distinct permutations of 8 items and 5040 distinct permutations of these chess pieces and (as the task name points out) only 960 permutations which also satisfy the constraints on bishop and rook position, so little memory is needed to generate the table. Also, since the table is built at "compile time", execution is fast (though "compilation" is reasonably fast also).


<syntaxhighlight lang="j">row0=: u: 9812+2}.5|i.10
<syntaxhighlight lang="j">row0=: u: 9812+2}.5|i.10