A* Pathing classes for C# and C++


The following implementation of A* was mainly made for learning purposes and I couldn't find much in the way of A* in actual code to start with so I've put them up here so people can at least look (and also check out the theory tutorial that I used to make it.) Short of doing it with bit operators and optimizing it futher down just memory size wise it should be a pretty good starting point.

Example Code

The C# version implements the IComparable interface which lets us use easy functions such as the .Min() on a list to get the actual smallest (of whatever you implement it to compare with the CompareTo)

Public class AbsTile : IComparable { //Class logic and variables etc //comparing function for lists to use public int CompareTo(object obj) { if (obj is AbsTile) { var tile = obj as AbsTile; //or whatever your class is called. if (tile.FScore < FScore) return 1; if (tile.FScore > FScore) return -1; } return 0; } }

Above is how I implemented my Compare in my Tile class, returning 1 or -1 based on if it was greater or lesser and of course 0 if it somehow got compared to something that wasn't a Tile


These are of course not completely plug and play since they always rely on tiles of some sort that need to be decided by the programmer. Mostly I'm putting these up just for people that might want help with A* and see some practicle and commented implementations. I take no responsibility for the use of this code, as I said it's mostly for people who want to have a "cheat sheet" of sorts to check against.