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

Class WaTor

cellular_automata/wa_tor.py:94–478  ·  view source on GitHub ↗

Represents the main Wa-Tor algorithm. :attr time_passed: A function that is called every time time passes (a chronon) in order to visually display the new Wa-Tor planet. The `time_passed` function can block using ``time.sleep`` to slow the algorithm progression.

Source from the content-addressed store, hash-verified

92
93
94class WaTor:
95 """
96 Represents the main Wa-Tor algorithm.
97
98 :attr time_passed: A function that is called every time
99 time passes (a chronon) in order to visually display
100 the new Wa-Tor planet. The `time_passed` function can block
101 using ``time.sleep`` to slow the algorithm progression.
102
103 >>> wt = WaTor(10, 15)
104 >>> wt.width
105 10
106 >>> wt.height
107 15
108 >>> len(wt.planet)
109 15
110 >>> len(wt.planet[0])
111 10
112 >>> len(wt.get_entities()) == PREDATOR_INITIAL_COUNT + PREY_INITIAL_COUNT
113 True
114 """
115
116 time_passed: Callable[["WaTor", int], None] | None
117
118 def __init__(self, width: int, height: int) -> None:
119 self.width = width
120 self.height = height
121 self.time_passed = None
122
123 self.planet: list[list[Entity | None]] = [[None] * width for _ in range(height)]
124
125 # Populate planet with predators and prey randomly
126 for _ in range(PREY_INITIAL_COUNT):
127 self.add_entity(prey=True)
128 for _ in range(PREDATOR_INITIAL_COUNT):
129 self.add_entity(prey=False)
130 self.set_planet(self.planet)
131
132 def set_planet(self, planet: list[list[Entity | None]]) -> None:
133 """
134 Ease of access for testing
135
136 >>> wt = WaTor(WIDTH, HEIGHT)
137 >>> planet = [
138 ... [None, None, None],
139 ... [None, Entity(True, coords=(1, 1)), None]
140 ... ]
141 >>> wt.set_planet(planet)
142 >>> wt.planet == planet
143 True
144 >>> wt.width
145 3
146 >>> wt.height
147 2
148 """
149 self.planet = planet
150 self.width = len(planet[0])
151 self.height = len(planet)

Callers 1

wa_tor.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected