-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.js
More file actions
140 lines (127 loc) · 4.08 KB
/
AStar.js
File metadata and controls
140 lines (127 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Find the shortest route to exit from a maze. 0 free space; 1 blocked space; 3 start/ end.
let map =[
[3,0,1,0,0,0,0,0],
[0,0,0,0,1,1,0,0],
[1,1,1,1,1,1,0,1],
[0,0,0,0,0,0,0,1],
[0,1,1,1,1,1,1,1],
[0,1,0,0,0,1,0,0],
[0,1,0,1,0,1,0,0],
[0,0,0,1,0,0,0,3]
];
let FINISH = 3;
let FREESPACE = 0;
let BLOCKEDSPACE = 1;
let MAPSIZE = {
'maxX': map[0].length - 1,
'maxY': map.length - 1
};
const findTheShortest = (initialX, initialY, finalX, finalY, map) => {
let FINALX = finalX;
let FINALY = finalY;
let openList = {};
let closeList = {};
openList[initialX+"x"+initialY] = {
'x': initialX,
'y': initialY,
'father': null,
'toStart': 0,
'toFinish': Math.abs(initialX - FINALX) + Math.abs(initialY - FINALY),
'cost': Math.abs(initialX - FINALX) + Math.abs(initialY - FINALY)
};
let nextPosition = {'x': initialX, 'y': initialY};
let currentPositionKey = initialX+"x"+initialY;
const canGoUpDown = (tile, step) => {
let arrayLine = tile.y + step;
let arrayColumn = tile.x;
if(arrayLine < 0 || arrayLine > MAPSIZE.maxY) return {'block': true};
let tileKey = arrayColumn+"x"+arrayLine;
if(openList[tileKey]) return {'block': true};
if(closeList[tileKey]) return {'block': true};
if(map[arrayLine][arrayColumn]===BLOCKEDSPACE) return {'block': true};
let answer = {'x': arrayColumn, 'y': arrayLine }
if(map[arrayLine][arrayColumn]===FINISH) answer['end'] = true;
return answer;
}
const canGoLeftRight = (tile, step) => {
let arrayColumn = tile.x + step;
let arrayLine = tile.y;
let tileKey = arrayColumn+"x"+arrayLine;
if(openList[tileKey]) return {'block': true};
if(closeList[tileKey]) return {'block': true};
if(arrayColumn < 0 || arrayColumn > MAPSIZE.maxX ) return {'block': true};
if(map[arrayLine][arrayColumn]===BLOCKEDSPACE) return {'block': true}
let answer = {'x': arrayColumn, 'y': arrayLine};
if(map[arrayLine][arrayColumn]===FINISH) answer['end'] = true;
return answer;
}
const addToOpenList = (position, fatherKey) => {
let start = openList[fatherKey].toStart + 1;
let finish = Math.abs(position.x - FINALX) + Math.abs(position.y - FINALY);
openList[position.x+"x"+position.y] = {
'x': position.x,
'y': position.y,
'father': openList[fatherKey].x +"x"+openList[fatherKey].y,
'toStart': start,
'toFinish': finish,
'cost': start + finish
};
}
const getSmallerCost = (list) => {
let smallCost = 0;
let smallKey = '';
for(let key in list){
if(list[key].cost < smallCost || smallKey === ''){
smallKey = key;
smallCost = list[key].cost;
}
}
return smallKey;
}
const moveToCloseList = (key) => {
closeList[key] = {...openList[key]};
delete openList[key];
}
do{
let noNewAdd = true;
nextPosition = canGoUpDown(openList[currentPositionKey],-1);
if(nextPosition.block!= true){
addToOpenList(nextPosition,currentPositionKey);
noNewAdd = false;
}
nextPosition = canGoUpDown(openList[currentPositionKey],1);
if(nextPosition.block != true){
addToOpenList(nextPosition, currentPositionKey);
noNewAdd = false;
}
nextPosition = canGoLeftRight(openList[currentPositionKey], -1);
if(nextPosition.block!= true){
addToOpenList(nextPosition, currentPositionKey);
noNewAdd = false;
}
nextPosition = canGoLeftRight(openList[currentPositionKey], 1);
if(nextPosition.block!= true){
addToOpenList(nextPosition, currentPositionKey);
noNewAdd = false;
}
moveToCloseList(currentPositionKey);
if(nextPosition.end) {
currentPositionKey = finalX+"x"+finalY;
moveToCloseList(currentPositionKey);
break;
}
currentPositionKey = getSmallerCost(openList);
if(currentPositionKey==="") {
console.log("Dead End");
break;
}
}while(currentPositionKey != "");
let next = currentPositionKey;
let route = [];
while(next!=null){
route.push(next);
next = closeList[next].father;
}
return route;
}
console.log(findTheShortest(0,0,7,7,map));