forked from Code-Bullet/Tetris-AI-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AStar.js
93 lines (59 loc) · 2.04 KB
/
AStar.js
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
class AStar{
constructor(){
this.nodes = [];
this.createNodes();
this.addEdges();
this.gameWidth = game.gameWidth;
this.gameHeight = game.gameHeight;
}
createNodes(){
this.nodes = [];
for(let r = 0;r<4;r++){
for(let y =0; y< this.gameHeight;y++){
for(let x = 0 ; x<this.gameWidth;x++) {
this.nodes.push(new Node(x,y,r));
}
}
}
}
addEdges(){
let connectEdges = (node1, node2) => {
node1.edges.push(node2);
node2.edges.push(node1);
}
for(let r = 0;r<4;r++){
for(let y =0; y< this.gameHeight;y++){
for(let x = 0 ; x<this.gameWidth;x++) {
//connect horizontal edges
if(x!=0){
connectEdges(this.getNodeAtPosition(x-1,y,r), this.getNodeAtPosition(x,y,r))
}
if(x < this.gameWidth-1){
connectEdges(this.getNodeAtPosition(x,y,r), this.getNodeAtPosition(x+1,y,r))
}
//connect vertical (y) edges
if(y!=0){
connectEdges(this.getNodeAtPosition(x,y-1,r), this.getNodeAtPosition(x,y,r))
}
if(y< this.gameHeight-1){
connectEdges(this.getNodeAtPosition(x,y,r), this.getNodeAtPosition(x,y+1,r))
}
//connect rotational edges
//note one way edge
this.getNodeAtPosition(x,y,r).edges.push(x,y,(r+1)%4);
}
}
}
}
getNodeAtPosition(x,y,r){
return this.nodes[x + this.gameWidth*y + this.gameWidth*this.gameHeight* r];
}
findPathsToAllEndPoints(){
let paths = [];
let pathToExtend = new Path();
let winningPaths = new Path();
let extendedPath = new Path();
while(true){
}
}
}