Skip to content

Commit cba375b

Browse files
committed
Further refactoring Pathfinding Visualisation component
* Use Enum type over string to define algorithm * Move buttons and interactive components to ControlPanel
1 parent 3a9c1c9 commit cba375b

File tree

5 files changed

+39
-53
lines changed

5 files changed

+39
-53
lines changed

src/components/ControlPanel.tsx

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
11
// ControlPanel.tsx
22
import React from 'react';
3+
import { Algorithm } from '../utils/AlgorithmEnum';
34

4-
const ControlPanel = ({
5-
BFSSolver,
6-
DFSSolver,
7-
DijkstraSolver,
8-
PrimsMazeSolver,
9-
DFSMazeSolver,
10-
generateTable,
11-
clearLatestRun
12-
}: {
13-
BFSSolver: () => void,
14-
DFSSolver: () => void,
15-
DijkstraSolver: () => void,
16-
PrimsMazeSolver: () => void,
17-
DFSMazeSolver: () => void,
5+
interface ButtonLayoutProps {
6+
triggerAlgorithm: (algorithm: Algorithm) => void,
187
generateTable: () => void,
198
clearLatestRun: () => void
20-
}) => {
21-
22-
function runAlgorithm(setAlgorithm: () => void): void {
23-
clearLatestRun();
24-
setAlgorithm();
25-
}
9+
}
2610

11+
const ControlPanel: React.FC<ButtonLayoutProps> = ({ triggerAlgorithm, generateTable, clearLatestRun}) => {
2712
return (
2813
<div>
29-
<button onClick={() => runAlgorithm(BFSSolver)} style={{ backgroundColor: 'white', color: 'black' }}>Run BFS Visualisation</button>
30-
<button onClick={() => runAlgorithm(DFSSolver)} style={{ backgroundColor: 'white', color: 'black' }}>Run DFS Visualisation</button>
31-
<button onClick={() => runAlgorithm(DijkstraSolver)} style={{ backgroundColor: 'white', color: 'black' }}>Run Dijkstra Visualisation</button>
32-
<button onClick={() => runAlgorithm(PrimsMazeSolver)} style={{ backgroundColor: 'white', color: 'black' }}>Generate Prims Maze</button>
33-
<button onClick={() => runAlgorithm(DFSMazeSolver)} style={{ backgroundColor: 'white', color: 'black' }}>Generate DFS Maze</button>
34-
<button onClick={generateTable} style={{ backgroundColor: 'white', color: 'black' }}>Reset Board</button>
35-
<button onClick={clearLatestRun} style={{ backgroundColor: 'white', color: 'black' }}>Clear Latest Run</button>
14+
<button onClick={() => triggerAlgorithm(Algorithm.BFS)} style={{backgroundColor: 'white', color: 'black'}}>Run BFS Visualisation</button>
15+
<button onClick={() => triggerAlgorithm(Algorithm.BFS)} style={{backgroundColor: 'white', color: 'black'}}>Run DFS Visualisation</button>
16+
<button onClick={() => triggerAlgorithm(Algorithm.DIJKSTRA)} style={{backgroundColor: 'white', color: 'black'}}>Run Djikstra Visualisation</button>
17+
<button onClick={() => triggerAlgorithm(Algorithm.PRIMS_MAZE)} style={{backgroundColor: 'white', color: 'black'}}>Generate Prims Maze</button>
18+
<button onClick={() => triggerAlgorithm(Algorithm.DFS_MAZE)} style={{backgroundColor: 'white', color: 'black'}}>Generate DFS Maze</button>
19+
<button onClick={() => generateTable()} style={{backgroundColor: 'white', color: 'black'}}>Reset Board</button>
20+
<button onClick={() => clearLatestRun()} style={{backgroundColor: 'white', color: 'black'}}>Clear Latest Run</button>
3621
</div>
3722
);
3823
};

src/pages/PathfindingVisualiser.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React, { useState, useEffect } from 'react';
22
import useWindowDimensions from '../utils/useWindowDimensions';
33
import '../styles/pathfinding.css';
4-
import { HiChevronDoubleRight } from "react-icons/hi2";
5-
import { HiOutlineFlag } from "react-icons/hi";
6-
import { Cell } from '../algorithms/utils/PathfindingUtils';
74
import MazeGrid from '../components/MazeGrid.tsx';
85
import mazeHooks from '../utils/mazeHooks.ts';
96
import pathfindingAlgorithmHooks from '../utils/pathfindingAlgorithmHooks.ts';
7+
import ControlPanel from '../components/ControlPanel.tsx';
108

119
const PathfindingVisualiser: React.FC = () => {
1210
const { width = 0, height = 0 } = useWindowDimensions();
@@ -26,7 +24,7 @@ const PathfindingVisualiser: React.FC = () => {
2624
setStartNode,
2725
setEndNode,
2826
generateTable,
29-
clearAttribute,
27+
clearLatestRun,
3028
populateOptimalPath,
3129
} = mazeHooks(width, height);
3230

@@ -91,11 +89,6 @@ const PathfindingVisualiser: React.FC = () => {
9189
});
9290
};
9391

