test_life_game.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import collections
  2. from sandbox.life_game.simulation import Cell, Empty
  3. from synergine2.cycle import CycleManager
  4. from synergine2.simulation import Simulation, Subjects
  5. from synergine2.utils import initialize_subject
  6. from synergine2.xyz_utils import get_str_representation_from_positions
  7. from tests import BaseTest, str_kwargs
  8. class TestSimpleSimulation(BaseTest):
  9. def test_cycles_evolution(self):
  10. simulation = Simulation()
  11. subjects = self._get_subjects(simulation)
  12. simulation.subjects = subjects
  13. cycle_manager = CycleManager(
  14. subjects=subjects,
  15. )
  16. assert """
  17. 0 0 0 0 0
  18. 0 1 1 1 0
  19. 0 0 0 0 0
  20. """ == self._get_str_representation_of_subjects(
  21. subjects,
  22. )
  23. cycle_manager.next()
  24. assert """
  25. 0 0 1 0 0
  26. 0 0 1 0 0
  27. 0 0 1 0 0
  28. """ == self._get_str_representation_of_subjects(
  29. subjects,
  30. )
  31. cycle_manager.next()
  32. assert """
  33. 0 0 0 0 0
  34. 0 1 1 1 0
  35. 0 0 0 0 0
  36. """ == self._get_str_representation_of_subjects(
  37. subjects,
  38. )
  39. def _get_subjects(self, simulation: Simulation):
  40. cells = Subjects(simulation=simulation)
  41. for position in [
  42. (-1, 0, 0),
  43. (0, 0, 0),
  44. (1, 0, 0),
  45. ]:
  46. cells.append(Cell(
  47. simulation=simulation,
  48. position=position,
  49. ))
  50. for position in [
  51. (-2, -1, 0),
  52. (-1, -1, 0),
  53. (0, -1, 0),
  54. (1, -1, 0),
  55. (2, -1, 0),
  56. (-2, 0, 0),
  57. (2, 0, 0),
  58. (-2, 1, 0),
  59. (-1, 1, 0),
  60. (0, 1, 0),
  61. (1, 1, 0),
  62. (2, 1, 0),
  63. ]:
  64. cells.append(Empty(
  65. simulation=simulation,
  66. position=position,
  67. ))
  68. return cells
  69. def _get_str_representation_of_subjects(self, subjects: list):
  70. items_positions = collections.defaultdict(list)
  71. for subject in subjects:
  72. if type(subject) == Cell:
  73. items_positions['1'].append(subject.position)
  74. if type(subject) == Empty:
  75. items_positions['0'].append(subject.position)
  76. return get_str_representation_from_positions(
  77. items_positions,
  78. **str_kwargs
  79. )