How do I represent a hextile/hex grid in memory?

Viewed 52554

Say I'm building a board game with a hextile grid, like Settlers of Catan:

Hosted by imgur.com

Note that each vertex and edge may have an attribute (a road and settlement above).

How would I make a data structure which represents this board? What are the patterns for accessing each tile's neighbors, edges and vertices?

9 Answers

You could create a 2D array and then consider the valid positions as:

  • On even-numbered rows (0,2,4,...): the odd numbered cells.
  • On odd-numbered rows (1,3,5,...): the even numbered cells.

For each cell, its neighbors would be:

  • Same column, 2 rows up
  • Same column, 2 rows down
  • 1 left + 1 up
  • 1 left + 1 down
  • 1 right + 1 up
  • 1 right + 1 down

Illustration:

Hex Grid

The x marks are hexes. x that are diagonal to each other are neighbors. | connects vertical neighbors.

I am sitting here "in my free time coding for fun" with hexes. And it goes like this... I will tell you what it looks like in words.

  1. Hexagon: it has six neighbour hexagons. It can deliver the reference for each neighbouring hex tile. It can tell you what it consists of(water ,rock, dust). It can connect itself to others and vice versa. It can even automatically connect the others surrounding him to create a greater field and or making sure all fields can be adressed by its neighbours.
  2. A building references up to three roads and three Hex Tiles. They can tell you which they are.
  3. A road references two hexes and other roads when they are adressed by neighbouring tiles. They can tell which tiles that are and which roads or buildings they connect to.

This is just an idea how I would work on it.

Related