GameOfLifeLibrary.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. class GameOfLife:
  2. def __init__(self, sizex, sizey):
  3. assert sizex > 0
  4. assert sizey > 0
  5. self.sizex = sizex
  6. self.sizey = sizey
  7. self.cells = [[NoUnit(i, j) for j in range(sizey)] for i in range(sizex)]
  8. def parse_field(self, field):
  9. assert len(field) == self.sizex
  10. x = 0
  11. for line in field:
  12. y = 0
  13. assert len(line) == self.sizey
  14. for char in line:
  15. if char == 'f':
  16. self.cells[x][y] = FishUnit(x, y)
  17. if char == 's':
  18. self.cells[x][y] = ShrimpUnit(x, y)
  19. if char == 'r':
  20. self.cells[x][y] = RockUnit(x, y)
  21. y += 1
  22. x += 1
  23. def print_field(self):
  24. dict = {
  25. 'NoUnit': 'n',
  26. 'RockUnit': 'r',
  27. 'ShrimpUnit': 's',
  28. 'FishUnit': 'f'
  29. }
  30. for x in range(self.sizex):
  31. for y in range(self.sizey):
  32. print(dict[self.cells[x][y].type], sep='', end='')
  33. print()
  34. def correct_coors(self, x, y):
  35. return 0 <= x < self.sizex and 0 <= y < self.sizey
  36. def iterate(self):
  37. new_cells = [[NoUnit(i, j) for j in range(self.sizey)] for i in range(self.sizex)]
  38. neighbour_diffs = [
  39. [-1, -1],
  40. [0, -1],
  41. [1, -1],
  42. [-1, 0],
  43. [1, 0],
  44. [-1, 1],
  45. [0, 1],
  46. [1, 1]
  47. ]
  48. for x in range(self.sizex):
  49. for y in range(self.sizey):
  50. neighbours = [self.cells[x - d[0]][y - d[1]]
  51. if self.correct_coors(x - d[0], y - d[1]) else
  52. NoUnit(0, 0)
  53. for d in neighbour_diffs]
  54. # print(x, y, neighbours)
  55. if not self.cells[x][y].should_be_killed(neighbours):
  56. new_cells[x][y] = self.cells[x][y]
  57. if self.cells[x][y].can_spawn_fish(neighbours):
  58. new_cells[x][y] = FishUnit(x, y)
  59. elif self.cells[x][y].can_spawn_shrimp(neighbours):
  60. new_cells[x][y] = ShrimpUnit(x, y)
  61. self.cells = new_cells
  62. class Unit:
  63. def __init__(self, posx, posy):
  64. self.posx = posx
  65. self.posy = posy
  66. self.type = 'Unit'
  67. def should_be_killed(self, neighbours):
  68. return False
  69. def can_spawn_shrimp(self, neighbours):
  70. return False
  71. def can_spawn_fish(self, neighbours):
  72. return False
  73. def __str__(self):
  74. return self.type + '(' + str(self.posx) + ', ' + str(self.posy) + ')'
  75. def __repr__(self):
  76. return self.type + '(' + str(self.posx) + ', ' + str(self.posy) + ')'
  77. def __eq__(self, other):
  78. return self.type == other.type and self.posx == other.posx and self.posy == other.posy
  79. class NoUnit(Unit):
  80. def __init__(self, posx, posy):
  81. super().__init__(posx, posy)
  82. self.type = 'NoUnit'
  83. def can_spawn_shrimp(self, neighbours):
  84. ns_num = sum(1 if neighbour.type == 'ShrimpUnit' else 0 for neighbour in neighbours)
  85. return ns_num == 3
  86. def can_spawn_fish(self, neighbours):
  87. ns_num = sum(1 if neighbour.type == 'FishUnit' else 0 for neighbour in neighbours)
  88. return ns_num == 3
  89. class FishUnit(Unit):
  90. def __init__(self, posx, posy):
  91. super().__init__(posx, posy)
  92. self.type = 'FishUnit'
  93. def should_be_killed(self, neighbours):
  94. ns_num = sum(1 if neighbour.type == 'FishUnit' else 0 for neighbour in neighbours)
  95. return ns_num < 2 or ns_num >= 4
  96. class ShrimpUnit(Unit):
  97. def __init__(self, posx, posy):
  98. super().__init__(posx, posy)
  99. self.type = 'ShrimpUnit'
  100. def should_be_killed(self, neighbours):
  101. ns_num = sum(1 if neighbour.type == 'ShrimpUnit' else 0 for neighbour in neighbours)
  102. return ns_num < 2 or ns_num >= 4
  103. class RockUnit(Unit):
  104. def __init__(self, posx, posy):
  105. super().__init__(posx, posy)
  106. self.type = 'RockUnit'
  107. def should_be_killed(self, neighbours):
  108. return False