Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let compass = ['North', 'East', 'South', 'West'];
- function calculateDirections(current, ending) {
- let queue = [];
- let reversePath = [];
- let step = {};
- step[current] = [];
- queue.push(current);
- while (queue.length > 0 && !pathFound) {
- current = queue.shift();
- for (let index in compass) {
- let neighbour = mapNodes[current][compass[index]];
- if (neighbour.length > 0 && !step[neighbour]) {
- step[neighbour] = [compass[index], current];
- if (neighbour == ending) {
- pathFound = true;
- break;
- }
- queue.push(neighbour);
- }
- }
- }
- if (pathFound) {
- current = ending;
- let currentStep;
- while ((currentStep = step[current]) && currentStep.length > 0) {
- let previous = currentStep[1];
- currentStep[1] = current;
- reversePath.push(currentStep);
- current = previous;
- }
- while (reversePath.length > 0) {
- currentPath.push(reversePath.pop());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement