79557609

Date: 2025-04-05 23:06:47
Score: 0.5
Natty:
Report link

The main problem as far as I see it is that you have a misunderstanding of what the A*-algorithm actually does. As far as I see it, you have some kind of rectangular field with discrete points on it and you want to have the shortest path between two points. Why do you use A* at all? You can just draw a horizontal and a vertical line and that's it.

The main point of route planning algorithms is to find the shortest path in an environment where NOT all paths from source to target are actually valid. For example, you could insert obstacles onto your raster (=points that are inside your rectangle, but are invalid).

Now to your code: You are computing the g-values incorrectly. g is actually the length of the path you have already taken. You cannot just use your heuristic-function here. You really have to build the correct sum. Also, you are using closedList.contains(n) and n2.equals(n), but your Node-class doesn't override hashCode() and equals(). These are the two major flaws I've found immediately. (There may be more...)

Side note: Your code is very verbose. You could for example collapse all cases in your nextNodes()-method into one loop that has four iterations. I'm just suggesting that, because you may get more helpful answers in the future if your code is cleaner. Also please improve your formatting for better readability.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: user1994405