Skip to content

Commit 1b5a317

Browse files
committed
implemented a star
1 parent 64b9f7a commit 1b5a317

File tree

8 files changed

+89
-8
lines changed

8 files changed

+89
-8
lines changed

src/components/Nav.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function Nav({
135135
value="Change End"
136136
isDisabled={isDisabled}
137137
onClick={async () => {
138+
console.log("active")
138139
await selectEnd(grid, startTile, endTile, isChangeEndSelectedRef);
139140
}}
140141
></ChangeStartOrEndPositionButton>

src/components/SelectButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function ChangeStartOrEndPositionButton({
1616
</label>
1717
<button
1818
disabled={isDisabled}
19-
className="bg-gray-700 cursor-pointer hover:bg-gray-800 transition ease-in active:ring-0 active:border-0 p-2 min-w-[200px] sm:min-w-full"
19+
className="bg-gray-700 cursor-pointer disabled:bg-gray-900 hover:bg-gray-800 transition ease-in active:ring-0 active:border-0 p-2 min-w-[200px] sm:min-w-full "
2020
id={lable}
2121
onClick={onClick}
2222
>{value}</button>

src/index.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
color-scheme: light dark;
1111
color: rgba(255, 255, 255, 0.87);
12-
background-color: #242424;
12+
background-color: #000000;
1313

1414
font-synthesis: none;
1515
text-rendering: optimizeLegibility;
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { getUntraversedNeighbours } from "../../../utils/getUntraversedneighbours";
2+
import { getEulerDistance, isEqual } from "../../../utils/helpers";
3+
import { isInQueue } from "../../../utils/isInQueue";
4+
import { GridType, TileType } from "../../../utils/types";
5+
6+
export const aStar = (
7+
grid: GridType,
8+
startTile: TileType,
9+
endTile: TileType
10+
) => {
11+
const traversedTiles: TileType[] = [];
12+
const baseTile = grid[startTile.row][startTile.col];
13+
baseTile.distance = 0;
14+
baseTile.isTraversed = true;
15+
const unTraversed = [baseTile];
16+
17+
while (unTraversed.length) {
18+
unTraversed.sort((a, b) => {
19+
return a.distance - b.distance;
20+
});
21+
const tile = unTraversed.shift() as TileType;
22+
if (tile.isWall) continue;
23+
if (tile.distance === Infinity) break;
24+
tile.isTraversed = true;
25+
traversedTiles.push(tile);
26+
if (isEqual(tile, endTile)) break;
27+
28+
const neighbors = getUntraversedNeighbours(grid, tile);
29+
for (let i = 0; i < neighbors.length; i++) {
30+
if (!isInQueue(neighbors[i], unTraversed)) {
31+
const neighbour = neighbors[i];
32+
neighbour.distance = tile.distance + getEulerDistance(neighbour);
33+
neighbour.parent = tile;
34+
unTraversed.push(neighbour);
35+
}
36+
}
37+
}
38+
39+
const path = [];
40+
let tile = grid[endTile.row][endTile.col];
41+
while (tile !== null) {
42+
tile.isPath = true;
43+
path.unshift(tile);
44+
tile = tile.parent!;
45+
}
46+
return { traversedTiles, path };
47+
};

src/utils/constants.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export const TILE_STYLE =
3131
'lg:w-[17px] md:w-[15px] xs:w-[8px] w-[7px] lg:h-[17px] md:h-[15px] xs:h-[8px] h-[7px] border-t border-r border-sky-200'
3232

3333
export const TRAVERSED_TILE_STYLE = TILE_STYLE + ' bg-cyan-400'
34-
export const START_TILE_STYLE = TILE_STYLE + ' bg-green-400'
35-
export const END_TILE_STYLE = TILE_STYLE + ' bg-red-400'
34+
export const START_TILE_STYLE = TILE_STYLE + ' bg-green-600'
35+
export const END_TILE_STYLE = TILE_STYLE + ' bg-red-600'
3636
export const WALL_TILE_STYLE = TILE_STYLE + ' bg-gray-400'
37-
export const PATH_TILE_STYLE = TILE_STYLE + ' bg-green-500'
37+
export const PATH_TILE_STYLE = TILE_STYLE + ' bg-green-400'
3838

3939
export const MAZES : MazeSelectType[] = [
4040
{name : 'No maze',value : 'NONE'},

src/utils/getUntraversedneighbours.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,43 @@ export const getUntraversedNeighbours = (grid: GridType, tile: TileType) => {
88
//* the order of if statements define in whibh direction the searching will begin here for example column is pushed first
99

1010
if (col < MAX_COLS - 1) {
11+
// #*# to save going after board
1112
neighbours.push(grid[row][col + 1]);
1213
}
1314

1415
if (row > 0) {
16+
// #*# to save going before board
1517
neighbours.push(grid[row - 1][col]);
1618
}
1719
if (row < MAX_ROWS - 1) {
20+
// #*# to save after before board
1821
neighbours.push(grid[row + 1][col]);
1922
}
2023
if (col > 0) {
21-
neighbours.push(grid[row][col - 1]); // by mistake was +1 which was pushing an undefined thing in the array breaking the program
24+
// #*# to save going before board
25+
neighbours.push(grid[row][col - 1]); //! by mistake was +1 which was pushing an undefined thing in the array breaking the program
2226
}
2327

28+
// ! code to enable 8 directional searching
29+
30+
// // upper left
31+
// if (row > 0 && col > 0) {
32+
// neighbours.push(grid[row - 1][col - 1]);
33+
// }
34+
// // upper right
35+
// if (col < MAX_COLS - 1 && row > 0) {
36+
// neighbours.push(grid[row - 1][col + 1]);
37+
// }
38+
39+
// // lower left
40+
// if (row < MAX_ROWS - 1 && col > 0) {
41+
// neighbours.push(grid[row + 1][col - 1]);
42+
// }
43+
44+
// // lower right
45+
// if (row < MAX_ROWS - 1 && col < MAX_COLS - 1) {
46+
// neighbours.push(grid[row + 1][col + 1]);
47+
// }
48+
2449
return neighbours.filter((neighbour) => !neighbour.isTraversed);
2550
};

src/utils/helpers.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MAX_COLS, MAX_ROWS } from "./constants";
1+
import { END_TILE_CONFIGURATION, MAX_COLS, MAX_ROWS } from "./constants";
22
import { GridType, TileType } from "./types";
33

44
const createRow = (row: number, startTile: TileType, endTile: TileType) => {
@@ -142,3 +142,8 @@ export const dropFromQueue = (tile: TileType, queue: TileType[]) => {
142142
}
143143
}
144144
};
145+
146+
export const getEulerDistance = (tile : TileType) => {
147+
const distance = Math.sqrt(Math.pow((END_TILE_CONFIGURATION.row-tile.row),2)+Math.pow((END_TILE_CONFIGURATION.col-tile.col),2))
148+
return parseFloat(distance.toFixed(2))
149+
}

src/utils/runPathFindingAlgorithm.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { aStar } from "../lib/algorithms/pathfinding/aStar";
12
import { bfs } from "../lib/algorithms/pathfinding/bfs";
23
import { dfs } from "../lib/algorithms/pathfinding/dfs";
34
import { djkistra } from "../lib/algorithms/pathfinding/djkistra";
@@ -20,7 +21,9 @@ export const runPathFindingAlgorithm = ({
2021
case 'DFS':
2122
return dfs(grid,startTile,endTile);
2223
case 'DJISKSTRA':
23-
return djkistra(grid,startTile,endTile);
24+
return djkistra(grid,startTile,endTile);
25+
case 'A_STAR':
26+
return aStar(grid,startTile,endTile);
2427

2528
default:
2629
return bfs(grid,startTile,endTile);

0 commit comments

Comments
 (0)