Example #1
0
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;
}