@@ -3,13 +3,13 @@ import "./GraphTraversalVisualiser.css"
33import { UCS } from './graphAlgorithms/ucs' ;
44import { BFS } from './graphAlgorithms/bfs' ;
55import { DFS } from './graphAlgorithms/dfs' ;
6+ import { conflict } from './graphAlgorithms/helper' ;
67
78const MatrixVisualization = ( ) => {
89 const [ matrix , setMatrix ] = useState ( [ ] ) ;
910 const [ start , setStart ] = useState ( { row : 0 , col : 0 } ) ;
10- // const [objective, setObjective] = useState({ row: 0, col: 0 });
11-
1211 const [ objectives , setObjectives ] = useState ( [ ] ) ;
12+ // const [weights, setWeights] = useState([]);
1313 const [ obstacles , setObstacles ] = useState ( [ ] ) ;
1414 const [ algorithm , setAlgorithm ] = useState ( 'Reset' ) ;
1515 const [ button , setButton ] = useState ( false ) ;
@@ -19,42 +19,10 @@ const MatrixVisualization = () => {
1919 generateMatrix ( ) ;
2020 } , [ ] ) ;
2121
22- // function to check for conflict
23- const conflict = ( i , j , maze ) => {
24- // index checking
25- if ( i < 0 || i >= maze . length || j < 0 || j >= maze [ 0 ] . length ) {
26- return false ;
27- }
28- // if its currently in use
29- if ( maze [ i ] [ j ] === "w" ) {
30- return true ;
31- }
32- // if objective is surronded by obstacles
33- if ( maze [ i ] [ j ] === "o" ) {
34- if (
35- ( i > 0 && maze [ i - 1 ] [ j ] === "w" ) ||
36- ( i < maze . length - 1 && maze [ i + 1 ] [ j ] === "w" ) ||
37- ( j > 0 && maze [ i ] [ j - 1 ] === "w" ) ||
38- ( j < maze [ 0 ] . length - 1 && maze [ i ] [ j + 1 ] === "w" )
39- ) {
40- return true ;
41- }
42- }
43- return false ;
44- }
4522
46- // clear the old path
47- const clearPath = ( ) => {
48- const pathNodes = document . querySelectorAll ( '.path' ) ;
49- pathNodes . forEach ( ( node ) => {
50- node . classList . remove ( 'path' ) ;
51- } ) ;
52- const expandedNodes = document . querySelectorAll ( '.expanded' ) ;
53- expandedNodes . forEach ( ( node ) => {
54- node . classList . remove ( 'expanded' ) ;
55- } ) ;
56- } ;
5723
24+
25+ // generate a new matrix
5826 const generateMatrix = ( ) => {
5927 const n = 30
6028 const newMatrix = Array . from ( { length : n } , ( ) => Array ( n ) . fill ( " " ) ) ; // generate array
@@ -93,9 +61,11 @@ const MatrixVisualization = () => {
9361 // set the approriate obstacles, objectives and new matrix/grid
9462 setObjectives ( objectivesArray ) ;
9563 setObstacles ( obstaclesArray ) ;
64+ // setWeights(weightsArray);
9665 setMatrix ( newMatrix ) ;
9766 } ;
9867
68+ // reset the matrix
9969 const resetMatrix = ( ) => {
10070 clearPath ( ) ; // remove the previously drawn path
10171 generateMatrix ( ) ; // generate a new matrix
@@ -123,10 +93,20 @@ const MatrixVisualization = () => {
12393 }
12494 }
12595
96+ // clear the old path
97+ const clearPath = ( ) => {
98+ const pathNodes = document . querySelectorAll ( '.path' ) ;
99+ pathNodes . forEach ( ( node ) => {
100+ node . classList . remove ( 'path' ) ;
101+ } ) ;
102+ const expandedNodes = document . querySelectorAll ( '.expanded' ) ;
103+ expandedNodes . forEach ( ( node ) => {
104+ node . classList . remove ( 'expanded' ) ;
105+ } ) ;
106+ } ;
107+
126108 const handleSubmit = ( event ) => {
127109 let method = algorithm ;
128- let time = 0 ;
129- disableButtons ( ) ;
130110 if ( method === 'astar' ) {
131111 //
132112 } else if ( method === 'BFS' ) {
@@ -135,7 +115,6 @@ const MatrixVisualization = () => {
135115 colorNodes ( result . path , result . expanded ) ;
136116 } else if ( method === 'Reset' ) {
137117 resetMatrix ( ) ;
138- enableButtons ( 1 ) ;
139118 } else if ( method === 'DFS' ) {
140119 const result = DFS ( matrix , [ start . row , start . col ] , [ objectives [ 0 ] . row , objectives [ 0 ] . col ] ) ;
141120 colorNodes ( result . path , result . expanded ) ;
@@ -145,23 +124,13 @@ const MatrixVisualization = () => {
145124 colorNodes ( result . path , result . expanded ) ;
146125 }
147126
148- enableButtons ( time ) ;
149127 event . preventDefault ( ) ;
150128 } ;
151129
152130 const handleChange = ( event ) => {
153131 setAlgorithm ( event . target . value ) ;
154132 } ;
155133
156- const disableButtons = ( ) => {
157- setButton ( true ) ;
158- } ;
159-
160- const enableButtons = ( time ) => {
161- setTimeout ( ( ) => {
162- setButton ( false ) ;
163- } , time ) ;
164- } ;
165134
166135 return (
167136 < div className = 'GraphTraversalVisualiser' >
0 commit comments