run.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import sys
  2. import collections
  3. from sandbox.life_game.simulation import CellBornBehaviour, CellDieBehaviour, Cell, Empty
  4. from sandbox.life_game.utils import get_subjects_from_str_representation
  5. from synergine2.core import Core
  6. from synergine2.cycle import CycleManager
  7. from synergine2.simulation import Simulation
  8. from synergine2.terminals import Terminal
  9. from synergine2.terminals import TerminalPackage
  10. from synergine2.terminals import TerminalManager
  11. from synergine2.xyz_utils import get_str_representation_from_positions
  12. class SimplePrintTerminal(Terminal):
  13. def __init__(self):
  14. super().__init__()
  15. self.subjects = None
  16. def receive(self, value):
  17. self.update_with_package(value)
  18. self.print_str_representation()
  19. def update_with_package(self, package: TerminalPackage):
  20. self.subjects = package.subjects if package.subjects else self.subjects
  21. for subject_id, actions in package.actions.items():
  22. for action, value in actions.items():
  23. if action == CellBornBehaviour:
  24. # Remove Empty subject
  25. self.subjects = [s for s in self.subjects[:] if s.id != subject_id]
  26. # Add born subject
  27. self.subjects.append(value)
  28. if action == CellDieBehaviour:
  29. # Remove Cell subject
  30. self.subjects = [s for s in self.subjects[:] if s.id != subject_id]
  31. # Add Empty subject
  32. self.subjects.append(value)
  33. def print_str_representation(self):
  34. items_positions = collections.defaultdict(list)
  35. for subject in self.subjects:
  36. if type(subject) == Cell:
  37. items_positions['1'].append(subject.position)
  38. if type(subject) == Empty:
  39. items_positions['0'].append(subject.position)
  40. print(get_str_representation_from_positions(
  41. items_positions,
  42. separator=' ',
  43. #force_items_as=(('0', ' '),),
  44. ))
  45. print()
  46. def main():
  47. start_str_representation = """
  48. 0 0 0 0 0 0 0 0 0 0 0
  49. 0 0 0 1 1 1 1 0 0 0 0
  50. 0 0 0 1 0 0 1 0 0 0 0
  51. 0 1 1 1 0 0 1 1 1 0 0
  52. 0 1 0 0 0 0 0 0 1 0 0
  53. 0 1 0 0 0 0 0 0 1 0 0
  54. 0 1 1 1 0 0 1 1 1 0 0
  55. 0 0 0 1 0 0 1 0 0 0 0
  56. 0 0 0 1 1 1 1 0 0 0 0
  57. 0 0 0 0 0 0 0 0 0 0 0
  58. 0 0 0 0 0 0 0 0 0 0 0
  59. """
  60. simulation = Simulation()
  61. subjects = get_subjects_from_str_representation(
  62. start_str_representation,
  63. simulation,
  64. )
  65. simulation.subjects = subjects
  66. core = Core(
  67. simulation=simulation,
  68. cycle_manager=CycleManager(subjects=subjects),
  69. terminal_manager=TerminalManager([SimplePrintTerminal()]),
  70. )
  71. core.run()
  72. if __name__ == '__main__':
  73. main()