So let's get this straight, the game will be played on the console? Right, well now you will need to set up your data structures, the most obvious is with nodes.
The nodes
Each hexagon is a node with six edges.
typedef struct Node {
void *object; /* Pointer to object */
Node *node_array; /* Pointer to node_array with 'node_count' nodes */
int node_count; /* size of node_array */
} Node;
How to initialize and connect the node structure
Imagine the following hexagon:
/\
| |
\/
It has the following edges, NORTHEAST, EAST, SOUTHEAST, SOUTHWEST, WEST and NORTHWEST. Next observe how they will be arranged (10, 11 and 12 were represented in Hex so that they can fit in one space):
// 0 1 2 3
// 4 5 6 7 8
// 9 A B C
So 0
will link to 5
through it's SOUTHEAST
link, and 4
through it's SOUTHWEST
link. Also notice how the rows alternate between odd and even numbers of elements. Let's call {0, 1, 2, 3}
row[0], and {4, 5, 6, 7, 8}
row[1]. And let's call this a 5x3 hexmap. The easiest way to create this array is with malloc(sizeof(Node) * width * height)
.
Connecting the nodes
First of all let me point out that every even row (0, 2, 4, ...) will have width-1
elements. But there's more, each element (x, y) on this row will link to the following element in your array:
- (x+1, y-1) - NORTHEAST
- (x+1, y) - EAST
- (x+1, y+1) - SOUTHEAST
- (x, y+1) - SOUTHWEST
- (x-1, y) - WEST
- (x, y-1) - NORTHWEST
Elements on the other rows, such as {4, 5, 6, 7, 8
} will have width
elements, where element (x, y) links to the following:
- (x, y-1) - NORTHEAST
- (x+1, y) - EAST
- (x, y+1) - SOUTHEAST
- (x-1, y+1) - SOUTHWEST
- (x-1, y) - WEST
- (x-1, y-1) - NORTHWEST
When trying to link (x1,y1) with (x2, y2), ensure that 0 <= x < width
and 0 <= y < height
.
Remember ...
Your array contains one unused element at the end of every two rows (row[0], row[2], etc.). Also you might want to provide them all with some sort of label or index so that you can refer the player to them. You could label them as (x,y) pairs, or numerically by their index, it's all up to you. The (x, y) pair is very easy for you since that will map directly to the array they are stored in.