Cell.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. '''
  2. Created on 8 nov. 2016
  3. Cell of a board game
  4. @author: olinox
  5. '''
  6. from core import constants
  7. class Cell(object):
  8. def __init__(self, geometry, x, y, z = 0):
  9. if not all(isinstance(value, int) for value in [x, y, z]):
  10. raise TypeError("x, y and z should be integers")
  11. self._geometry = geometry
  12. self._x = x
  13. self._y = y
  14. self._z = z
  15. self.__update_neighbours()
  16. @property
  17. def x(self):
  18. return self._x
  19. @property
  20. def y(self):
  21. return self._y
  22. @property
  23. def z(self):
  24. return self._z
  25. @property
  26. def coord(self):
  27. return (self._x, self._y)
  28. @property
  29. def coord3d(self):
  30. return (self._x, self._y, self._z)
  31. def __repr__(self):
  32. return "Cell {}".format(self.coord)
  33. def __update_neighbours(self):
  34. """update the tuple of neighbours cells"""
  35. x, y = self._x, self._y
  36. if self._geometry == constants.HEXGRID:
  37. if 1 == (x % 2):
  38. self._neighbours = ( (x, y-1), (x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1), (x-1, y) )
  39. else:
  40. self._neighbours = ( (x, y-1), (x+1, y-1), (x+1, y), (x, y+1), (x-1, y), (x-1, y-1) )
  41. elif self._geometry == constants.SQUAREGRID:
  42. self._neighbours = ( (x-1, y-1), (x, y-1), (x+1, y-1), \
  43. (x-1, y) , (x, y-1), (x+1, y) , \
  44. (x-1, y+1), (x, y+1),(x+1, y+1) )