A* algorithm in c++!

This is not an algorithm to get you an A* in your GCSE’s. Don’t be so silly. A* (Astar) is a path finding algorithm used to find…. paths.
It is very similar to Dijkstra’s algorithm, if you know what that is. I found a great explanation here: http://www.policyalmanac.org/games/aStarTutorial.htm , so I’m not just going to recreate that in a lesser form without diagrams. If you want to understand it, go check that out.

The following source code  makes no attempt at clarity or efficiency. Nor does it claim to make good use of spelling. It does, however, work. It also has no formatting thanks to wordpress. But perhaps if you cut and paste it into a c++ program, it might get sorted for you. Who knows.

It first randomly generates a small world, which is filled with walls (0’s) and empty space ( ). This is seeded the same everytime, but could be changed with a call to time(0) (#include<time.h> I *think*). The A* algorithm is the called to find a route from the top left to the bottom right.

Next, the end of the A* is printed. Start in the bottom right, and just go in the direction indicated (u for up, r for right, l for left, d for down). Keep doing this until you reach the starting position. Or not if it has failed, which is possible!

Lastly I get the computer to compute the route, and print it out with little @ symbols. Lovely.


Astar– This is a word document!

Read the rest of this entry »