3

I am programming a 2d game, and I was wondering what the best data structure would be to store all of the hexagonal tiles in a map class? This is in java.

Cayle
  • 199
  • 1
  • 1
  • 11
  • This is a great diagram to deciding what collection is best for specific situations: http://stackoverflow.com/questions/21974361/what-java-collection-should-i-use – aliteralmind Mar 03 '14 at 01:51
  • You should know 2D hexagonal tiles can be described by only 2 coordinates. A two-dimensional data structure might be something to look into – Jochem Kuijpers Mar 03 '14 at 01:53
  • 3
    You might want to take a look at this: http://www.redblobgames.com/grids/hexagons/ – Jochem Kuijpers Mar 03 '14 at 01:57

2 Answers2

6

Just use a 2D array, but have the "rows" and "columns" at a 60 or 120 degree angle to each other.

My quick ASCII illustration:

0   1   2   3   4   5   6  
  1   2   3   4   5   6  
1   2   3   4   5   6   7
  2   3   4   5   6   7
2   3   4   5   6   7   8
  3   4   5   6   7   8
3   4   5   6   7   8   9

(the numbers are row numbers, the column numbers are obvious...)

Effectively, you want to "stripe" the rows across the columns.

mikera
  • 105,238
  • 25
  • 256
  • 415
3

Just simple 2D array is good idea, you have all the info you need there :

enter image description here

libik
  • 22,239
  • 9
  • 44
  • 87
  • Actually that labeling is very awkward, since the difference vector to get to neighboring cells depends on the cell's coordinates itself. It would be more sensible to label along the "diagonals" IMHO – Niklas B. Mar 03 '14 at 01:57
  • @NiklasB. - I think it really does not matter in any way – libik Mar 03 '14 at 02:06
  • @NiklasB. You mean like mikera's solution? Doesn't that have the same problem (although perhaps to a lesser extent)? Moving straight down, right, left or up (e.g. 0,0, 0,2, 0,4 following the mapping in this answer - so skipping every second row) results in a coordinate distance change of 1, where it actually takes 2 moves to get there. – Bernhard Barker Mar 03 '14 at 09:02