main.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import random
  2. import json
  3. # from ant import Ant
  4. # from gatherer import Gatherer
  5. from simulation.logic.fsm_ant import FSMAnt
  6. from simulation.board import Board
  7. class Blob_Manager:
  8. DROP_VALUE = 25
  9. def __init__(self, board, max_scouters):
  10. """
  11. :type board: Board
  12. :type max_scouters: int
  13. """
  14. self.board = board
  15. self.knowledge = dict()
  16. self.knowledge['food'] = []
  17. self.knowledge['max_scouters'] = max_scouters
  18. self.scouters = []
  19. for _ in range(max_scouters):
  20. self.add_scouter()
  21. def save(self):
  22. return json.dumps(self.knowledge)
  23. def load(self, filename):
  24. with open(filename, 'r') as file:
  25. line = file.readline()
  26. json_acceptable_string = line.replace("'", "\"")
  27. k = json.loads(json_acceptable_string)
  28. self.knowledge['food'] = [tuple(x) for x in k['food']]
  29. self.knowledge['max_scouters'] = k['max_scouters']
  30. while len(self.scouters) < self.knowledge['max_scouters']:
  31. self.add_scouter()
  32. def move(self):
  33. deads = []
  34. for scouter in self.scouters:
  35. old = (scouter.x, scouter.y)
  36. scouter.move()
  37. if old == (scouter.x, scouter.y):
  38. deads.append(scouter)
  39. else:
  40. scouter.update()
  41. if self.board.has_food(scouter.x, scouter.y) and (scouter.x, scouter.y) not in self.knowledge['food']:
  42. self.food_discovered(scouter.x, scouter.y)
  43. for dead in deads:
  44. self.scouters.remove(dead)
  45. self.add_scouter()
  46. def add_scouter(self):
  47. if len(self.scouters) < self.knowledge['max_scouters']:
  48. if len(self.knowledge['food']) != 0:
  49. index = random.randrange(len(self.knowledge['food']))
  50. (x, y) = self.knowledge['food'][index]
  51. else:
  52. print("This will be nice in the future")
  53. x = 0
  54. y = 0
  55. self.scouters.append(FSMAnt(self.board, self.knowledge, x, y, Blob_Manager.DROP_VALUE))
  56. else:
  57. print("Max scouters already reached !")
  58. def reset(self, x, y):
  59. for scouter in self.scouters.copy():
  60. if scouter.x == x and scouter.y == y:
  61. self.scouters.remove(scouter)
  62. for food in self.knowledge['food'].copy():
  63. if food == (x, y):
  64. self.knowledge['food'].remove(food)
  65. self.knowledge['max_scouters'] -= 1
  66. def food_discovered(self, x, y):
  67. self.knowledge['food'].append((x, y))
  68. self.knowledge['max_scouters'] += 1
  69. for _ in range(1):
  70. self.scouters.append(FSMAnt(self.board, self.knowledge, x, y, Blob_Manager.DROP_VALUE))
  71. print("Food discovered in (" + str(x) + ", " + str(y) + ") - Total scouters : " + str(len(self.scouters)))