Skip to content

Commit

Permalink
Layout, Dijkstras, Redux Setup Done
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhalevedant06 committed May 24, 2022
1 parent 2625bfb commit d2bdcdb
Show file tree
Hide file tree
Showing 21 changed files with 453 additions and 117 deletions.
76 changes: 76 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-redux": "^8.0.2",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Pathfinding Visualizer</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
Binary file removed public/logo192.png
Binary file not shown.
Binary file removed public/logo512.png
Binary file not shown.
25 changes: 0 additions & 25 deletions public/manifest.json

This file was deleted.

3 changes: 0 additions & 3 deletions public/robots.txt

This file was deleted.

62 changes: 62 additions & 0 deletions src/Algorithms/dijkstra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export function dijkstra(grid, startNode, finishNode) {
const visitedNodesInOrder = [];
startNode.distance = 0;
const unvisitedNodes = getAllNodes(grid);
while (!!unvisitedNodes.length) {
sortNodesByDistance(unvisitedNodes);
const closestNode = unvisitedNodes.shift();
// If we encounter a wall, we skip it.
if (closestNode.isWall) continue;
// If the closest node is at a distance of infinity,
// we must be trapped and should therefore stop.
if (closestNode.distance === Infinity) return visitedNodesInOrder;
closestNode.isVisited = true;
visitedNodesInOrder.push(closestNode);
if (closestNode === finishNode) return visitedNodesInOrder;
updateUnvisitedNeighbors(closestNode, grid);
}
}

function sortNodesByDistance(unvisitedNodes) {
unvisitedNodes.sort((nodeA, nodeB) => nodeA.distance - nodeB.distance);
}

function updateUnvisitedNeighbors(node, grid) {
const unvisitedNeighbors = getUnvisitedNeighbors(node, grid);
for (const neighbor of unvisitedNeighbors) {
neighbor.distance = node.distance + 1;
neighbor.previousNode = node;
}
}

function getUnvisitedNeighbors(node, grid) {
const neighbors = [];
const {col, row} = node;
if (row > 0) neighbors.push(grid[row - 1][col]);
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]);
if (col > 0) neighbors.push(grid[row][col - 1]);
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]);
return neighbors.filter(neighbor => !neighbor.isVisited);
}

function getAllNodes(grid) {
const nodes = [];
for (const row of grid) {
for (const node of row) {
nodes.push(node);
}
}
return nodes;
}

// Backtracks from the finishNode to find the shortest path.
// Only works when called *after* the dijkstra method above.
export function getNodesInShortestPathOrder(finishNode) {
const nodesInShortestPathOrder = [];
let currentNode = finishNode;
while (currentNode !== null) {
nodesInShortestPathOrder.unshift(currentNode);
currentNode = currentNode.previousNode;
}
return nodesInShortestPathOrder;
}
38 changes: 0 additions & 38 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,38 +0,0 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
31 changes: 13 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import logo from './logo.svg';
import './App.css';

import PathFinder from './Components/PathFinder';
import {Provider} from 'react-redux'
import store from './Redux/store'
import { selectData } from "./Redux/slice";
import { useSelector } from "react-redux";
function App() {
// const user = useSelector(isLoggedIn);

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
<>
<Provider store={store}>
<PathFinder/>
</Provider>

</>
);
}

Expand Down
8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

Loading

0 comments on commit d2bdcdb

Please sign in to comment.