+ if (randomness == 0)
+ // Deterministic:
+ return "jsfqkbnr/pppppppp/8/8/8/8/PPPPPPPP/JSDQKBNR w 0 1111 - -";
+
+ let pieces = { w: new Array(8), b: new Array(8) };
+ // Shuffle pieces on first (and last rank if randomness == 2)
+ for (let c of ["w", "b"]) {
+ if (c == 'b' && randomness == 1) {
+ pieces['b'] = pieces['w'];
+ break;
+ }
+
+ let positions = ArrayFun.range(8);
+
+ // Get random squares for bishop and sentry
+ let randIndex = 2 * randInt(4);
+ let bishopPos = positions[randIndex];
+ // The sentry must be on a square of different color
+ let randIndex_tmp = 2 * randInt(4) + 1;
+ let sentryPos = positions[randIndex_tmp];
+ if (c == 'b') {
+ // Check if white sentry is on the same color as ours.
+ // If yes: swap bishop and sentry positions.
+ if ((pieces['w'].indexOf('s') - sentryPos) % 2 == 0)
+ [bishopPos, sentryPos] = [sentryPos, bishopPos];
+ }
+ positions.splice(Math.max(randIndex, randIndex_tmp), 1);
+ positions.splice(Math.min(randIndex, randIndex_tmp), 1);
+
+ // Get random squares for knight and lancer
+ randIndex = randInt(6);
+ const knightPos = positions[randIndex];
+ positions.splice(randIndex, 1);
+ randIndex = randInt(5);
+ const lancerPos = positions[randIndex];
+ positions.splice(randIndex, 1);
+
+ // Get random square for queen
+ randIndex = randInt(4);
+ const queenPos = positions[randIndex];
+ positions.splice(randIndex, 1);
+
+ // Rook, jailer and king positions are now almost fixed,
+ // only the ordering rook-> jailer or jailer->rook must be decided.
+ let rookPos = positions[0];
+ let jailerPos = positions[2];
+ const kingPos = positions[1];
+ if (Math.random() < 0.5) [rookPos, jailerPos] = [jailerPos, rookPos];
+
+ pieces[c][rookPos] = "r";
+ pieces[c][knightPos] = "n";
+ pieces[c][bishopPos] = "b";
+ pieces[c][queenPos] = "q";
+ pieces[c][kingPos] = "k";
+ pieces[c][sentryPos] = "s";
+ // Lancer faces north for white, and south for black:
+ pieces[c][lancerPos] = c == 'w' ? 'c' : 'g';
+ pieces[c][jailerPos] = "j";
+ }
+ return (
+ pieces["b"].join("") +
+ "/pppppppp/8/8/8/8/PPPPPPPP/" +
+ pieces["w"].join("").toUpperCase() +
+ " w 0 1111 - -"
+ );