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

Class Entity

cellular_automata/wa_tor.py:38–91  ·  view source on GitHub ↗

Represents an entity (either prey or predator). >>> e = Entity(True, coords=(0, 0)) >>> e.prey True >>> e.coords (0, 0) >>> e.alive True

Source from the content-addressed store, hash-verified

36
37
38class Entity:
39 """
40 Represents an entity (either prey or predator).
41
42 >>> e = Entity(True, coords=(0, 0))
43 >>> e.prey
44 True
45 >>> e.coords
46 (0, 0)
47 >>> e.alive
48 True
49 """
50
51 def __init__(self, prey: bool, coords: tuple[int, int]) -> None:
52 self.prey = prey
53 # The (row, col) pos of the entity
54 self.coords = coords
55
56 self.remaining_reproduction_time = (
57 PREY_REPRODUCTION_TIME if prey else PREDATOR_REPRODUCTION_TIME
58 )
59 self.energy_value = None if prey is True else PREDATOR_INITIAL_ENERGY_VALUE
60 self.alive = True
61
62 def reset_reproduction_time(self) -> None:
63 """
64 >>> e = Entity(True, coords=(0, 0))
65 >>> e.reset_reproduction_time()
66 >>> e.remaining_reproduction_time == PREY_REPRODUCTION_TIME
67 True
68 >>> e = Entity(False, coords=(0, 0))
69 >>> e.reset_reproduction_time()
70 >>> e.remaining_reproduction_time == PREDATOR_REPRODUCTION_TIME
71 True
72 """
73 self.remaining_reproduction_time = (
74 PREY_REPRODUCTION_TIME if self.prey is True else PREDATOR_REPRODUCTION_TIME
75 )
76
77 def __repr__(self) -> str:
78 """
79 >>> Entity(prey=True, coords=(1, 1))
80 Entity(prey=True, coords=(1, 1), remaining_reproduction_time=5)
81 >>> Entity(prey=False, coords=(2, 1)) # doctest: +NORMALIZE_WHITESPACE
82 Entity(prey=False, coords=(2, 1),
83 remaining_reproduction_time=20, energy_value=15)
84 """
85 repr_ = (
86 f"Entity(prey={self.prey}, coords={self.coords}, "
87 f"remaining_reproduction_time={self.remaining_reproduction_time}"
88 )
89 if self.energy_value is not None:
90 repr_ += f", energy_value={self.energy_value}"
91 return f"{repr_})"
92
93
94class WaTor:

Callers 2

add_entityMethod · 0.85
move_and_reproduceMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected