EDIT: Wrapped the example map in a code block so the formatting is correct.
Ok, I'm trying to write an extremely simple A* algorithm over a hexagonal grid. I understand, and can do the A* portion. In fact, my A* works for square grids. What I can't wrap my brain around is finding neighbors with hexagons. Here's the layout for the heagonal grid
0101 0301
0201 0401
0102 0302
0202 0402
etc, etc
So, what I need help with is writing a Hexagon class that, given it's hex coordinates, can generate a list of neighbors. It needs to be able to generate neighbors which would 'fall off' the grid (like 0000 or 2101 in a 20x20 grid) because that's how my A* tracks across multiple maps laid side-by-side. So something that would work with this code snippet:
planet = Hex('0214') print(planet.neighbors()) ['Hex 0213', 'Hex 0215', 'Hex 0115', 'Hex 0315', 'Hex 0116', 'Hex 0316']
It depends on how you define the coordinates of your hex tiles.
Let's see.
, , , ,
/ \ / \ / \ / \
| A1| A2| A3| A4|
\ / \ / \ / \ /
| B1| B2| B3|
/ \ / \ / \ / \
| C1| C2| C3| C4|
\ / \ / \ / \ /
' ' ' '
In this case, neighbor definition is different for even and odd rows.
For a cell (X,Y) where Y is even, the neighbors are: (X,Y-1),(X+1,Y-1),(X-1,Y),(X+1,Y),(X,Y+1),(X+1,Y+1)
For a cell (X,Y) where Y is odd, the neighbors are: (X-1,Y-1),(X,Y-1),(X-1,Y),(X+1,Y),(X-1,Y+1),(X,Y+1)