I am trying to code a simple pathfinding algorithm to find the path from a location to an end goal given a list of blocked points (squares). however my code just keeps running even if the end goal is right next to the location. I have attached screenshots of all relevant pieces of my code.
The idea is I find the path and then from the list of points (path) I can print what direction the player needs to move.
`private readonly Contains contains;
static Contains checkcontains = new Contains();
static Checker directionCheck = new Checker(checkcontains);
public Path(Contains contains) {
this.contains = contains;
}
public void checkPath(Points location, Points endGoal, List<Points> obstacles, List<string> Path){
List<Points> openList = new List<Points>();
List<Points> closedList = new List<Points>();
List<Points> pointPath = new List<Points>();
List<int> openFscore = new List<int>();
openList.Add(location);
int locationFscore = Fscore(location, location, endGoal);
openFscore.Add(locationFscore);
Points CurrentSq = location;
Path.Add("Start");
while(CurrentSq != endGoal){
int Closest = openFscore.Min();
Points Current = openList[openFscore.LastIndexOf(Closest)];
pointPath.Add(Current);
closedList.Add(Current);
openFscore.Remove(openFscore[openList.IndexOf(Current)]);
openList.Remove(Current);
List<Points> adjacent = [Current.northPoint(), Current.southPoint(), Current.eastPoint(), Current.westPoint()];
foreach (Points square in adjacent){
if (contains.Containers(closedList, square) == true || contains.Containers(obstacles, square) == true){
continue;
}
else if(contains.Containers(openList, square) == false){
openList.Add(square);
openFscore.Add(Fscore(location, square, endGoal));
}
}
CurrentSq = Current;
}
for(int i = 1; i <= pointPath.Count; i++){
int NS = pointPath[i].Y - pointPath[i-1].Y;
int EW = pointPath[i].X - pointPath[i-1].X;
string direction;
if (NS < 0){
direction = "South";
}
else if (NS > 0){
direction = "North";
}
else if (EW < 0){
direction = "West";
}
else if (EW > 0){
direction = "East";
}
else {
direction = "";
}
Path.Add(direction);
}
}
static int Gscore(Points location, Points current){
int score = Math.Abs(location.X - current.X) + Math.Abs(location.Y - current.Y);
return score;
}
static int Hscore(Points current, Points endGoal){
int score =Math.Abs(endGoal.X - current.X) + Math.Abs(endGoal.Y - current.Y);
return score;
}
static int Fscore(Points location, Points current, Points endGoal){
int score = Gscore(location, current) + Hscore(current, endGoal);
return score;
}
}`
Here is the code from the Points class:
public class Points {
public int X {get; private set;}
public int Y {get; private set;}
public Points(int x, int y){
X = x;
Y = y;
}
// store methods!!!!! for finding the NESW of each point
public Points readPoint(string[] input, int X, int Y) {
int x = int.Parse(input[X]);
int y = int.Parse(input[Y]);
return new Points(x, y);
}
public Points eastPoint() {
return new Points(X + 1, Y);
}
public Points westPoint(){
return new Points(X - 1, Y);
}
public Points northPoint(){
return new Points(X, Y + 1);
}
public Points southPoint(){
return new Points(X, Y - 1);
}
//modified modified modified
}
}
and the code from the contains class:
public bool Containers(List<Points> obstacles, Points point){
// check x co ords and then check y co ords
// default to false
// create new list (separate obstacles into x and y values)
string contains = "false";
// iterate through the given list to see if the x value is blocked
for (int i = 0; i < obstacles.Count; i ++){
if(obstacles[i].X == point.X){
if(obstacles[i].Y == point.Y){
contains = "true";
}
}
}
if (contains == "true"){
return true;
}
else {
return false;
}
}
}
user25197772 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.