fork download
  1. class Cell:
  2. x : int = None
  3. y : int = None
  4. size : int = None
  5. color : str = None
  6.  
  7. def __init__(self, x, y, size, color):
  8. self.x = x
  9. self.y = y
  10. self.size = size
  11. self.color = color
  12.  
  13. class Map:
  14. height : int = None
  15. width : int = None
  16. get: list = None
  17.  
  18. def __init__(self, height, width, size, color='white'):
  19. self.height = height
  20. self.width = width
  21. self.get = [[] for i in range(height)]
  22. for y in range(height):
  23. for x in range(width):
  24. self.get[y].append(Cell(x, y, size, color))
  25.  
  26. def directionFromAngle(angle): # Function returns (x, y)
  27. if angle == 0:
  28. return (0, 1)
  29. elif angle == 90:
  30. return (1, 0)
  31. elif angle == 180:
  32. return (0, -1)
  33. elif angle == 270:
  34. return (-1, 0)
  35. else:
  36. return None
  37.  
  38.  
  39. class Ant(Cell):
  40. direction : tuple = None
  41. angle : int = None
  42. field : Map = None
  43. left_color : str = None
  44. right_color : str = None
  45.  
  46. def __init__(self, x, y, size, angle, field, color='green', left_color='black', right_color='white'):
  47. Cell.__init__(self, x, y, size, right_color)
  48. self.angle = angle
  49. self.direction = directionFromAngle(self.angle)
  50. self.field = field
  51. self.left_color = left_color
  52. self.right_color = right_color
  53.  
  54. def next(self):
  55. if self.field.get[self.y][self.x].color == self.right_color:
  56. self.angle = 0 if self.angle + 90 == 360 else self.angle + 90 # Because angle must be 0 not 0
  57. self.field.get[self.y][self.x].color = self.left_color
  58. elif self.field.get[self.y][self.x].color == self.left_color:
  59. self.angle = 0 if self.angle - 90 == 360 else self.angle - 90
  60. self.field.get[self.y][self.x].color = self.right_color
  61.  
  62. self.x += self.direction[0]
  63. self.y += self.direction[1]
  64. self.direction = directionFromAngle(self.angle)
  65.  
Success #stdin #stdout 0.02s 9088KB
stdin
Standard input is empty
stdout
Standard output is empty