shuffle within button groups too
This commit is contained in:
parent
76f7d7c76f
commit
0f6345d66f
1 changed files with 17 additions and 18 deletions
|
@ -1,3 +1,18 @@
|
|||
function shuffle<A>(subject: A[]): A[] {
|
||||
let res = Array.from(subject);
|
||||
|
||||
for (let i = 0; i < res.length - 1; ++i) {
|
||||
const j = i + Math.floor(Math.random() * (res.length - i));
|
||||
if (i != j) {
|
||||
const k = res[i]!;
|
||||
res[i] = res[j]!;
|
||||
res[j] = k;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function group<A>(subject: A[], keepTogether: A[][]): A[][] {
|
||||
type Value = {array: A[], added: boolean};
|
||||
|
||||
|
@ -14,7 +29,7 @@ function group<A>(subject: A[], keepTogether: A[][]): A[][] {
|
|||
if (group?.added) { continue; }
|
||||
else if (group) {
|
||||
group.added = true;
|
||||
res.push(group.array);
|
||||
res.push(shuffle(group.array));
|
||||
} else {
|
||||
res.push([x]);
|
||||
}
|
||||
|
@ -23,24 +38,8 @@ function group<A>(subject: A[], keepTogether: A[][]): A[][] {
|
|||
return res;
|
||||
}
|
||||
|
||||
function shuffle<A>(subject: A[]): A[] {
|
||||
let res = Array.from(subject);
|
||||
|
||||
for (let i = 0; i < res.length - 2; ++i) {
|
||||
const j = i + Math.floor(Math.random() * (res.length - i));
|
||||
if (i != j) {
|
||||
const k = res[i]!;
|
||||
res[i] = res[j]!;
|
||||
res[j] = k;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
function groupedShuffle<A>(subject: A[], keepTogether: A[][]): A[] {
|
||||
let groups = group(subject, keepTogether);
|
||||
return shuffle(groups).flat();
|
||||
return shuffle(group(subject, keepTogether)).flat();
|
||||
}
|
||||
|
||||
function shuffleAll() {
|
||||
|
|
Loading…
Add table
Reference in a new issue