-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
45 lines (39 loc) · 1.12 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const range = (start, end) => {
let res = [];
for (let i = start; i < end; i++) {
res.push(i);
}
return res;
};
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
const shuffle = (sequence) => {
const swap = (i, j) => {
const ith_item = sequence[i];
const jth_item = sequence[j];
sequence[i] = jth_item;
sequence[j] = ith_item;
};
for (let i = sequence.length - 1; i > 0; i--) {
const roll = Math.floor(Math.random() * i);
swap(roll, i);
}
return sequence;
};
const generateBoardItems = (gridSize, theme) => {
gridSize = gridSize[0] * gridSize[1];
const totalPairs = gridSize / 2;
const rangeSize = theme === "icons" ? 9 : 99;
const source = range(0, rangeSize);
let withReplacement = totalPairs > source.length;
const items = [];
for (let i = 0; i < totalPairs; i++) {
let randomIndex = Math.floor(Math.random() * source.length);
let randomItem = source[randomIndex];
items.push(randomItem, randomItem);
if (!withReplacement) {
source.splice(randomIndex, 1);
}
}
return shuffle(items);
};
export { generateBoardItems };