- let pieces = { w: new Array(8), b: new Array(8) };
- let flags = "";
- // Shuffle pieces on first (and last rank if randomness == 2)
- for (let c of ["w", "b"]) {
- if (c == 'b' && randomness == 1) {
- const lancerIdx = pieces['w'].findIndex(p => {
- return Object.keys(V.LANCER_DIRS).includes(p);
- });
- pieces['b'] =
- pieces['w'].slice(0, lancerIdx)
- .concat(['g'])
- .concat(pieces['w'].slice(lancerIdx + 1));
- flags += flags;
- 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.
- // NOTE: test % 2 == 1 because there are 7 slashes.
- if ((pieces['w'].indexOf('s') - sentryPos) % 2 == 1)
- [bishopPos, sentryPos] = [sentryPos, bishopPos];
+ const baseFen = ChessRules.GenRandInitFen(options);
+ const fenParts = baseFen.split(' ');
+ const posParts = fenParts[0].split('/');
+
+ // Replace one bishop by sentry, so that sentries on different colors
+ // Also replace one random rook by jailer,
+ // and one random knight by lancer (facing north/south)
+ let pieceLine = { b: posParts[0], w: posParts[7].toLowerCase() };
+ let posBlack = { r: -1, n: -1, b: -1 };
+ const mapP = { r: 'j', n: 'l', b: 's' };
+ ['w', 'b'].forEach(c => {
+ ['r', 'n', 'b'].forEach(p => {
+ let pl = pieceLine[c];
+ let pos = -1;
+ if (options.randomness == 2 || c == 'b')
+ pos = (randInt(2) == 0 ? pl.indexOf(p) : pl.lastIndexOf(p));
+ else pos = posBlack[p];
+ pieceLine[c] =
+ pieceLine[c].substr(0, pos) + mapP[p] + pieceLine[c].substr(pos+1);
+ if (options.randomness == 1 && c == 'b') posBlack[p] = pos;
+ });
+ });
+ // Rename 'l' into 'g' (black) or 'c' (white)
+ pieceLine['w'] = pieceLine['w'].replace('l', 'c');
+ pieceLine['b'] = pieceLine['b'].replace('l', 'g');
+ if (options.randomness == 2) {
+ const ws = pieceLine['w'].indexOf('s');
+ const bs = pieceLine['b'].indexOf('s');
+ if (ws % 2 != bs % 2) {
+ // Fix sentry: should be on different colors.
+ // => move sentry on other bishop for random color
+ const c = sample(['w', 'b'], 1);
+ pieceLine[c] = pieceLine[c]
+ .replace('b', 't') //tmp
+ .replace('s', 'b')
+ .replace('t', 's');