@@ -6,6 +6,7 @@ class Maze{
6
6
this . matrix = this . preparemaze ( this . rows , this . cols )
7
7
this . startNode = this . matrix [ 0 ] [ 0 ]
8
8
this . endNode = this . matrix [ 1 ] [ 1 ]
9
+
9
10
10
11
11
12
this . el . addEventListener ( "toggleWall" , ( ) => {
@@ -49,7 +50,6 @@ class Maze{
49
50
clearMaze ( ) {
50
51
this . matrix . forEach ( row => {
51
52
row . forEach ( node => {
52
- node . removeClass ( "wall" )
53
53
node . removeClass ( "searching" )
54
54
node . removeClass ( "curr" )
55
55
node . removeClass ( "inWay" )
@@ -105,6 +105,7 @@ class Node{
105
105
this . isWall = false
106
106
this . isStart = false
107
107
this . isEnd = false
108
+ this . temp = new Array ( 0 , 0 , 0 , 0 ) // temp border for prev wall memory, can be used if old memory removed
108
109
109
110
this . el = document . createElement ( "td" )
110
111
this . el . id = getId ( this . i , this . j )
@@ -119,14 +120,25 @@ class Node{
119
120
120
121
this . el . ondrop = ( e ) => this . drop ( e )
121
122
122
- this . el . onmousedown = ( e ) => { Node . clicked = true }
123
- this . el . onmouseover = ( e ) => { this . mouseEvent ( e ) }
124
- this . el . onmouseup = ( e ) => { Node . clicked = false }
123
+ this . el . onmousedown = ( e ) => { this . mouseEvent ( e , "down" ) }
124
+ this . el . onmouseover = ( e ) => { this . mouseEvent ( e , "over" ) }
125
+ this . el . onmouseup = ( e ) => { this . mouseEvent ( e , "up" ) }
125
126
}
126
127
127
- mouseEvent ( event ) {
128
- if ( Node . clicked ) {
129
- this . toggleWall ( )
128
+ mouseEvent ( event , type ) {
129
+ // event.preventDefault()
130
+ switch ( type ) {
131
+ case "down" :
132
+ if ( ! this . isStart && ! this . isEnd ) Node . clicked = true
133
+ break
134
+ case "up" :
135
+ Node . clicked = false
136
+ break
137
+ case "over" :
138
+ if ( ! this . isStart && ! this . isEnd && Node . clicked )
139
+ this . toggleWall ( )
140
+ break
141
+
130
142
}
131
143
}
132
144
@@ -138,15 +150,13 @@ class Node{
138
150
139
151
drop ( event ) {
140
152
event . preventDefault ( )
141
- if ( ! this . isStart && ! this . isEnd ) {
142
- let data = event . dataTransfer . getData ( "data" )
143
- let loc = Maze . getLoc ( event . target . id )
144
- if ( data == "start" ) {
145
- this . maze . setStartNode ( loc [ 0 ] , loc [ 1 ] )
146
- }
147
- if ( data == "end" ) {
148
- this . maze . setEndNode ( loc [ 0 ] , loc [ 1 ] )
149
- }
153
+ let data = event . dataTransfer . getData ( "data" )
154
+ let loc = Maze . getLoc ( event . target . id )
155
+ if ( data == "start" && ! this . isEnd ) {
156
+ this . maze . setStartNode ( loc [ 0 ] , loc [ 1 ] )
157
+ }
158
+ if ( data == "end" && ! this . isStart ) {
159
+ this . maze . setEndNode ( loc [ 0 ] , loc [ 1 ] )
150
160
}
151
161
}
152
162
@@ -157,16 +167,29 @@ class Node{
157
167
this . el . classList . remove ( name )
158
168
}
159
169
170
+
160
171
toggleWall ( ) {
161
172
if ( this . isWall ) {
173
+ // removing wall
162
174
this . isWall = false
163
175
this . removeClass ( "wall" )
164
- for ( let i = 0 ; i < this . sides ; ++ i ) this . removeWall ( i )
165
- return
176
+ this . border = this . temp
177
+ if ( this . border . toString ( ) != "0,0,0,0" ) {
178
+ for ( let i = 0 ; i < this . sides && ! this . temp ; ++ i )
179
+ this . removeWall ( i )
180
+ }
181
+ }
182
+ else {
183
+ // building wall
184
+ if ( ( this . border . toString ( ) != "1,1,1,1" ) ) {
185
+ console . log ( "here" ) ;
186
+ this . temp = new Array ( ...this . border )
187
+ }
188
+ this . isWall = true
189
+ this . addClass ( "wall" )
190
+ for ( let i = 0 ; i < this . sides ; ++ i )
191
+ this . addWall ( i )
166
192
}
167
- this . isWall = true
168
- this . addClass ( "wall" )
169
- for ( let i = 0 ; i < this . sides ; ++ i ) this . addWall ( i )
170
193
}
171
194
172
195
addWall ( i ) {
0 commit comments