run.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import collections
  2. from sandbox.life_game.simulation import Cell, LotOfCellsSignalBehaviour, LifeGame, \
  3. EmptyPositionWithLotOfCellAroundEvent
  4. from sandbox.life_game.simulation import Empty
  5. from sandbox.life_game.simulation import CellDieEvent
  6. from sandbox.life_game.simulation import CellBornEvent
  7. from sandbox.life_game.utils import get_subjects_from_str_representation
  8. from synergine2.core import Core
  9. from synergine2.cycle import CycleManager
  10. from synergine2.simulation import Simulation
  11. from synergine2.simulation import Event
  12. from synergine2.terminals import Terminal
  13. from synergine2.terminals import TerminalPackage
  14. from synergine2.terminals import TerminalManager
  15. from synergine2.xyz_utils import get_str_representation_from_positions
  16. class SimplePrintTerminal(Terminal):
  17. subscribed_events = [
  18. CellDieEvent,
  19. CellBornEvent,
  20. ]
  21. def __init__(self):
  22. super().__init__()
  23. self._cycle_born_count = 0
  24. self._cycle_die_count = 0
  25. self.register_event_handler(CellDieEvent, self.record_die)
  26. self.register_event_handler(CellBornEvent, self.record_born)
  27. def record_die(self, event: Event):
  28. self._cycle_die_count += 1
  29. def record_born(self, event: Event):
  30. self._cycle_born_count += 1
  31. def receive(self, package: TerminalPackage):
  32. self._cycle_born_count = 0
  33. self._cycle_die_count = 0
  34. super().receive(package)
  35. self.print_str_representation()
  36. def print_str_representation(self):
  37. items_positions = collections.defaultdict(list)
  38. for subject in self.subjects.values():
  39. if type(subject) == Cell:
  40. items_positions['1'].append(subject.position)
  41. if type(subject) == Empty:
  42. items_positions['0'].append(subject.position)
  43. print(get_str_representation_from_positions(
  44. items_positions,
  45. separator=' ',
  46. force_items_as=(('0', ' '),),
  47. # force_positions_as=(
  48. # ((-3, -10, 0), 'V'),
  49. # ((-2, -9, 0), 'X'),
  50. # )
  51. ))
  52. # Display current cycle events
  53. print('This cycle: {0} born, {1} dead'.format(
  54. self._cycle_born_count,
  55. self._cycle_die_count,
  56. ))
  57. print()
  58. class CocosTerminal(Terminal):
  59. subscribed_events = [
  60. CellDieEvent,
  61. CellBornEvent,
  62. EmptyPositionWithLotOfCellAroundEvent,
  63. ]
  64. def __init__(self):
  65. super().__init__()
  66. self.subjects = None
  67. self.gui = None
  68. def receive(self, package: TerminalPackage):
  69. self.gui.before_received(package)
  70. super().receive(package)
  71. self.gui.after_received(package)
  72. def run(self):
  73. from sandbox.life_game import gui
  74. self.gui = gui.LifeGameGui(self)
  75. self.gui.run()
  76. def main():
  77. start_str_representation = """
  78. 0 0 0 0 0 0 0 0 0 0 0 0 0
  79. 0 0 0 1 1 1 1 0 0 0 0 0 0
  80. 0 0 0 1 0 0 1 0 0 0 0 0 0
  81. 0 1 1 1 0 0 1 1 1 0 0 0 0
  82. 0 1 0 0 0 0 0 0 1 0 0 0 0
  83. 0 1 0 0 0 0 0 0 1 0 0 0 0
  84. 0 1 1 1 0 0 1 1 1 0 0 0 0
  85. 0 0 0 1 0 0 1 0 0 0 0 0 0
  86. 0 0 0 1 1 1 1 0 0 0 0 0 0
  87. 0 0 0 0 0 0 0 0 0 0 0 0 0
  88. 0 0 0 0 0 0 0 0 0 0 0 0 0
  89. 0 0 0 0 0 0 0 0 0 0 0 0 0
  90. 0 0 0 0 0 0 0 0 0 0 0 0 0
  91. 0 0 0 0 0 0 0 0 0 0 0 0 0
  92. 0 0 0 0 0 1 1 1 0 0 0 0 0
  93. 0 0 0 0 0 0 0 1 0 0 0 0 0
  94. 0 0 0 0 0 0 1 0 0 0 0 0 0
  95. 0 0 0 0 0 0 0 0 0 0 0 0 0
  96. 0 0 0 0 0 0 0 0 0 0 0 0 0
  97. """
  98. simulation = LifeGame()
  99. subjects = get_subjects_from_str_representation(
  100. start_str_representation,
  101. simulation,
  102. )
  103. simulation.subjects = subjects
  104. core = Core(
  105. simulation=simulation,
  106. cycle_manager=CycleManager(simulation=simulation),
  107. terminal_manager=TerminalManager([CocosTerminal(), SimplePrintTerminal()]),
  108. )
  109. core.run()
  110. if __name__ == '__main__':
  111. main()