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

前端 未结 9 557
迷失自我
迷失自我 2020-12-04 05:23

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

\"Hosted

Note that

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 05:40

    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.

提交回复
热议问题