Skip to content

Commit e9bca32

Browse files
committed
Separate out MazeGrid to separate component
1 parent cba7dd2 commit e9bca32

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

src/components/MazeGrid.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { Cell } from '../algorithms/utils/PathfindingUtils';
3+
import { HiChevronDoubleRight, HiOutlineFlag } from "react-icons/hi";
4+
import '../styles/pathfinding.css';
5+
6+
7+
interface MazeGridProps {
8+
maze: Cell[][],
9+
handleMouseDown: (rowIndex: number, colIndex: number) => void,
10+
handleMouseEnter: (rowIndex: number, colIndex: number) => void,
11+
handleMouseUp: () => void
12+
}
13+
14+
const MazeGrid: React.FC<MazeGridProps> = ({ maze, handleMouseDown, handleMouseEnter, handleMouseUp }) => {
15+
return (
16+
<div onMouseUp={handleMouseUp}>
17+
{maze.map((row, rowIndex) => (
18+
<div key={rowIndex} className="pathfinding-row" id={'row-' + rowIndex}>
19+
{row.map((cell, colIndex) => (
20+
<div
21+
key={colIndex}
22+
onMouseDown={() => handleMouseDown(rowIndex, colIndex)}
23+
onMouseEnter={() => handleMouseEnter(rowIndex, colIndex)}
24+
style={{
25+
width: '25px',
26+
height: '25px',
27+
backgroundColor: cell.wall ? 'black' : (cell.finalPath ? 'yellow' : (cell.visited ? 'lightblue' : 'white')),
28+
border: '.5px solid lightblue',
29+
display: 'flex',
30+
alignItems: 'center',
31+
justifyContent: 'center'
32+
}}>
33+
{cell.start ? <HiChevronDoubleRight style={{ color: 'black', fontSize: '26px' }} /> : ''}
34+
{cell.end ? <HiOutlineFlag style={{ color: 'black', fontSize: '26px' }} /> : ''}
35+
</div>
36+
))}
37+
</div>
38+
))}
39+
</div>
40+
);
41+
};
42+
43+
export default MazeGrid;

src/pages/PathfindingVisualiser.tsx

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import '../styles/pathfinding.css';
44
import { HiChevronDoubleRight } from "react-icons/hi2";
55
import { HiOutlineFlag } from "react-icons/hi";
66
import { Cell } from '../algorithms/utils/PathfindingUtils';
7+
import MazeGrid from '../components/MazeGrid.tsx';
78
import { BreadthFirstSearch } from '../algorithms/pathfinding/BreadthFirstSearch.ts';
89
import { Dijkstra } from '../algorithms/pathfinding/Dijkstra.ts';
910
import { DepthFirstSearch } from '../algorithms/pathfinding/DepthFirstSearch.ts';
@@ -180,30 +181,12 @@ const PathfindingVisualiser: React.FC = () => {
180181

181182
return (
182183
<div>
183-
<div onMouseUp={handleMouseUp}>
184-
{maze.map((row, rowIndex) => (
185-
<div key={rowIndex} className="pathfinding-row" id={'row-' + rowIndex}>
186-
{row.map((cell, colIndex) => (
187-
<div
188-
key={colIndex}
189-
onMouseDown={() => handleMouseDown(rowIndex, colIndex)}
190-
onMouseEnter={() => handleMouseEnter(rowIndex, colIndex)}
191-
style={{
192-
width: '25px',
193-
height: '25px',
194-
backgroundColor: cell.wall ? 'black' : (cell.finalPath ? 'yellow' : (cell.visited ? 'lightblue' : 'white')),
195-
border: '.5px solid lightblue',
196-
display: 'flex',
197-
alignItems: 'center',
198-
justifyContent: 'center'
199-
}}>
200-
{cell.start ? <HiChevronDoubleRight style={{color: 'black', fontSize: '26px'}} /> : ''}
201-
{cell.end ? <HiOutlineFlag style={{color: 'black', fontSize: '26px'}} /> : ''}
202-
</div>
203-
))}
204-
</div>
205-
))}
206-
</div>
184+
<MazeGrid
185+
maze={maze}
186+
handleMouseDown={handleMouseDown}
187+
handleMouseEnter={handleMouseEnter}
188+
handleMouseUp={handleMouseUp}
189+
/>
207190
<button onClick={() => {triggerBFS();}} style={{backgroundColor: 'white', color: 'black'}}>Run BFS Visualisation</button>
208191
<button onClick={() => {triggerDFS();}} style={{backgroundColor: 'white', color: 'black'}}>Run DFS Visualisation</button>
209192
<button onClick={() => {triggerDijkstra();}} style={{backgroundColor: 'white', color: 'black'}}>Run Djikstra Visualisation</button>

0 commit comments

Comments
 (0)