/
World.py
52 lines (41 loc) · 1.69 KB
/
World.py
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
44
45
46
47
48
49
50
51
52
from Ant import *
# Constraints
EMPTY_SPACE = None
class World:
def __init__(self, world_size, population, change_factor):
self.world_size = world_size
self.population = population
self.change_factor = change_factor
self.map = [[None for y in range(world_size)] for x in range(world_size)]
self.populate()
def get_count(self):
counter = [0, 0, 0]
for y in range(self.world_size):
for x in range(self.world_size):
if self.map[y][x] is not None:
counter[self.map[y][x].type] += 1
return counter
def check_classes(self):
for y in range(self.world_size):
for x in range(self.world_size):
if self.map[y][x] is not None:
self.map[y][x].check_surrounding()
def show(self, tela):
tela.fill((0, 0, 0))
for i in range(self.world_size):
for j in range(self.world_size):
if self.map[i][j] is not None:
self.map[i][j].draw(tela)
def populate(self):
for i in range(self.population):
found_unused_cell = False
while not found_unused_cell:
x, y = random.randint(0, self.world_size - 1), random.randint(0, self.world_size - 1)
if self.map[y][x] is None:
found_unused_cell = True
self.map[y][x] = Ant(pos=(x, y), world=self, ant_type=WORKER_ANT, change_factor=self.change_factor)
def move_ants(self):
for y in range(self.world_size):
for x in range(self.world_size):
if self.map[y][x] is not None:
self.map[y][x].move()