Hi Slenkar,
I had to do this for a project recently, my implementation only allows 90-degree turns (no diagonals), but I hope this is still helpful:
What I did is to have an integer variable which stores the current direction (0 for across, 1 for down).
In each iteration of the main A* loop, you reassign the current node to which ever one on the open list has the lowest f score (gone + heuristic). Just before I reassign the current node, I store a reference to the previous 'current' node in another variable.
After setting the new 'current' node, I compare the co-ordinates of the 'previous' and 'current' nodes. If their x-coordinates are the same, that means that we are going down, if their y-coordinates are the same, that means that we are going across.
When looking at each of the nodes adjacent to the current node, I look at the direction from the 'current' node to the adjacent node. If the direction is the same (ie. the direction from the previous to the current node is the same as from the current node to the adjacent one), I set the 'gone' cost of the adjacent node to 1, if the direction is different, I set the 'gone' cost of the adjacent node to 2.
|