MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / move_and_reproduce

Method move_and_reproduce

cellular_automata/wa_tor.py:257–345  ·  view source on GitHub ↗

Attempts to move to an unoccupied neighbouring square in either of the four directions (North, South, East, West). If the move was successful and the `remaining_reproduction_time` is equal to 0, then a new prey or predator can also be created in the previous

(
        self, entity: Entity, direction_orders: list[Literal["N", "E", "S", "W"]]
    )

Source from the content-addressed store, hash-verified

255 ]
256
257 def move_and_reproduce(
258 self, entity: Entity, direction_orders: list[Literal["N", "E", "S", "W"]]
259 ) -> None:
260 """
261 Attempts to move to an unoccupied neighbouring square
262 in either of the four directions (North, South, East, West).
263 If the move was successful and the `remaining_reproduction_time` is
264 equal to 0, then a new prey or predator can also be created
265 in the previous square.
266
267 :param direction_orders: Ordered list (like priority queue) depicting
268 order to attempt to move. Removes any systematic
269 approach of checking neighbouring squares.
270
271 >>> planet = [
272 ... [None, None, None],
273 ... [None, Entity(True, coords=(1, 1)), None],
274 ... [None, None, None]
275 ... ]
276 >>> wt = WaTor(WIDTH, HEIGHT)
277 >>> wt.set_planet(planet)
278 >>> wt.move_and_reproduce(Entity(True, coords=(1, 1)), direction_orders=["N"])
279 >>> wt.planet # doctest: +NORMALIZE_WHITESPACE
280 [[None, Entity(prey=True, coords=(0, 1), remaining_reproduction_time=4), None],
281 [None, None, None],
282 [None, None, None]]
283 >>> wt.planet[0][0] = Entity(True, coords=(0, 0))
284 >>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
285 ... direction_orders=["N", "W", "E", "S"])
286 >>> wt.planet # doctest: +NORMALIZE_WHITESPACE
287 [[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None,
288 Entity(prey=True, coords=(0, 2), remaining_reproduction_time=4)],
289 [None, None, None],
290 [None, None, None]]
291 >>> wt.planet[0][1] = wt.planet[0][2]
292 >>> wt.planet[0][2] = None
293 >>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
294 ... direction_orders=["N", "W", "S", "E"])
295 >>> wt.planet # doctest: +NORMALIZE_WHITESPACE
296 [[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None, None],
297 [None, Entity(prey=True, coords=(1, 1), remaining_reproduction_time=4), None],
298 [None, None, None]]
299
300 >>> wt = WaTor(WIDTH, HEIGHT)
301 >>> reproducable_entity = Entity(False, coords=(0, 1))
302 >>> reproducable_entity.remaining_reproduction_time = 0
303 >>> wt.planet = [[None, reproducable_entity]]
304 >>> wt.move_and_reproduce(reproducable_entity,
305 ... direction_orders=["N", "W", "S", "E"])
306 >>> wt.planet # doctest: +NORMALIZE_WHITESPACE
307 [[Entity(prey=False, coords=(0, 0),
308 remaining_reproduction_time=20, energy_value=15),
309 Entity(prey=False, coords=(0, 1), remaining_reproduction_time=20,
310 energy_value=15)]]
311 """
312 row, col = coords = entity.coords
313
314 adjacent_squares: dict[Literal["N", "E", "S", "W"], tuple[int, int]] = {

Callers 2

perform_prey_actionsMethod · 0.95

Calls 4

get_entitiesMethod · 0.95
EntityClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected