I’m implementing a version of the A* path-finding algorithm in Javascript. To preserve my sanity, I’m using a pseudo-multi-dimensional array (i.e. nested arrays). If you aren’t familiar with A*, one step is checking the surrounding nodes of an “open” node.
How can I pass the location of the “open” node and check adjacent nodes in my array? Shouldn’t array locations be first-class?
function checknode (node) //Such as [5,2]
{
if(array[node+1][node]==something) //In this case [6,2]
}
Yeah, I know I could pass the x and y coordinates as seperate arguments, like so:
function checknode (nodex, nodey)
{
if(array[nodex+1][nodey]==something)
}
But the implications this has as far as my code is concerned is undesirable.
EDIT: Basically, I’m wondering if the location of an item of an array (5,2) can be passed into my function and manipulated.
You can make a composite value (using an object) that contains the two coordinates. That way you can pass it as a single parameter:
Example call: