-
Notifications
You must be signed in to change notification settings - Fork 0
/
world_growth.py
229 lines (181 loc) · 6.82 KB
/
world_growth.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# pylint: disable=invalid-name
from random import randint, choice
class Character:
def __init__(self, directions):
self.directions = directions
def get_directions(self):
return self.directions
def create_characters(
characters_amount,
character_directions_amount,
directions=None,
):
directions = directions or [
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
(-1, -1),
(1, 1),
(-1, 1),
(1, -1),
]
characters = []
for _ in range(characters_amount):
character_directions = []
for _ in range(character_directions_amount):
random_direction = randint(0, len(directions) - 1)
character_directions.append(directions[random_direction])
characters.append(Character(character_directions))
return characters
def is_inside_world(world, x, y):
if x < 0 or y < 0:
return False
world_size_x, world_size_y = world.size
if (x > world_size_x - 1) or (y > world_size_y - 1):
return False
return True
def get_neighbors_count(world, world_object, x, y):
neighbors_count = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, 1), (1, -1)]
for direction_x, direction_y in directions:
neighbor_x = x + direction_x
neighbor_y = y + direction_y
if is_inside_world(world, neighbor_x, neighbor_y) and isinstance(
world.world_objects[neighbor_x][neighbor_y], world_object
):
neighbors_count += 1
return neighbors_count
def throw_coin():
if randint(0, 1) == 1:
return True
return False
def get_grow_directions(world, directions, seed_x, seed_y, field_type):
grow_directions = []
for direction_x, direction_y in directions:
grow_x = seed_x + direction_x
grow_y = seed_y + direction_y
if is_inside_world(world, grow_x, grow_y) and isinstance(
world.world_objects[grow_x][grow_y], field_type
):
grow_directions.append((grow_x, grow_y))
return grow_directions
def plant_seeds(world, seed_type, seeds_amount, field_type):
planted_seeds = []
for _ in range(seeds_amount):
world_size_x, world_size_y = world.size
random_pos_x = randint(1, world_size_x - 2)
random_pos_y = randint(1, world_size_y - 2)
while (
not isinstance(world.world_objects[random_pos_x][random_pos_y], field_type)
or (random_pos_x, random_pos_y) in planted_seeds
):
random_pos_x = randint(1, world_size_x - 2)
random_pos_y = randint(1, world_size_y - 2)
world.set_cell(seed_type, random_pos_x, random_pos_y)
planted_seeds.append((random_pos_x, random_pos_y))
def is_adjacent_to(world, x, y, adjacent_types):
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, 1), (1, -1)]
for adjacent_type in adjacent_types:
is_adjacent_to_type = False
for direction_x, direction_y in directions:
neighbor_x = x + direction_x
neighbor_y = y + direction_y
if is_inside_world(world, neighbor_x, neighbor_y) and isinstance(
world.world_objects[neighbor_x][neighbor_y], adjacent_type
):
is_adjacent_to_type = True
break
if not is_adjacent_to_type:
return False
return True
def is_adjacent_only_to(world, x, y, adjacent_only_to_type):
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, 1), (1, -1)]
for direction_x, direction_y in directions:
neighbor_x = x + direction_x
neighbor_y = y + direction_y
if is_inside_world(world, neighbor_x, neighbor_y) and not isinstance(
world.world_objects[neighbor_x][neighbor_y], adjacent_only_to_type
):
return False
return True
def plant_seeds_among(world, seed_type, seeds_amount, field_type, among_type):
for _ in range(seeds_amount):
fields = world.get_world_objects(field_type)
if not fields:
return
field = choice(fields)
cell_x = field[0]
cell_y = field[1]
max_tries = 120
tries_count = 0
while not is_adjacent_only_to(world, cell_x, cell_y, among_type):
field = choice(fields)
cell_x = field[0]
cell_y = field[1]
tries_count += 1
if tries_count > max_tries:
break
if tries_count > max_tries:
break
world.set_cell(seed_type, cell_x, cell_y)
def plant_seeds_where(
world,
seed_type,
seeds_amount,
field_type,
adjacent_types=None,
not_adjacent_types=None,
):
adjacent_types = adjacent_types or []
not_adjacent_types = not_adjacent_types or []
for _ in range(seeds_amount):
fields = world.get_world_objects(field_type)
if not fields:
return
field = choice(fields)
cell_x = field[0]
cell_y = field[1]
max_tries = 120
tries_count = 0
while (
adjacent_types and not is_adjacent_to(world, cell_x, cell_y, adjacent_types)
) or (
not_adjacent_types
and is_adjacent_to(world, cell_x, cell_y, not_adjacent_types)
):
field = choice(fields)
cell_x = field[0]
cell_y = field[1]
tries_count += 1
if tries_count > max_tries:
break
if tries_count > max_tries:
break
world.set_cell(seed_type, cell_x, cell_y)
def grow_seeds(world, characters, seed_type, field_type, grow_rate):
for _ in range(grow_rate):
for x, y in world.get_world_objects(seed_type):
rand_character = randint(0, len(characters) - 1)
grow_directions = get_grow_directions(
world, characters[rand_character].get_directions(), x, y, field_type
)
if not grow_directions:
continue
rand_direction_index = randint(0, len(grow_directions) - 1)
grow_x, grow_y = grow_directions[rand_direction_index]
world.set_cell(seed_type, grow_x, grow_y)
def grow_seeds_random(world, characters, seed_type, field_type, grow_rate):
for _ in range(grow_rate):
for x, y in world.get_world_objects(seed_type):
if throw_coin():
continue
rand_character = randint(0, len(characters) - 1)
grow_directions = get_grow_directions(
world, characters[rand_character].get_directions(), x, y, field_type
)
if not grow_directions:
continue
rand_direction_index = randint(0, len(grow_directions) - 1)
grow_x, grow_y = grow_directions[rand_direction_index]
world.set_cell(seed_type, grow_x, grow_y)