I'm currently working on a game where I need to find the shortest route between two points.
https://i.sstatic.net/jBnEd.png
In my map, I have a 2D array called matrix: Node[][]
,
class Node{
index: {
x: number,
y: number
},
isAvailable: boolean
}
The goal is to calculate the shortest path taking node availability into consideration.
For example, nodes that are trees are marked as unavailable node.isAvailable = false
However, I've hit a roadblock when trying to implement the algorithm for this matrix.
I attempted to use Dijkstra's algorithm from here, but struggled with applying it. Here's what I tried:
const graph = new Dijkstra();
//convert the matrix (2d array) to graph
matrix.map((row) => {
row.map((node: Node) => {
let x = node.index.x;
let y = node.index.y;
graph.addVertex(x + ":" + y, {x: x, y: y});
});
});
console.log(graph.shortestPath('0:0', '5:5'));
//the output was ['0:0'] (definitely not the answer)
Can anyone provide guidance on how to properly apply the algorithm to this matrix?
By the way, here's my full code