Skip to content

Commit e36b6af

Browse files
committed
Maze update cells with throw error
1 parent b7d7f41 commit e36b6af

File tree

10 files changed

+126
-144
lines changed

10 files changed

+126
-144
lines changed

src/apps/path-finder/algorithms/maze-generator/binary.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ export async function generateBinaryMaze({
2727
cols,
2828
entry,
2929
exit,
30+
updateGrid,
31+
updateCells,
3032
}: MazeAlgoProps) {
3133
const grid = generateGrid(rows, cols, CellType.wall);
34+
updateGrid(grid);
3235

3336
for (let row = 0; row < rows; row += 2) {
3437
for (let col = 0; col < cols; col += 2) {
35-
grid[row][col] = CellType.clear;
36-
37-
const neighbors = getNeighbors(grid, { row, col });
38+
const cell = { row, col };
39+
const neighbors = getNeighbors(grid, cell);
3840
const neighbor = neighbors[Math.floor(Math.random() * neighbors.length)];
3941

40-
if (neighbor) {
41-
grid[neighbor.row][neighbor.col] = CellType.clear;
42-
}
42+
const cellsToUpdate = neighbor ? [cell, neighbor] : cell;
43+
await updateCells(grid, cellsToUpdate);
4344
}
4445
}
4546

46-
grid[entry.row][entry.col] = CellType.entry;
47-
grid[exit.row][exit.col] = CellType.exit;
48-
47+
updateCells(grid, entry, CellType.entry);
48+
updateCells(grid, exit, CellType.exit);
4949
return grid;
5050
}

src/apps/path-finder/algorithms/maze-generator/kruskal.ts

