# Pathfinding in hex grid based game

I'm making a TBS using a hex grid. Player can move units from one hex to another using bridges, which will be randomly generated. I'm using cubic and axial coordinate system for my grid, it's well described in Red Blob Games' Hexagonal Grids.

I don't know how to make a hex understand connection with another one and then calculate best path for units.

I'm not sure how to approach the issue - should I make six slots for bridges in my hex class and then calculate path or should I make movement system somehow and then use absence of bridge as a "blocker"?

You can just apply A*( A-star ). Compared to a uniform square grid the only difference is the way you collect the adjacent tiles ( aka your hexagons ).

Each tile should have a table of booleans representing the bridges corresponding to their direction like so

//Depending on your hexagon order
enum Direction{
NORTH,
NORTH_EAST,
SOUTH_EAST,
SOUTH,
SOUTH_WEST,
NORTH_WEST
}


Bool[] bridgeTable = new Bool[Direction.length];


In your A* just iterate over your enum list and check the corresponding boolean table that goes with it. This however assumes you always start with the tile which is placed north from the current tile.

For example:

//If there is no bridge
if(!bridgeTable[Direction.NORTH]) continue;
else //calc costs etc


You could also pre-calculate the shortest path from one end to another. If new bridges are added or removed you need to recalculate the table/graph: Floyd_algorithm