1

I am currently developing a simple game in Unity.

I got a game board composed of hexagons. Let's say, the red dot is the player.

Game board

Now I want to show the user on which fields he can go, depending on the number he diced. The user should not be allowed to enter one hexagons multiple times in one move.

Is there any algorithm to solve this path problem? I've only found algorithms which asserted whether there is a connection between two nodes, but no algorithm to get all possible moves.

Lolo
  • 11
  • 1

2 Answers2

1

Yes, in fact most algorithms that check whether there is a connection between two graph nodes also compute what the connection is. It's just a matter of not discarding the information that was used to make the yes/no judgement and instead returning it.

Path-finding algorithms are almost always recursive in nature: if the target node is adjacent, the answer is yes, otherwise check (via a self-call) whether it's adjacent to any of the adjacent ones, etc. Actually constructing the path just means returning a partial path on each call, which ultimately resolves to a complete path if the top-level call succeeds.

Kilian Foth
  • 110,899
1

The best resource for hex-grid algorithms I've ever read is this Red Blob Games article, and it has a whole section on movement range. It says:

If there are obstacles, the simplest thing to do is a distance-limited flood fill (breadth first search).

The exact implementation will depend on the coordinate system you've chosen to represent your hex grid, but essentially you'll find all the hexes that are one move away, then from that list you can find all the hexes that are two moves away, and so on...