@@ -5,9 +5,10 @@ import { GridContainer, Columns, Rows, NodeContainer } from "./Grid_styles";
55import { NodeClass } from "../node_component/Node_Class" ;
66import Node from "../node_component/Node" ;
77import {
8- end_algorithm_animation_action ,
9- end_maze_animation_action ,
10- restart_grid_action ,
8+ selected_pathfinding_algorithm_active_action ,
9+ selected_maze_algorithm_active_action ,
10+ selected_pathfinding_algorithm_on_grid_action ,
11+ selected_maze_algorithm_on_grid_action ,
1112} from "../../redux/global_actions" ;
1213import { a_star_algorithm } from "../../algorithms/a_star_algorithm" ;
1314import { dijkstra_algorithm } from "../../algorithms/dijkstra" ;
@@ -16,12 +17,16 @@ import { recursive_division_algorithm } from "../../mazes/recursive_division_alg
1617import { sidewinder_algorithm } from "../../mazes/sidewinder_algorithm" ;
1718
1819const Grid = ( {
19- active_algorithm,
20- active_maze,
21- active_maze_on_grid,
22- play_algorithm_animation,
23- end_algorithm_animation,
24- end_maze_animation,
20+ selected_pathfinding_algorithm,
21+ selected_maze_algorithm,
22+ selected_pathfinding_algorithm_active,
23+ selected_maze_algorithm_active,
24+ selected_pathfinding_algorithm_on_grid,
25+ selected_maze_algorithm_on_grid,
26+ selected_pathfinding_algorithm_active_fun,
27+ selected_maze_algorithm_active_fun,
28+ selected_pathfinding_algorithm_on_grid_fun,
29+ selected_maze_algorithm_on_grid_fun,
2530} ) => {
2631 // screen size
2732 const columns = Math . floor ( window . innerWidth / 30 ) ;
@@ -74,11 +79,20 @@ const Grid = ({
7479 } , [ ] ) ;
7580
7681 useEffect ( ( ) => {
77- if ( active_algorithm && play_algorithm_animation ) {
78- run_algorithm ( active_algorithm ) ;
82+ if (
83+ selected_pathfinding_algorithm &&
84+ selected_pathfinding_algorithm_active &&
85+ ! selected_pathfinding_algorithm_on_grid
86+ ) {
87+ run_algorithm ( selected_pathfinding_algorithm ) ;
7988 }
80- if ( active_maze && ! active_maze_on_grid ) {
81- run_maze ( active_maze ) ;
89+ if (
90+ selected_maze_algorithm &&
91+ selected_maze_algorithm_active &&
92+ ! selected_maze_algorithm_on_grid &&
93+ ! selected_pathfinding_algorithm_on_grid
94+ ) {
95+ run_maze ( selected_maze_algorithm ) ;
8296 }
8397 } ) ;
8498
@@ -94,7 +108,7 @@ const Grid = ({
94108 `node_${ path [ i ] . i } _${ path [ i ] . j } `
95109 ) ;
96110 node_js . className = "node_path" ;
97- if ( i === path . length - 2 ) end_algorithm_animation ( ) ;
111+ if ( i === path . length - 2 ) selected_pathfinding_algorithm_on_grid_fun ( ) ;
98112 } , i * 75 ) ;
99113 }
100114 } ;
@@ -143,7 +157,7 @@ const Grid = ({
143157 }
144158
145159 if ( i === maze . length - 1 ) {
146- end_maze_animation ( ) ;
160+ selected_maze_algorithm_on_grid_fun ( ) ;
147161 }
148162 } , i * 50 ) ;
149163 }
@@ -200,7 +214,7 @@ const Grid = ({
200214 // moving mouse around
201215 const on_mouse_enter = ( i , j ) => {
202216 if ( ! creating_obstacles ) return ;
203- if ( i !== start_i || ( j !== start_j && i !== end_i ) || j !== end_j ) {
217+ if ( ( i !== start_i || j !== start_j ) && ( i !== end_i || j !== end_j ) ) {
204218 mouse_action ( "CREATE_OBSTACLE" , i , j ) ;
205219 }
206220 } ;
@@ -225,6 +239,7 @@ const Grid = ({
225239 const run_algorithm = ( algorithm ) => {
226240 switch ( algorithm ) {
227241 case "RUN_A_STAR_ALGORITHM" :
242+ selected_pathfinding_algorithm_active_fun ( ) ;
228243 const [ visited_a_star , path_a_star ] = a_star_algorithm (
229244 grid [ start_i ] [ start_j ] ,
230245 grid [ end_i ] [ end_j ] ,
@@ -237,6 +252,7 @@ const Grid = ({
237252 }
238253 break ;
239254 case "RUN_BFS_ALGORITHM" :
255+ selected_pathfinding_algorithm_active_fun ( ) ;
240256 const [ visited_bfs , path_bfs ] = bfs_algorithm (
241257 grid [ start_i ] [ start_j ] ,
242258 grid [ end_i ] [ end_j ] ,
@@ -256,6 +272,7 @@ const Grid = ({
256272 const run_maze = ( maze ) => {
257273 switch ( maze ) {
258274 case "RUN_RECURSIVE_DIVISION_ALGORITHM" :
275+ selected_maze_algorithm_active_fun ( ) ;
259276 const recursive_maze = recursive_division_algorithm (
260277 grid ,
261278 columns ,
@@ -265,6 +282,7 @@ const Grid = ({
265282 break ;
266283
267284 case "RUN_SIDEWINDER_ALGORITHM" :
285+ selected_maze_algorithm_active_fun ( ) ;
268286 const sidewinder_maze = sidewinder_algorithm ( grid , columns , rows ) ;
269287 maze_nodes_animation ( sidewinder_maze ) ;
270288 break ;
@@ -309,21 +327,31 @@ const Grid = ({
309327// redux
310328const mapStateToProps = ( {
311329 global_reducer : {
312- active_algorithm,
313- active_maze,
314- active_maze_on_grid,
315- play_algorithm_animation,
330+ selected_pathfinding_algorithm,
331+ selected_maze_algorithm,
332+ selected_pathfinding_algorithm_active,
333+ selected_maze_algorithm_active,
334+ selected_pathfinding_algorithm_on_grid,
335+ selected_maze_algorithm_on_grid,
316336 } ,
317337} ) => ( {
318- active_algorithm,
319- active_maze,
320- active_maze_on_grid,
321- play_algorithm_animation,
338+ selected_pathfinding_algorithm,
339+ selected_maze_algorithm,
340+ selected_pathfinding_algorithm_active,
341+ selected_maze_algorithm_active,
342+ selected_pathfinding_algorithm_on_grid,
343+ selected_maze_algorithm_on_grid,
322344} ) ;
323345
324346const mapDispatchToProps = ( dispatch ) => ( {
325- end_algorithm_animation : ( ) => dispatch ( end_algorithm_animation_action ( ) ) ,
326- end_maze_animation : ( ) => dispatch ( end_maze_animation_action ( ) ) ,
347+ selected_pathfinding_algorithm_active_fun : ( ) =>
348+ dispatch ( selected_pathfinding_algorithm_active_action ( ) ) ,
349+ selected_maze_algorithm_active_fun : ( ) =>
350+ dispatch ( selected_maze_algorithm_active_action ( ) ) ,
351+ selected_pathfinding_algorithm_on_grid_fun : ( ) =>
352+ dispatch ( selected_pathfinding_algorithm_on_grid_action ( ) ) ,
353+ selected_maze_algorithm_on_grid_fun : ( ) =>
354+ dispatch ( selected_maze_algorithm_on_grid_action ( ) ) ,
327355} ) ;
328356
329357export default connect ( mapStateToProps , mapDispatchToProps ) ( Grid ) ;
0 commit comments