94-
function clearLatestRun(){
95-
clearAttribute('visited');
96-
clearAttribute('finalPath');
97-
}
98-
9992
return (
10093
<div>
10194
<MazeGrid
@@ -104,13 +97,7 @@ const PathfindingVisualiser: React.FC = () => {
10497
handleMouseEnter={handleMouseEnter}
10598
handleMouseUp={handleMouseUp}
10699
/>
107-
<button onClick={() => triggerAlgorithm('bfs')} style={{backgroundColor: 'white', color: 'black'}}>Run BFS Visualisation</button>
108-
<button onClick={() => triggerAlgorithm('dfs')} style={{backgroundColor: 'white', color: 'black'}}>Run DFS Visualisation</button>
109-
<button onClick={() => triggerAlgorithm('dijkstra')} style={{backgroundColor: 'white', color: 'black'}}>Run Djikstra Visualisation</button>
110-
<button onClick={() => triggerAlgorithm('primsMaze')} style={{backgroundColor: 'white', color: 'black'}}>Generate Prims Maze</button>
111-
<button onClick={() => triggerAlgorithm('dfsMaze')} style={{backgroundColor: 'white', color: 'black'}}>Generate DFS Maze</button>
112-
<button onClick={() => generateTable()} style={{backgroundColor: 'white', color: 'black'}}>Reset Board</button>
113-
<button onClick={() => clearLatestRun()} style={{backgroundColor: 'white', color: 'black'}}>Clear Latest Run</button>
100+
<ControlPanel triggerAlgorithm={triggerAlgorithm} generateTable={generateTable} clearLatestRun={clearLatestRun}/>
114101
</div>
115102
);
116103
};

src/utils/AlgorithmEnum.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum Algorithm {
2+
BFS = 'bfs',
3+
DIJKSTRA = 'dijkstra',
4+
DFS = 'dfs',
5+
PRIMS_MAZE = 'primsMaze',
6+
DFS_MAZE = 'dfsMaze'
7+
}

src/utils/mazeHooks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ const mazeHooks = (width: number, height: number) => {
7373
));
7474
};
7575

76+
function clearLatestRun(){
77+
clearAttribute('visited');
78+
clearAttribute('finalPath');
79+
}
80+
7681
async function populateOptimalPath(optimalPath: Cell[] | null) {
7782
if (!optimalPath) {
7883
return;
@@ -106,7 +111,7 @@ const mazeHooks = (width: number, height: number) => {
106111
setStartNode,
107112
setEndNode,
108113
generateTable,
109-
clearAttribute,
114+
clearLatestRun,
110115
populateOptimalPath,
111116
};
112117
};

src/utils/pathfindingAlgorithmHooks.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Dijkstra } from '../algorithms/pathfinding/Dijkstra.ts';
55
import { DepthFirstSearch } from '../algorithms/pathfinding/DepthFirstSearch.ts';
66
import { primsMazeGeneration } from '../algorithms/mazes/PrimsMazeGeneration.ts';
77
import { dfsMazeGeneration } from '../algorithms/mazes/DFSMazeGeneration.ts';
8+
import { Algorithm } from '../utils/AlgorithmEnum.ts';
89

910
interface PathfindingAlgorithmHooksProps {
1011
clearLatestRun: () => void;
@@ -23,31 +24,31 @@ const pathfindingAlgorithmHooks = ({
2324
endNode,
2425
setMaze,
2526
}: PathfindingAlgorithmHooksProps) => {
26-
const [algorithm, setAlgorithm] = useState<string | null>(null);
27+
const [algorithm, setAlgorithm] = useState<Algorithm | null>(null);
2728

2829
useEffect(() => {
2930
if (!algorithm) return;
3031

3132
const startCell = maze[startNode[0]][startNode[1]];
3233
const endCell = maze[endNode[0]][endNode[1]];
3334

34-
const algorithmMap: Record<string, () => Promise<void> | void> = {
35-
bfs: async () => {
35+
const algorithmMap: Record<Algorithm, () => Promise<void> | void> = {
36+
[Algorithm.BFS]: async () => {
3637
const optimalPath = await BreadthFirstSearch(maze, startCell, endCell, setMaze);
3738
if (optimalPath) populateOptimalPath(optimalPath);
3839
},
39-
dijkstra: async () => {
40+
[Algorithm.DIJKSTRA]: async () => {
4041
const optimalPath = await Dijkstra(maze, startCell, endCell, setMaze);
4142
if (optimalPath) populateOptimalPath(optimalPath);
4243
},
43-
dfs: async () => {
44+
[Algorithm.DFS]: async () => {
4445
const optimalPath = await DepthFirstSearch(maze, startCell, endCell, setMaze);
4546
if (optimalPath) populateOptimalPath(optimalPath);
4647
},
47-
primsMaze: () => {
48+
[Algorithm.PRIMS_MAZE]: () => {
4849
primsMazeGeneration(maze, startCell, endCell, setMaze);
4950
},
50-
dfsMaze: () => {
51+
[Algorithm.DFS_MAZE]: () => {
5152
dfsMazeGeneration(maze, startCell, endCell, setMaze);
5253
},
5354
};
@@ -57,9 +58,10 @@ const pathfindingAlgorithmHooks = ({
5758
setAlgorithm(null);
5859
}, [algorithm, maze, startNode, endNode, clearLatestRun, populateOptimalPath, setMaze]);
5960

60-
const triggerAlgorithm = (algorithm: 'bfs' | 'dijkstra' | 'dfs' | 'primsMaze' | "dfsMaze") => {
61+
const triggerAlgorithm = (algorithm: Algorithm) => {
6162
clearLatestRun();
62-
setAlgorithm(algorithm)};
63+
setAlgorithm(algorithm);
64+
};
6365

6466
return {
6567
triggerAlgorithm,

0 commit comments

Comments
 (0)