-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangton_ant.py
More file actions
43 lines (32 loc) · 1.65 KB
/
Copy pathlangton_ant.py
File metadata and controls
43 lines (32 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from PIL import Image
import numpy as np
from collections import deque
X_SIZE, Y_SIZE = 1024, 1024 # Set field dimension
field = np.ones((X_SIZE, Y_SIZE), np.byte) # Create white field
class Ant:
"""Create Ant who will walk"""
def __init__(self, field, pos):
self.field = field
self.x, self.y = pos
self.increments = deque([(1, 0), (0, 1), (-1, 0), (0, -1)]) # Create possible move directions
def run(self):
value = self.field[self.x, self.y] # Get color of position
self.field[self.x, self.y] = not value # Invert position
self.increments.rotate(1) if value else self.increments.rotate(-1) # Change direction of the ant
dx, dy = self.increments[0] # Select increment for move
self.x += dx # Add increment for x
self.y += dy # Add increment for y
def get_field(self) -> np.ndarray:
return self.field
ant = Ant(field, pos=[field.shape[0] // 2, field.shape[1] // 2]) # place the ant to field center
count = 0
while True:
count += 1
ant.run()
if ant.x == X_SIZE or ant.y == Y_SIZE: break # Checking the ant for reaching the border
if ant.x == 0 or ant.y == 0: break # Checking the ant for reaching the border
newfield = ant.get_field() # Getting a field modified by movement of ant
print(f"Количество черных точек: {newfield.size - np.count_nonzero(newfield)}") # display count black cells
print(f"Количество шагов муравья: {count}") # display count of ant steps
newfield[newfield == 1] = -1 # Preparing field values to create contrasting Image
Image.fromarray(newfield, mode='L').show() # Create & Show Image