GameOfLifeLibrary.py 4.0 KB

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