Say I\'m building a board game with a hextile grid, like Settlers of Catan:
Note that
Such a grid can be represented in a two-dimensional array:
If
2
7 3
1
6 4
5
is the number one with its neighbors in the hex grid, then you can put this into a 2D array like so:
2 3
7 1 4
6 5
Obviously neighbor-ness is determined in this grid not only by being horizontally or vertically adjacent but also using one diagonal.
You can use a graph too, if you like to, though.
2
7 3
1
6 4
5
You can also try to 'flat' rows of your map. For this example it would be:
2
7 1 3
6 5 4
Its sometimes more useful to have rows in one row:P
I would suggest something like the following (I'll use Delphi-style declarations):
type
THexEdge = record
Hexes: array[1..2] of Integer; // Index of adjoining hexes.
// Other edge stuff goes here.
end;
THexVertex = record
Hexes: array[1..3] of Integer; // Index of adjoining hexes.
// Other vertex stuff goes here.
end;
THex = record
Edges: array[1..6] of Integer; // Index of edge.
Vertices: array[1..6] of Integer; // Index of vertex.
// Other hex stuff goes here.
end;
var
Edges: array of THexEdge;
Vertices: array of THexVertex;
HexMap: array of THex;
Each hex has six edges and six vertices. Each edge keeps track of its two adjoining hexes, and each vertex keeps track of its three adjoining hexes (hexes on the edges of the map will be a special case).
There are many things that you could do a different way of course. You could use pointers rather than arrays, you could use objects rather than records, and you could store your hexes in a two-dimensional array as other answerers have suggested.
Hopefully, that might give you some ideas about one way to approach it though.
I've dealt a lot with hexes. In cases like this, you track each of the 6 points for the borders of the hex. This lets you draw it quite easily.
You would have a single array of objects that represent hexes. Each of these hex objects also has 6 "pointers" (or an index to another array) pointing to another array of "sides". Same thing for "vertices". Of course the vertices would have 3 pointers to the adjoining hexes, and the sides would have 2.
So, a hex may be something like: X, Y, Point(6), Vertices(6), Sides(6)
Then you have a Hex array, vertice array, and side array.
Then it is pretty simple to find the vertices/sides for a hex, or whatever.
When I say pointer it could just as easily be an integer pointing to the element in the vertice or side array or whatever. And of course arrays could be lists or whatever.
Amit Patel has posted an amazing page on this topic. It's so comprehensive and wonderful that it needs to be the definitive answer to this question: Hexagonal Grids
This article goes through how to set up a Isomeric/Hexagonal grid game. I recommend you have a look at the Forcing Isometric and Hexagonal Maps onto a Rectangular Grid
section and the the movement section. Although it is different from what you are looking for it may help you formulate how to do what you want.