Search code examples
hexagonal-tiles

Hexagonal board data representation


I'll be honest right now and admit that this is for a school project. We're supposed to design a solver for a game (I won't go into details, that's not the purpose of my post) involving a hexagonal board, such as this one. My problem is this: I don't know how to represent this board as data. We're using a functional language for this (won't go into specifics here), and I'm not so great at functional programming. If anyone has any tips as to how I can wrap my head around representing a hexagonal game board as data in a functional programming paradigm, that would be great!

I know this post might be super vague...I'm just stuck at data representation, and I'm sure that I'll be able to make some headway into this solver once I get some sort of idea as to how to represent this board as data.


Solution

  • Notice how alternate rows line up perfectly, suggesting using a 2D array. Further notice that the rows in between also all line up perfectly. This suggests 2 approaches:

    1. Use 2 distinct 2D arrays, one for the odd rows & another for the even ones
    2. Use a single 2D array, keeping in mind that the odd rows are all offset a 1/2 column form the even ones.

    Even if you can't use an array directly, you can think of organizing the cells this way, and represent those locations in whatever way is appropriate to your constraints. You might even use floats for the column indices (so that you can more faithfully represent the in-between rows elements positions using an x-index of ?.5).