Cell.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. '''
  2. Created on 8 nov. 2016
  3. Cell of a board game
  4. @author: olinox
  5. '''
  6. from core import geometry
  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._neighbours = ()
  16. self.__update_neighbours()
  17. @property
  18. def x(self):
  19. return self._x
  20. @property
  21. def y(self):
  22. return self._y
  23. @property
  24. def z(self):
  25. return self._z
  26. @property
  27. def coord(self):
  28. return (self._x, self._y)
  29. @property
  30. def coord3d(self):
  31. return (self._x, self._y, self._z)
  32. def __repr__(self):
  33. return "Cell {}".format(self.coord)
  34. @property
  35. def neighbours(self):
  36. return self._neighbours
  37. def __update_neighbours(self):
  38. """update the tuple of neighbours cells"""
  39. x, y = self._x, self._y
  40. if self._geometry == geometry.HEX:
  41. if 1 == (x % 2):
  42. self._neighbours = ( (x, y-1), (x+1, y), (x+1, y+1), (x, y+1), (x-1, y+1), (x-1, y) )
  43. else:
  44. self._neighbours = ( (x, y-1), (x+1, y-1), (x+1, y), (x, y+1), (x-1, y), (x-1, y-1) )
  45. elif self._geometry == geometry.SQUARE:
  46. self._neighbours = ( (x-1, y-1), (x, y-1), (x+1, y-1), \
  47. (x-1, y) , (x, y-1), (x+1, y) , \
  48. (x-1, y+1), (x, y+1),(x+1, y+1) )