run.py 4.1KB

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