Beep, boop, da-be-da-boop.
Ahem! A* is probably not what you want to start out, with you don’t already understand how to do Dijkstra’s and all of that.
Anyway, what A* star does is…
- Keeps track of visited nodes and the ‘best cost’ to get to that node. (Typically called the closed list).
- Keeps track of nodes that can be visited during the next iteration, sorted by (And this is important) the distance from your start to that node plus a guess as to how far it is away from the goal.
Then, the algorithm works as follows:
- Put the starting node in the ‘open’ queue.
- Pop the top off your open queue.
- Put it in the closed list.
- Check if it’s the goal. If you haven’t reached it yet, set it as the best, otherwise check if it’s the best (Meaning least ground covered).
4.1) If it’s not the goal, get all of its adjacent nodes.
- For each adjacent…
5.1) Check if getting to that node would take longer than your current best path. If it does discard it, otherwise…
5.2) Then, check whether it’s in the closed list. If it is, check if it’d be a shorter path to get to it.*** If it is, compute the ‘expected distance’ then add it to the open list.
5.3) If it’s not in the closed list, compute the ‘expected distance’ then add it to the open list.
- If there’s a node in the open list, go back to 2.
- The best path is your best path.
*** Depending on how you want to implement it, and how “good” your guess of distance remaining is, you might be able to skip this part.
The guess depends on your map and various other stuff. Typically, you use something like a Manhattan distance (Think adding the opposite and adjacent sides of the right angle formed by the line between the two positions) or the Euclidean distance (Straight line distance). These are ‘good’ for a given value of good.
If you want some real code for this, give me a poke and I’ll write up something.