GameOfLifeLibrary.py 4.3 KB

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