RosettaCodeData/Task/Generate-Chess960-starting-position/D/generate-chess960-starting-position-2.d
2025-06-11 20:16:52 -04:00

25 lines
694 B
D

import std.stdio : writeln;
import std.random;
import std.array : join;
import std.string : indexOf, lastIndexOf;
import std.algorithm : nextPermutation;
void main() {
// The Pieces, no King at all but 3 Rooks!
auto pos = ["Q", "R", "R", "R", "B", "B", "N", "N"].randomShuffle;
do {
auto candidate = pos.join("");
if (candidate.indexOf('B') % 2 != candidate.lastIndexOf('B') % 2) {
auto dupPos = pos.dup;
// Promote middle Rook to King
dupPos[candidate[0..candidate.lastIndexOf('R')].lastIndexOf('R')] = "K";
dupPos.join("").writeln; // Output!
return;
}
} while (pos.nextPermutation);
}