-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
73 lines (58 loc) · 1.51 KB
/
helper.js
File metadata and controls
73 lines (58 loc) · 1.51 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
export const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const baseBarClass = "bar rounded-md transition-all duration-300 text-center";
export const classLists = {
initial: `${baseBarClass} bg-blue-200`,
pointer1: `${baseBarClass} bg-red-500`,
pointer2: `${baseBarClass} bg-blue-500`,
middle: `${baseBarClass} bg-yellow-500`,
done: `${baseBarClass} bg-green-500`,
};
export const generateRandomArray = (num) => {
const arr = [];
for (let i = 0; i < num; i++) {
// Random number between 10 and 100 (inclusive)
const randomValue = Math.floor(Math.random() * (100 - 10 + 1)) + 10;
arr.push(randomValue);
}
return arr;
};
export const toggleControls = (btn1,btn2, state) => {
btn1.disabled = state;
btn2.disabled = state;
};
//path
const COLS = 20;
export const getRowCol = (index) => {
return {
row: Math.floor(index / COLS),
col: index % COLS
};
};
export const getIndex = (row, col) => {
return row * COLS + col;
};
export const getNeighbors = (index, totalCells) => {
const { row, col } = getRowCol(index);
const neighbors = [];
const directions = [
[0, 1], // right
[0, -1], // left
[1, 0], // down
[-1, 0] // up
];
for (let [dr, dc] of directions) {
const newRow = row + dr;
const newCol = col + dc;
if (
newRow >= 0 &&
newCol >= 0 &&
getIndex(newRow, newCol) < totalCells &&
newCol < COLS
) {
neighbors.push(getIndex(newRow, newCol));
}
}
return neighbors;
};