1
1
import { generateGrid } from '../../helpers/grid' ;
2
2
import { CellType , MazeAlgoProps } from '../../models/interfaces' ;
3
3
4
+ interface DrawWallConfig {
5
+ updateCells : MazeAlgoProps [ 'updateCells' ] ;
6
+ divisionPoint : number ;
7
+ passagePoint : number ;
8
+ start : number ;
9
+ end : number ;
10
+ }
11
+
4
12
function getRandomEvenNumber ( min : number , max : number ) {
5
13
return Math . floor ( ( Math . random ( ) * ( max - min + 1 ) ) / 2 ) * 2 + min ;
6
14
}
@@ -9,101 +17,85 @@ function getRandomOddNumber(min: number, max: number) {
9
17
return Math . floor ( ( Math . random ( ) * ( max - min ) ) / 2 ) * 2 + 1 + min ;
10
18
}
11
19
12
- function drawHorizontalWall (
20
+ async function drawHorizontalWall (
13
21
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
25
23
) {
26
24
for ( let pos = start ; pos <= end ; pos ++ ) {
27
- grid [ divisionPoint ] [ pos ] = CellType . wall ;
25
+ await updateCells ( grid , [ { row : divisionPoint , col : pos } ] , CellType . wall ) ;
28
26
}
29
- grid [ divisionPoint ] [ passagePoint ] = CellType . clear ;
27
+ await updateCells ( grid , [ { row : divisionPoint , col : passagePoint } ] ) ;
30
28
}
31
29
32
- function drawVerticalWall (
30
+ async function drawVerticalWall (
33
31
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
45
33
) {
46
34
for ( let pos = start ; pos <= end ; pos ++ ) {
47
- grid [ pos ] [ divisionPoint ] = CellType . wall ;
35
+ await updateCells ( grid , [ { row : pos , col : divisionPoint } ] , CellType . wall ) ;
48
36
}
49
- grid [ passagePoint ] [ divisionPoint ] = CellType . clear ;
37
+ await updateCells ( grid , [ { row : passagePoint , col : divisionPoint } ] ) ;
50
38
}
51
39
52
40
export async function generateRecursiveDivisionMaze ( {
53
41
rows,
54
42
cols,
55
43
entry,
56
44
exit,
45
+ updateGrid,
46
+ updateCells,
57
47
} : MazeAlgoProps ) {
58
48
const grid = generateGrid ( rows , cols , CellType . clear ) ;
49
+ updateGrid ( grid ) ;
59
50
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
+ }
63
60
64
- return grid ;
65
- }
61
+ const width = colEnd - colStart ;
62
+ const height = rowEnd - rowStart ;
63
+ const isHorizontal = width < height ;
66
64
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 ) ;
77
68
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
+ } ) ;
81
76
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 ) ;
85
82
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
+ } ) ;
92
90
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
+ }
98
95
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 ) ;
105
99
106
- recursiveDivision ( grid , rowStart , rowEnd , colStart , divisionPoint - 1 ) ;
107
- recursiveDivision ( grid , rowStart , rowEnd , divisionPoint + 1 , colEnd ) ;
108
- }
100
+ return grid ;
109
101
}
0 commit comments