-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export async function generateKruskalMaze({
6060
exit,
6161
updateGrid,
6262
updateCells,
63-
isGenerating,
6463
}: MazeAlgoProps) {
6564
const grid = generateGrid(rows, cols, CellType.wall);
6665
updateGrid(grid);
@@ -84,10 +83,6 @@ export async function generateKruskalMaze({
8483
} else {
8584
await updateCells(grid, randomEdge, CellType.wall);
8685
}
87-
88-
if (!isGenerating()) {
89-
return null;
90-
}
9186
}
9287

9388
updateCells(grid, entry, CellType.entry);

src/apps/path-finder/algorithms/maze-generator/prims.ts

-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export async function generatePrimsMaze({
5757
exit,
5858
updateGrid,
5959
updateCells,
60-
isGenerating,
6160
}: MazeAlgoProps) {
6261
const grid = generateGrid(rows, cols, CellType.wall);
6362
updateGrid(grid);
@@ -72,10 +71,6 @@ export async function generatePrimsMaze({
7271
const neighbor = neighbors[randomIndex];
7372
neighbors.splice(randomIndex, 1);
7473

75-
if (!isGenerating()) {
76-
return null;
77-
}
78-
7974
if (grid[neighbor.row][neighbor.col] !== CellType.clear) {
8075
const middleCell = createPassage(grid, neighbor);
8176
await updateCells(grid, [middleCell, neighbor]);

src/apps/path-finder/algorithms/maze-generator/random.ts

-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export async function generateMazeRandomly({
88
exit,
99
updateGrid,
1010
updateCells,
11-
isGenerating,
1211
}: MazeAlgoProps) {
1312
const grid = generateGrid(rows, cols, CellType.clear);
1413
updateGrid(grid);
@@ -17,11 +16,6 @@ export async function generateMazeRandomly({
1716
for (let j = 0; j < cols; j++) {
1817
if (Math.random() < 0.25) {
1918
await updateCells(grid, { row: i, col: j }, CellType.wall);
20-
grid[i][j] = CellType.wall;
21-
22-
if (!isGenerating()) {
23-
return null;
24-
}
2519
}
2620
}
2721
}

src/apps/path-finder/algorithms/maze-generator/recursive-backtracking.ts

+35-35
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ const directions = [
88
{ row: 0, col: 2 },
99
];
1010

11-
export async function generateRecursiveBacktrackingMaze({
12-
rows,
13-
cols,
14-
entry,
15-
exit,
16-
}: MazeAlgoProps) {
17-
const grid = generateGrid(rows, cols, CellType.wall);
18-
19-
grid[0][0] = CellType.clear;
20-
recursiveBacktracking(grid, 0, 0, rows, cols);
21-
22-
grid[entry.row][entry.col] = CellType.entry;
23-
grid[exit.row][exit.col] = CellType.exit;
24-
25-
return grid;
26-
}
27-
2811
function getNonMazeNeighbors(grid: CellType[][], row: number, col: number) {
2912
const rows = grid.length;
3013
const cols = grid[0].length;
@@ -41,32 +24,49 @@ function getNonMazeNeighbors(grid: CellType[][], row: number, col: number) {
4124
.filter((cell) => grid[cell.row][cell.col] !== CellType.clear);
4225
}
4326

44-
function createPassage(grid: CellType[][], cell: Cell, neighbor: Cell) {
27+
async function createPassage(
28+
grid: CellType[][],
29+
updateCells: MazeAlgoProps['updateCells'],
30+
cell: Cell,
31+
neighbor: Cell
32+
) {
4533
const middleCell = {
4634
row: neighbor.row + (cell.row - neighbor.row) / 2,
4735
col: neighbor.col + (cell.col - neighbor.col) / 2,
4836
};
4937

50-
grid[neighbor.row][neighbor.col] = CellType.clear;
51-
grid[middleCell.row][middleCell.col] = CellType.clear;
38+
await updateCells(grid, [middleCell, neighbor], CellType.clear);
5239
}
5340

54-
function recursiveBacktracking(
55-
grid: CellType[][],
56-
row: number,
57-
col: number,
58-
rows: number,
59-
cols: number
60-
) {
61-
const neighbors = getNonMazeNeighbors(grid, row, col);
62-
while (neighbors.length) {
63-
const randomIndex = Math.floor(Math.random() * neighbors.length);
64-
const neighbor = neighbors[randomIndex];
65-
neighbors.splice(randomIndex, 1);
41+
export async function generateRecursiveBacktrackingMaze({
42+
rows,
43+
cols,
44+
entry,
45+
exit,
46+
updateGrid,
47+
updateCells,
48+
}: MazeAlgoProps) {
49+
const grid = generateGrid(rows, cols, CellType.wall);
50+
updateGrid(grid);
51+
updateCells(grid, { row: 0, col: 0 });
52+
53+
async function recursiveBacktracking(row: number, col: number) {
54+
const neighbors = getNonMazeNeighbors(grid, row, col);
55+
while (neighbors.length) {
56+
const randomIndex = Math.floor(Math.random() * neighbors.length);
57+
const neighbor = neighbors[randomIndex];
58+
neighbors.splice(randomIndex, 1);
6659

67-
if (grid[neighbor.row][neighbor.col] !== CellType.clear) {
68-
createPassage(grid, { row, col }, neighbor);
69-
recursiveBacktracking(grid, neighbor.row, neighbor.col, rows, cols);
60+
if (grid[neighbor.row][neighbor.col] !== CellType.clear) {
61+
await createPassage(grid, updateCells, { row, col }, neighbor);
62+
await recursiveBacktracking(neighbor.row, neighbor.col);
63+
}
7064
}
7165
}
66+
67+
await recursiveBacktracking(0, 0);
68+
69+
updateCells(grid, entry, CellType.entry);
70+
updateCells(grid, exit, CellType.exit);
71+
return grid;
7272
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { generateGrid } from '../../helpers/grid';
22
import { CellType, MazeAlgoProps } from '../../models/interfaces';
33

4+
interface DrawWallConfig {
5+
updateCells: MazeAlgoProps['updateCells'];
6+
divisionPoint: number;
7+
passagePoint: number;
8+
start: number;
9+
end: number;
10+
}
11+
412
function getRandomEvenNumber(min: number, max: number) {
513
return Math.floor((Math.random() * (max - min + 1)) / 2) * 2 + min;
614
}
@@ -9,101 +17,85 @@ function getRandomOddNumber(min: number, max: number) {
917
return Math.floor((Math.random() * (max - min)) / 2) * 2 + 1 + min;
1018
}
1119

12-
function drawHorizontalWall(
20+
async function drawHorizontalWall(
1321
grid: CellType[][],
14-
{
15-
divisionPoint,
16-
passagePoint,
17-
start,
18-
end,
19-
}: {
20-
divisionPoint: number;
21-
passagePoint: number;
22-
start: number;
23-
end: number;
24-
}
22+
{ updateCells, divisionPoint, passagePoint, start, end }: DrawWallConfig
2523
) {
2624
for (let pos = start; pos <= end; pos++) {
27-
grid[divisionPoint][pos] = CellType.wall;
25+
await updateCells(grid, [{ row: divisionPoint, col: pos }], CellType.wall);
2826
}
29-
grid[divisionPoint][passagePoint] = CellType.clear;
27+
await updateCells(grid, [{ row: divisionPoint, col: passagePoint }]);
3028
}
3129

32-
function drawVerticalWall(
30+
async function drawVerticalWall(
3331
grid: CellType[][],
34-
{
35-
divisionPoint,
36-
passagePoint,
37-
start,
38-
end,
39-
}: {
40-
divisionPoint: number;
41-
passagePoint: number;
42-
start: number;
43-
end: number;
44-
}
32+
{ updateCells, divisionPoint, passagePoint, start, end }: DrawWallConfig
4533
) {
4634
for (let pos = start; pos <= end; pos++) {
47-
grid[pos][divisionPoint] = CellType.wall;
35+
await updateCells(grid, [{ row: pos, col: divisionPoint }], CellType.wall);
4836
}
49-
grid[passagePoint][divisionPoint] = CellType.clear;
37+
await updateCells(grid, [{ row: passagePoint, col: divisionPoint }]);
5038
}
5139

5240
export async function generateRecursiveDivisionMaze({
5341
rows,
5442
cols,
5543
entry,
5644
exit,
45+
updateGrid,
46+
updateCells,
5747
}: MazeAlgoProps) {
5848
const grid = generateGrid(rows, cols, CellType.clear);
49+
updateGrid(grid);
5950

60-
recursiveDivision(grid, 0, rows - 1, 0, cols - 1);
61-
grid[entry.row][entry.col] = CellType.entry;
62-
grid[exit.row][exit.col] = CellType.exit;
51+
async function recursiveDivision(
52+
rowStart: number,
53+
rowEnd: number,
54+
colStart: number,
55+
colEnd: number
56+
) {
57+
if (rowEnd - rowStart < 2 || colEnd - colStart < 2) {
58+
return;
59+
}
6360

64-
return grid;
65-
}
61+
const width = colEnd - colStart;
62+
const height = rowEnd - rowStart;
63+
const isHorizontal = width < height;
6664

67-
function recursiveDivision(
68-
grid: CellType[][],
69-
rowStart: number,
70-
rowEnd: number,
71-
colStart: number,
72-
colEnd: number
73-
) {
74-
if (rowEnd - rowStart < 2 || colEnd - colStart < 2) {
75-
return;
76-
}
65+
if (isHorizontal) {
66+
const divisionPoint = getRandomOddNumber(rowStart, rowEnd);
67+
const passagePoint = getRandomEvenNumber(colStart, colEnd);
7768

78-
const width = colEnd - colStart;
79-
const height = rowEnd - rowStart;
80-
const isHorizontal = width < height;
69+
await drawHorizontalWall(grid, {
70+
updateCells,
71+
divisionPoint,
72+
passagePoint,
73+
start: colStart,
74+
end: colEnd,
75+
});
8176

82-
if (isHorizontal) {
83-
const divisionPoint = getRandomOddNumber(rowStart, rowEnd);
84-
const passagePoint = getRandomEvenNumber(colStart, colEnd);
77+
await recursiveDivision(rowStart, divisionPoint - 1, colStart, colEnd);
78+
await recursiveDivision(divisionPoint + 1, rowEnd, colStart, colEnd);
79+
} else {
80+
const divisionPoint = getRandomOddNumber(colStart, colEnd);
81+
const passagePoint = getRandomEvenNumber(rowStart, rowEnd);
8582

86-
drawHorizontalWall(grid, {
87-
divisionPoint,
88-
passagePoint,
89-
start: colStart,
90-
end: colEnd,
91-
});
83+
await drawVerticalWall(grid, {
84+
updateCells,
85+
divisionPoint,
86+
passagePoint,
87+
start: rowStart,
88+
end: rowEnd,
89+
});
9290

93-
recursiveDivision(grid, rowStart, divisionPoint - 1, colStart, colEnd);
94-
recursiveDivision(grid, divisionPoint + 1, rowEnd, colStart, colEnd);
95-
} else {
96-
const divisionPoint = getRandomOddNumber(colStart, colEnd);
97-
const passagePoint = getRandomEvenNumber(rowStart, rowEnd);
91+
await recursiveDivision(rowStart, rowEnd, colStart, divisionPoint - 1);
92+
await recursiveDivision(rowStart, rowEnd, divisionPoint + 1, colEnd);
93+
}
94+
}
9895

99-
drawVerticalWall(grid, {
100-
divisionPoint,
101-
passagePoint,
102-
start: rowStart,
103-
end: rowEnd,
104-
});
96+
await recursiveDivision(0, rows - 1, 0, cols - 1);
97+
updateCells(grid, entry, CellType.entry);
98+
updateCells(grid, exit, CellType.exit);
10599

106-
recursiveDivision(grid, rowStart, rowEnd, colStart, divisionPoint - 1);
107-
recursiveDivision(grid, rowStart, rowEnd, divisionPoint + 1, colEnd);
108-
}
100+
return grid;
109101
}

src/apps/path-finder/components/controller/execution.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { searchPath } from '../../store/search-thunk';
1212

1313
const speeds = new Map([
1414
['0.5x', 50],
15-
['0.75x', 40],
15+
['0.7x', 40],
1616
['1x', 30],
1717
['2x', 20],
1818
['4x', 1],

src/apps/path-finder/components/controller/operations.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ const speeds = new Map([
1212
['4x', 1],
1313
['2x', 10],
1414
['1x', 25],
15-
['0.75x', 50],
15+
['0.7x', 50],
1616
['0.5x', 100],
17+
['0.1x', 250],
1718
]);
1819

1920
function Operations() {

src/apps/path-finder/models/interfaces.ts

-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,4 @@ export interface MazeAlgoProps {
5353
cells: Cell | Cell[],
5454
cellType?: CellType
5555
) => Promise<void>;
56-
isGenerating: () => boolean;
5756
}

0 commit comments

Comments
 (0)