-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructBorder.ts
53 lines (47 loc) · 1.25 KB
/
constructBorder.ts
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
import { MAX_COLS, MAX_ROWS, SLEEP_TIME, WALL_TILE_STYLE } from "./constants";
import { isEqual, sleep } from "./helpers";
import { GridType, TileType } from "./types";
export async function constructBorder(
grid: GridType,
startTile: TileType,
endTile: TileType
) {
const shape = [
{ row: 0, col: 1 },
{ row: 1, col: 0 },
{ row: 0, col: -1 },
{ row: -1, col: 0 },
];
let row = 0;
let col = 0;
for (let i = 0; i < 4; i++) {
const direction = shape[i];
while (
row + direction.row >= 0 &&
row + direction.row < MAX_ROWS &&
col + direction.col >= 0 &&
col + direction.col < MAX_COLS
) {
row += direction.row;
col += direction.col;
if (
!isEqual(grid[row][col], startTile) &&
!isEqual(grid[row][col], endTile)
) {
grid[row][col].isWall = true;
const tileElement = document.getElementById(`${row}-${col}`);
if (tileElement) {
tileElement.classList.add(
...WALL_TILE_STYLE.split(" "),
"animate-wall"
);
}
await sleep(SLEEP_TIME);
}
}
if (row < 0) row = 0;
if (row >= MAX_ROWS) row = MAX_ROWS - 1;
if (col < 0) col = 0;
if (col >= MAX_COLS) col = MAX_COLS - 1;
}
}