@@ -34,6 +34,7 @@ class GridLayout extends Component {
34
34
boxes :val2
35
35
} )
36
36
console . log ( document . getElementById ( `node-${ this . state . start [ 0 ] } -${ this . state . start [ 1 ] } ` ) ) ;
37
+ console . log ( this . state . boxes ) ;
37
38
}
38
39
}
39
40
@@ -42,6 +43,7 @@ class GridLayout extends Component {
42
43
this . setState ( {
43
44
boxes :val
44
45
} )
46
+ // this.updateStyle();
45
47
}
46
48
47
49
change = ( va ) => {
@@ -55,6 +57,7 @@ class GridLayout extends Component {
55
57
56
58
this . setState ( {
57
59
changed :true , start : va , unchanged : false } , ( ) => {
60
+
58
61
console . log ( "after state " , this . state . start ) ;
59
62
this . setState ( {
60
63
changed :false ,
@@ -78,6 +81,23 @@ class GridLayout extends Component {
78
81
79
82
// change(1);
80
83
84
+ // updateStyle(){
85
+ // for(let row=0;row<19;row++){
86
+ // for(let col=0;col<60;col++){
87
+ // document.getElementById(`node-${row}-${col}`).className = 'unvisited';
88
+ // if((row===this.state.start[0] && col===this.state.start[1]))
89
+ // {
90
+ // document.getElementById(`node-${row}-${col}`).className = 'start';
91
+ // }
92
+ // else if((row===this.state.end[0] && col===this.state.end[1]))
93
+ // {
94
+ // document.getElementById(`node-${row}-${col}`).className = 'end';
95
+ // }
96
+ // }
97
+ // }
98
+ // }
99
+
100
+
81
101
gridRender ( ) {
82
102
const b = [ ] ;
83
103
for ( let row = 0 ; row < 19 ; row ++ ) {
@@ -107,7 +127,7 @@ class GridLayout extends Component {
107
127
108
128
return b ;
109
129
}
110
-
130
+
111
131
112
132
grdRender ( r , c ) {
113
133
const b = [ ] ;
@@ -132,6 +152,7 @@ class GridLayout extends Component {
132
152
aDis :0
133
153
} ;
134
154
currRow . push ( val ) ;
155
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = 'unvisited' ;
135
156
}
136
157
b . push ( currRow )
137
158
}
@@ -180,12 +201,12 @@ class GridLayout extends Component {
180
201
const node = val [ row ] [ col ] ;
181
202
if ( st )
182
203
{
183
- if ( ! node . iswall && ! node . isweight ) {
204
+ if ( ! node . iswall && ! node . isweight && ! node . strt && ! node . end ) {
184
205
document . getElementById ( `node-${ row } -${ col } ` ) . className = '' ;
185
206
}
186
207
}
187
208
else {
188
- if ( ! node . iswall ) {
209
+ if ( ! node . iswall && ! node . strt && ! node . end ) {
189
210
document . getElementById ( `node-${ row } -${ col } ` ) . className = '' ;
190
211
}
191
212
}
@@ -416,7 +437,7 @@ class GridLayout extends Component {
416
437
const node = visitedNodes [ i ] ;
417
438
console . log ( node )
418
439
console . log ( node . row + " " + node . col ) ;
419
- console . log ( document . getElementById ( `node-${ node . row } -${ node . col } ` ) . id )
440
+ console . log ( document . getElementById ( `node-${ node . row } -${ node . col } ` ) . className )
420
441
document . getElementById ( `node-${ node . row } -${ node . col } ` ) . className +=
421
442
' node node-visited' ;
422
443
} , 10 * i ) ;
@@ -612,29 +633,110 @@ class GridLayout extends Component {
612
633
this . animateDijkstra ( visitedNodes , shortestPath ) ;
613
634
}
614
635
636
+ clearSurround ( row , col ) {
637
+ if ( row != 0 )
638
+ document . getElementById ( `node-${ row - 1 } -${ col } ` ) . className = 'gridblock unvisited' ;
639
+ if ( col != 0 )
640
+ document . getElementById ( `node-${ row } -${ col - 1 } ` ) . className = 'gridblock unvisited' ;
641
+ if ( col != 59 )
642
+ document . getElementById ( `node-${ row } -${ col + 1 } ` ) . className = 'gridblock unvisited' ;
643
+ if ( row != 18 )
644
+ document . getElementById ( `node-${ row + 1 } -${ col } ` ) . className = 'gridblock unvisited' ;
645
+
646
+ }
647
+
615
648
handleMouseDown ( row , col ) {
616
649
console . log ( "Down" ) ;
617
- const newgrid = this . getNewGridWithWall ( this . state . boxes , row , col ) ;
618
- this . setState ( {
619
- boxes :newgrid ,
620
- isMousePressed :true
621
- } ) ;
622
- document . getElementById ( `node-${ row } -${ col } ` ) . className = 'node-wall' ;
650
+ const node = this . state . boxes [ row ] [ col ]
651
+ console . log ( node )
652
+ if ( ! node . strt && ! node . end )
653
+ {
654
+ const newgrid = this . getNewGridWithWall ( this . state . boxes , row , col ) ;
655
+ this . setState ( {
656
+ boxes :newgrid ,
657
+ isMousePressed :true
658
+ } ) ;
659
+ console . log ( "wall -" , row , col ) ;
660
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = 'node-wall' ;
661
+ }
662
+ else if ( node . strt )
663
+ {
664
+ this . setState ( {
665
+ isStartPressed :true
666
+ } ) ;
667
+ console . log ( " -" , row , col ) ;
668
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = '' ;
669
+ }
670
+ else if ( node . end )
671
+ {
672
+ this . setState ( {
673
+ isEndPressed :true
674
+ } ) ;
675
+ console . log ( " -" , row , col ) ;
676
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = '' ;
677
+ }
678
+
623
679
624
680
}
625
681
626
682
handleMouseEnter ( row , col ) {
627
683
console . log ( "Enter -" + row + '-' + col ) ;
684
+ const node = this . state . boxes [ row ] [ col ]
628
685
629
- if ( ! this . state . isMousePressed ) return ;
630
- const newGrid = this . getNewGridWithWall ( this . state . boxes , row , col ) ;
631
- this . setState ( { boxes :newGrid } ) ;
686
+ if ( ! this . state . isMousePressed && ! this . state . isStartPressed && ! this . state . isEndPressed ) return ;
687
+ if ( this . state . isMousePressed )
688
+ {
689
+ if ( ! node . strt && ! node . end )
690
+ {
691
+ const newGrid = this . getNewGridWithWall ( this . state . boxes , row , col ) ;
692
+ this . setState ( { boxes :newGrid } ) ;
693
+ }
694
+ }
695
+
696
+ else if ( this . state . isStartPressed )
697
+ {
698
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = 'start' ;
699
+ this . setState ( {
700
+ // boxes:newGrid,
701
+ start :[ row , col ] ,
702
+ changed :true
703
+ } , ( ) => {
704
+ this . setState ( {
705
+ changed :false
706
+ } )
707
+ } )
708
+ this . clearSurround ( row , col ) ;
709
+ }
710
+
711
+ else if ( this . state . isEndPressed )
712
+ {
713
+ document . getElementById ( `node-${ row } -${ col } ` ) . className = 'end' ;
714
+ this . setState ( {
715
+ end :[ row , col ] ,
716
+ changed :true
717
+ } , ( ) => {
718
+ this . setState ( {
719
+ changed :false
720
+ } )
721
+ } )
722
+ this . clearSurround ( row , col ) ;
723
+ }
632
724
}
633
725
634
726
handleMouseUp ( ) {
635
727
console . log ( "Up" ) ;
636
-
637
- this . setState ( { isMousePressed :false } ) ;
728
+ if ( this . state . isMousePressed )
729
+ {
730
+ this . setState ( { isMousePressed :false } ) ;
731
+ }
732
+ else if ( this . state . isStartPressed )
733
+ {
734
+ this . setState ( { isStartPressed :false } ) ;
735
+ }
736
+ else if ( this . state . isEndPressed )
737
+ {
738
+ this . setState ( { isEndPressed :false } ) ;
739
+ }
638
740
}
639
741
640
742
@@ -651,6 +753,7 @@ class GridLayout extends Component {
651
753
return newGrid ;
652
754
}
653
755
756
+
654
757
chngstart ( ) {
655
758
this . grdRender ( ) ;
656
759
}
0 commit comments