Balances predators and preys so that prey can not dominate the predators, blocking up space for them to reproduce. >>> wt = WaTor(WIDTH, HEIGHT) >>> for i in range(2000): ... row, col = i // HEIGHT, i % WIDTH ... wt.planet[row][col] =
(self)
| 181 | return [entity for column in self.planet for entity in column if entity] |
| 182 | |
| 183 | def balance_predators_and_prey(self) -> None: |
| 184 | """ |
| 185 | Balances predators and preys so that prey |
| 186 | can not dominate the predators, blocking up |
| 187 | space for them to reproduce. |
| 188 | |
| 189 | >>> wt = WaTor(WIDTH, HEIGHT) |
| 190 | >>> for i in range(2000): |
| 191 | ... row, col = i // HEIGHT, i % WIDTH |
| 192 | ... wt.planet[row][col] = Entity(True, coords=(row, col)) |
| 193 | >>> entities = len(wt.get_entities()) |
| 194 | >>> wt.balance_predators_and_prey() |
| 195 | >>> len(wt.get_entities()) == entities |
| 196 | False |
| 197 | """ |
| 198 | entities = self.get_entities() |
| 199 | shuffle(entities) |
| 200 | |
| 201 | if len(entities) >= MAX_ENTITIES - MAX_ENTITIES / 10: |
| 202 | prey = [entity for entity in entities if entity.prey] |
| 203 | predators = [entity for entity in entities if not entity.prey] |
| 204 | |
| 205 | prey_count, predator_count = len(prey), len(predators) |
| 206 | |
| 207 | entities_to_purge = ( |
| 208 | prey[:DELETE_UNBALANCED_ENTITIES] |
| 209 | if prey_count > predator_count |
| 210 | else predators[:DELETE_UNBALANCED_ENTITIES] |
| 211 | ) |
| 212 | for entity in entities_to_purge: |
| 213 | self.planet[entity.coords[0]][entity.coords[1]] = None |
| 214 | |
| 215 | def get_surrounding_prey(self, entity: Entity) -> list[Entity]: |
| 216 | """ |