我在一个简单的 Winforms 应用程序中实现了 A*-pathfinding。还可以在网格上定义障碍物,以便代理可以在它们周围导航。
问题是我的代理无法预见路径。这就是为什么他并不总是选择最佳路径的原因。
如何让探路者预测较短的路径?一旦代理走错了路,也会出现死胡同的问题。
此方法获取相邻单元格并计算值:
public void addAdjascent(Vector pos)
{
foreach(Cell cell in allCells)
{
bool containedInClosedList = closedList.Any(c => c.id == cell.id);
if (!containedInClosedList)
{
if (cell.positionCR.X == pos.X - 1 && cell.positionCR.Y == pos.Y)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if(!containedInBlockedList)
{
cell.H = calcH(goal,cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X + 1 && cell.positionCR.Y == pos.Y)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X && cell.positionCR.Y == pos.Y - 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X && cell.positionCR.Y == pos.Y + 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X - 1 && cell.positionCR.Y == pos.Y - 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X - 1 && cell.positionCR.Y == pos.Y + 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X + 1 && cell.positionCR.Y == pos.Y - 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
if (cell.positionCR.X == pos.X + 1 && cell.positionCR.Y == pos.Y + 1)
{
bool containedInBlockedList = blockedList.Any(c => c.id == cell.id);
if (!containedInBlockedList)
{
cell.H = calcH(goal, cell);
cell.G = calcG(start, cell);
cell.F = cell.G + cell.H;
openList.Add(cell);
}
}
}
}
}
下面是计算 G 和 H 值的代码:
public int calcG(Vector start,Cell cell)
{
int distance = closedList.Count;
return distance;
}
public int calcH(Vector goal, Cell cell)
{
int distance;
int distanceX = (goal.X) - cell.positionCR.X;
int distanceY = (goal.Y) - cell.positionCR.Y;
if (distanceX < 0)
{
distanceX = distanceX * -1;
}
if (distanceY < 0)
{
distanceY = distanceY * -1;
}
distance = distanceX + distanceY;
return distance;
}
最后我计算下一个我想踩的单元格:
public void step()
{
addAdjascent(stepPos);
bool foundDestination = false;
int min = 8000;
foreach(Cell openCell in openList)
{
if (openCell.F < min)
{
min = openCell.F;
}
}
if(openList.Count > 0)
{
foreach (Cell openCell in openList)
{
if (openCell.positionCR.X == goal.X && openCell.positionCR.Y == goal.Y)
{
closedList.Add(openCell);
stepPos = openCell.positionCR;
foundDestination = true;
}
}
}
if(!foundDestination)
{
closedList.Add(openList.First(item => item.F == min));
stepPos = openList.First(item => item.F == min).positionCR;
}
openList.Clear();
}