# Grid Possible Movements, help needed

Hi, I’m working on a game that’s a turn based, battle game with tiles

What I need, and haven’t been able to figure out, is how to determine what tiles will be available for the unit to move

By selecting the current tile’s (the one where the selected unit is) neighbors (which I already calculate) I can highlight them and get the result for the image attached, but what I don’t know is how to iterate to get each neighbor’s neighbors until required

I can get this result

I’ve read about the Dijkstra and A* algorithm but I haven’t been able to execute it properly

Any help I get on this is much appreciated

Kind regards

You could check neighbors neighbors in a loop nth times until the max number of moves is reached and return an array of valid tiles, but some kind of pathfinding (A* and Dijkstra are pathfinding algorithms/patterns) would make it simpler, I think. Then you could just tell it to run based on your parameters (how many tiles you can move) and ask for all of the resulting valid tiles back.

I thought the A* Project asset had a tile example, but maybe I’m wrong.

Well, as I understand, what you want is a pathfinding algorithm.
And A* seems the more approproate/easy way to achieve that.

We can’t say anything more than, “yeah, you should use A*”.
You have to figure how to implement it.

But I know Quill18Creates shows how you could implement it in one of his “tutorial”, you could check it out.

I think you will need A* for the actual movement, but for selecting the tiles around you to decide where to move to I think its abit overkill.

All you need to do is add a function to your Tile class that takes a list of tiles, an int for the current depth and an int for the maxDepth wanted. Then in the function add the tiles neighbours to the list, checking to against the list to make sure its not already in there and then increase depth by 1 and if your not at your wanted depth go through the neighbours calling the same function with the same list.