gneighbours.py 918 B

1234567891011121314151617181920212223242526272829
  1. '''
  2. Created on 19 nov. 2016
  3. @author: olinox
  4. '''
  5. from core import geometry
  6. def neighbours_of(grid_shape, x, y):
  7. if grid_shape == geometry.SQUARE:
  8. return squ_neighbours_of(x, y)
  9. elif grid_shape == geometry.HEX:
  10. return hex_neighbours_of(x, y)
  11. else:
  12. raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
  13. def hex_neighbours_of(x, y):
  14. """ returns the list of coords of the neighbours of a cell on an hexagonal grid"""
  15. if x%2 == 0:
  16. return [(x, y-1), (x+1, y-1), (x+1, y), (x, y+1), (x-1, y), (x-1, y-1)]
  17. else:
  18. return [(x, y-1), (x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1), (x-1, y)]
  19. def squ_neighbours_of(x, y):
  20. """ returns the list of coords of the neighbours of a cell on an square grid"""
  21. return [(x-1, y-1), (x, y-1), (x+1, y-1), \
  22. (x-1, y), (x+1, y) , \
  23. (x-1, y+1), (x, y+1),(x+1, y+1)]