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