javascript - Looping Through Array Until Solution Found -
so have array below first value of each array being road name, second value being connections road , third can ignore (all fake roads). trying find quickest route (least steps) 1 road looking @ connections.. code looking through phases defined myself, add further connections i'll have add further logic - there anyway of looping through undefined amount until finds best route (there no point can't find route)?
array:
var road = [ //name, connection, [left, middle, right] ["upper-g", ["lower-g", "left corner", "top corner"], []], ["upper-tw", ["lower-tw", "left corner", "right corner"], []], ["upper-rg", ["lower-rg", "upper-rg", "top corner", "right corner"], []], ["lower-g", ["upper-g", "left corner", "top corner"], [] ], ["lower-tw", ["upper-tw", "lower-pd", "upper-pd", "left corner", "right corner"], []], ["lower-rg", ["upper-rg", "top corner", "right corner"], []], ["left corner", ["lower-g", "upper-g", "lower-tw", "upper-tw"], []], ["top corner", ["upper-g", "lower-g", "lower-rg", "upper-rg"], []], ["right corner", ["upper-rg", "lower-rg", "upper-tw", "lower-tw"], []], ["upper-pd", ["lower-pd", "lower-tw", "upper-tw"], []], ["lower-pd", ["upper-pd", "lower-tw", "upper-tw"], []] ];
code: call findpath('roadweareat', 'roadtogetto') result
function findpath(croad, froad){ //find roads in array var coriginalpos = findroadinarr(croad); var foriginalpos = findroadinarr(froad); //if either cannot found, return undefined if(coriginalpos == undefined || foriginalpos == undefined){ return "undefined"; } //target arrays ease , add first road directions var croadarray = road[coriginalpos]; var froadarray = road[foriginalpos]; var roaddirections = findbestpath(croadarray, froadarray); //found or not if(roaddirections != undefined){ console.log(roaddirections); } else { console.log("not found"); } } function findbestpath(croadarray, froadarray){ var roaddirections = findinitialpath(croadarray, froadarray); if(roaddirections == undefined){ roaddirections = findsecondpath(croadarray, froadarray); if(roaddirections == undefined){ roaddirections = findthirdpath(croadarray, froadarray); if(roaddirections == undefined){ roaddirections = findfourthpath(croadarray, froadarray); } } } return roaddirections; } function findinitialpath(croadarray, froadarray){ var roaddirections = [croadarray[0]]; //loop through current roadarray for(var = 0; < croadarray[1].length; i++){ //if it's next each other if(croadarray[1][i] == froadarray[0]){ roaddirections.push(froadarray[0]); return roaddirections; } } } function findsecondpath(croadarray, froadarray){ var roaddirections = [croadarray[0]]; //loop through current roadarray for(var = 0; < croadarray[1].length; i++){ //if in inbetween road for(var j = 0; j < froadarray[1].length; j++){ if(croadarray[1][i] == froadarray[1][j]){ roaddirections.push(croadarray[1][i]); roaddirections.push(froadarray[0]); return roaddirections; } } } } function findthirdpath(croadarray, froadarray){ var roaddirections = [croadarray[0]]; //loop through current roadarray for(var = 0; < croadarray[1].length; i++){ var newcurrentroad = road[findroadinarr(croadarray[1][i])]; //loop through var trydirections = findsecondpath(newcurrentroad, froadarray); if(trydirections != undefined){ //roaddirections.push(croadarray[1][i]); for(var j = 0; j < trydirections.length; j++){ roaddirections.push(trydirections[j]); } return roaddirections; } } } function findfourthpath(croadarray, froadarray){ var roaddirections = [croadarray[0]]; for(var = 0; < croadarray[1].length; i++){ var newcurrentroad = road[findroadinarr(croadarray[1][i])]; //loop through var trydirections = findthirdpath(newcurrentroad, froadarray); if(trydirections != undefined){ for(var j = 0; j < trydirections.length; j++){ roaddirections.push(trydirections[j]); } return roaddirections; } } } function findroadinarr(roadname){ for(var = 0; < road.length; i++){ if(road[i][0] == roadname){ return i; } } }
thanks help, once find algorthims came across neat solution need: https://github.com/qiao/pathfinding.js/
Comments
Post a Comment