I have a function for A star pathfinding that does not allow diagonal movement where I have penalties for changing direction (F score increased). However, I have found that the resulting path is not always linked, often jumping back to a random square.
This is my A star function. However say the location is (10, 4) and the end goal is (0, 13) the expected path should be (10, 4) to (0, 4) and then upwards to (0, 13). However the code returns the list of points: (0,4), (1, 3), (0, 5). Any suggestions on how to remove this error would be greatly appreciated!!! I have attached my code for my pathfinding and my F score calculations
public void checkPath(Points location, Points endGoal, List<Points> obstacles, List<string> Path){
List<Points> openList = new List<Points>();
List<Points> closedList = obstacles;
List<Points> pointPath = new List<Points>();
List<double> openFscore = new List<double>();
openList.Add(location);
List<Points> adjacent;
double locationFscore = Fscore(location, location, endGoal, pointPath);
openFscore.Add(locationFscore);
while(contains.Containers(pointPath, endGoal) == false){
double Closest = openFscore.Min();
Points Current = openList[openFscore.LastIndexOf(Closest)];
pointPath.Add(Current);
closedList.Add(Current);
openFscore.Remove(openFscore[openList.IndexOf(Current)]);
openList.Remove(Current);
adjacent = [Current.northPoint(), Current.southPoint(), Current.eastPoint(), Current.westPoint()];
foreach (Points square in adjacent){
if (contains.Containers(closedList, square) == true){
continue;
}
else if(contains.Containers(openList, square) == false){
openList.Add(square);
openFscore.Add(Fscore(location, square, endGoal, pointPath));
}
}
}
for(int i = 0; i < pointPath.Count; i++){
string direction = "("+ pointPath[i].X + ", " + pointPath[i].Y + ")";
Path.Add(direction);
}
static double Gscore(Points location, Points current){
double score = Math.Abs(location.X - current.X) + Math.Abs(location.Y - current.Y);
return score;
}
static double Hscore(Points current, Points endGoal){
double score = Math.Abs(endGoal.X - current.X) + Math.Abs(endGoal.Y - current.Y);
return score;
}
static double Fscore(Points location, Points current, Points endGoal, List<Points> pointPath){
double score = Gscore(location, current) + Hscore(current, endGoal);
if (pointPath.Count >= 2){
int length = pointPath.Count - 1;
int Xdir = Math.Abs(pointPath[length].X - pointPath[length - 1].X);
int Ydir = Math.Abs(pointPath[length].Y - pointPath[length - 1].Y);
if (Xdir == 0 && Math.Abs(pointPath[length].X - current.X) != 0){
score *= 1.1;
return score;
}
if (Ydir == 0 && Math.Abs(pointPath[length].Y - current.Y) != 0){
score *= 1.1;
return score;
}
else{
return score;
}
}
else {
return score;
}
}
user25197772 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.