Programming a Hexagon Board |
I received an e-mail asking about programming hexagon-based board games. I wrote a quick explanation, which I am posting here just in case any other people are wondering but aren't brave enough to send e-mail.
A hexagon-based board is just a staggered grid. Each row is offset 1/2 hexagon from the rows above and below it.
Figure 1 contains a pattern of Xs that should roughly represent a hex board. (Ignore the Row Y business until you read the text preceding Figure 2).
Figure 1.
Row Y: X X X X X
Row Y: X X X X X
Row Y: X X X X X
Row Y: X X X X X
Below is a representation of the above pattern using a value for each horizontal hexagon position (X) and row (Y):
Figure 2.
Row 0: 0 1 2 3 4
Row 1: 0 1 2 3 4
Row 2: 0 1 2 3 4
Row 3: 0 1 2 3 4
As a hexagon identified by X and Y, the rules for identifying neighbors are:
If you choose to reverse the rules for even and odd rows, it will result in the grid represented by Figure 3.
Figure 3.
Row 0: 0 1 2 3 4
Row 1: 0 1 2 3 4
Row 2: 0 1 2 3 4
Row 3: 0 1 2 3 4
To see an example, visit my programming page. Source code is not currently available.