X-Git-Url: https://git.auder.net/?p=xogo.git;a=blobdiff_plain;f=utils%2FsetupPieces.js;fp=utils%2FsetupPieces.js;h=2c8cceee81f5edd4ea8e6fc9c0d2ec7df24861f3;hp=11af74afd360a7b01af0ec41b169147ce9fe4975;hb=10c9010b3b04915e132b6b3820f2f19c9ea6dcf0;hpb=0cef7bb4f72046ce60db408bfb76103428e2c15a diff --git a/utils/setupPieces.js b/utils/setupPieces.js index 11af74a..2c8ccee 100644 --- a/utils/setupPieces.js +++ b/utils/setupPieces.js @@ -15,39 +15,41 @@ export const FenUtil = { flags += i; }); } - if (o.diffCol) { - o.diffCol.forEach(p => { - // Pieces of type p on different colors: - const firstP = res.indexOf(p), - lastP = res.lastIndexOf(p); - if ((firstP - lastP) % 2 != 0) { - const choice1 = Random.randBool() ? firstP : lastP; - let choice2; - do { - choice2 = Random.randInt(arr.length); + if (o.randomness >= 1) { + if (o.diffCol) { + o.diffCol.forEach(p => { + // Pieces of type p on different colors: + const firstP = res.indexOf(p), + lastP = res.lastIndexOf(p); + if ((firstP - lastP) % 2 != 0) { + const choice1 = Random.randBool() ? firstP : lastP; + let choice2; + do { + choice2 = Random.randInt(arr.length); + } + while ( + choice2 == choice1 || + o.diffCol.includes(choice2) || + (choice2 - choice1) % 2 != 0 + ); + res[choice1] = res[choice2]; + res[choice2] = p; } - while ( - choice2 == choice1 || - o.diffCol.includes(choice2) || - (choice2 - choice1) % 2 != 0 - ); - res[choice1] = res[choice2]; - res[choice2] = p; + }); + } + if (o.between) { + // Locate p1. If appearing first, exchange with first p2. + // If appearing last, exchange with last p2. + const p1 = res.indexOf(o.between["p1"]); + const firstP2 = res.indexOf(o.between["p2"]), + lastP2 = res.lastIndexOf(o.between["p2"]); + if (p1 < firstP2 || p1 > lastP2) { + res[p1] = o.between["p2"]; + if (p1 < firstP2) + res[firstP2] = o.between["p1"]; + else //p1 > lastP2 + res[lastP2] = o.between["p1"]; } - }); - } - if (o.between) { - // Locate p1. If appearing first, exchange with first p2. - // If appearing last, exchange with last p2. - const p1 = res.indexOf(o.between["p1"]); - const firstP2 = res.indexOf(o.between["p2"]), - lastP2 = res.lastIndexOf(o.between["p2"]); - if (p1 < firstP2 || p1 > lastP2) { - res[p1] = o.between["p2"]; - if (p1 < firstP2) - res[firstP2] = o.between["p1"]; - else //p1 > lastP2 - res[lastP2] = o.between["p1"]; } } return {fen: res, flags: flags};