title: Best Way to Store a Triangular/Hexagonal Grid in Python
body: <p>I'm making a game with hexagonal tiles, and have decided upon using a triangular/hexagonal grid. I found <a href="https://stackoverflow.com/questions/2049196/generating-triangular-hexagonal-coordinates-xyz">this</a> question which helped me generate coordinates, and slightly modified the code to store all the coordinates as keys in a dictionary with values of either "." (floor) or "X" (wall,) and included a function that prints out a string representation of the map where each non-blank character represents a hexagonal tile. This is the new code:</p>
  
  <pre><code>deltas = [[1,0,-1],[0,1,-1],[-1,1,0],[-1,0,1],[0,-1,1],[1,-1,0]]
  class HexGrid():
      def __init__(self, radius):
          self.radius = radius
          self.tiles = {(0, 0, 0): "X"}
          for r in range(radius):
              a = 0
              b = -r
              c = +r
              for j in range(6):
                  num_of_hexas_in_edge = r
                  for i in range(num_of_hexas_in_edge):
                      a = a+deltas[j][0]
                      b = b+deltas[j][1]
                      c = c+deltas[j][2]           
                      self.tiles[a,b,c] = "X"
  
      def show(self):
          l = []
          for y in range(20):
              l.append([])
              for x in range(60):
                  l[y].append(".")
          for (a,b,c), tile in self.tiles.iteritems():
              l[self.radius-1-b][a-c+(2*(self.radius-1))] = self.tiles[a,b,c]
          mapString = ""
          for y in range(len(l)):
              for x in range(len(l[y])):
                  mapString += l[y][x]
              mapString += "\n"
          print(mapString)
  </code></pre>
  
  <p>With this code, I can generate all the coordinates within the radius like so:</p>
  
  <pre><code>import hexgrid
  hg = hexgrid.HexGrid(radius)
  </code></pre>
  
  <p>and access a coordinate like this:</p>
  
  <pre><code>hg.tiles[a,b,c]
  </code></pre>
  
  <p>This seems to work fine for now, but I'm sure there must be some disadvantages to storing the map this way. If there are any disadvantages, could you please point them out, and maybe present a better way to store the map? Thanks a lot for your time.</p>