1 import {Random
} from "/utils/alea.js";
3 export class FenUtil
= {
5 // arg o (constraints): "between" with p1 and p2.
6 // "flags", "diffCol": array of pieceType
8 let res
= JSON
.parse(JSON
.stringify(arr
));
10 res
= Random
.shuffle(arr
);
13 res
.forEach((p
, i
) => {
14 if (o
.flags
.includes(p
))
19 o
.diffCol
.forEach(p
=> {
20 // Pieces of type p on different colors:
21 const firstP
= res
.indexOf(p
),
22 lastP
= res
.lastIndexOf(p
);
23 if ((firstP
- lastP
) % 2 != 0) {
24 const choice1
= Random
.randBool() ? firstP : lastP
;
27 choice2
= Random
.randInt(arr
.length
);
31 o
.diffCol
.includes(choice2
) ||
32 (choice2
- choice1
) % 2 != 0
34 res
[choice1
] = res
[choice2
];
40 // Locate p1. If appearing first, exchange with first p2.
41 // If appearing last, exchange with last p2.
42 const p1
= res
.indexOf(o
.between
["p1"]);
43 const firstP2
= res
.indexOf(o
.between
["p2"]),
44 lastP2
= res
.lastIndexOf(o
.between
["p2"]);
45 if (p1
< firstP2
|| p1
> lastP2
) {
46 res
[p1
] = o
.between
["p2"];
48 res
[firstP2
] = o
.between
["p1"];
50 res
[lastP2
] = o
.between
["p1"];
53 return {fen: res
, flags: flags
};
57 const row1
= FenUtil
.setupRow(arr
, o
);
58 const row2
= o
.randomness
== 2 ? FenUtil
.setupRow(arr
, o
) : row1
;
62 flags: row1
.flags
+ row2
.flags