/
aStarMain.cpp
91 lines (80 loc) · 1.66 KB
/
aStarMain.cpp
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
#include "stdafx.h"//VS2010 Requirement
#include "Macros.h"
using namespace std;
enum Directions {Up,Down,Left,DLeft,DULeft,Right,DRight,DURight};
unsigned int currentx,currenty,targetx,targety=0;
int tf,tg,th,ti,tx,ty,f,g,h=0;
std::vector<int> dir;
bool openarray[GAMEARRAYSIZE_MOCKUP][GAMEARRAYSIZE_MOCKUP];
bool closedarray[GAMEARRAYSIZE_MOCKUP][GAMEARRAYSIZE_MOCKUP];
void Astar()
{
openarray[currentx][currenty]=true;
for(int i=-1;i<=1;i++)
{
for(int j=-1;j<=1;j++)//not sure if this is the correct iteration
{
if(i==0&&j==0)
{
continue;
}
tempx=currentx+j;
tempy=currenty+i;
if(closedarray[tempx][tempy]==true)
{
continue;
}else{
openarray[tempx][tempy]=true;
//currentx,currenty = parent of this tile;
th = abs(tempx-targetx) + abs(tempy-targety);
tg=tg;
tf=tg+th;
if(tf<f)
{
f=tf;
ti = i;
tj = j;
tx = tempx;
ty = tempy;
}
//check if this score is less than
}
}
}
if(ti==-1&&tj==-1)
{
dir.pushback(DLeft);
}else if(ti==0&&tj==-1)
{
dir.pushback(Left);
}else if(ti==1&&tj==-1)
{
dir.pushback(DULeft);
}else if(ti==-1&&tj==0)
{
dir.pushback(Down);
}else if(ti==1&&tj==0)
{
dir.pushback(Up);
}else if(ti==-1&&tj==1)
{
dir.pushback(DLeft);
}else if(ti==0&&tj==1)
{
dir.pushback(Left);
}else if(ti==1&&tj==1)
{
dir.pushback(DULeft);
}
currentx=tx;
currenty=ty;
}
int _tmain(int argc, _TCHAR* argv[])
{
while(currentx!=targetx&¤ty!=targety)
{
Astar(currentx,currenty);
}
for( vector<enum> const_iterator i = dir.begin(); i != dir.end(); ++i)
cout << *i << ' ';
}