run.py 3.3KB

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