-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
507 lines (404 loc) · 18.3 KB
/
sprites.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import pygame
import os
import config
import math
class BaseSprite(pygame.sprite.Sprite):
images = dict()
def __init__(self, row, col, file_name, transparent_color=None):
pygame.sprite.Sprite.__init__(self)
if file_name in BaseSprite.images:
self.image = BaseSprite.images[file_name]
else:
self.image = pygame.image.load(os.path.join(config.IMG_FOLDER, file_name)).convert()
self.image = pygame.transform.scale(self.image, (config.TILE_SIZE, config.TILE_SIZE))
BaseSprite.images[file_name] = self.image
# making the image transparent (if needed)
if transparent_color:
self.image.set_colorkey(transparent_color)
self.rect = self.image.get_rect()
self.rect.topleft = (col * config.TILE_SIZE, row * config.TILE_SIZE)
self.row = row
self.col = col
class Agent(BaseSprite):
def __init__(self, row, col, file_name):
super(Agent, self).__init__(row, col, file_name, config.DARK_GREEN)
def move_towards(self, row, col):
row = row - self.row
col = col - self.col
self.rect.x += col
self.rect.y += row
def place_to(self, row, col):
self.row = row
self.col = col
self.rect.x = col * config.TILE_SIZE
self.rect.y = row * config.TILE_SIZE
# game_map - list of lists of elements of type Tile
# goal - (row, col)
# return value - list of elements of type Tile
def get_agent_path(self, game_map, goal):
pass
class ExampleAgent(Agent):
def __init__(self, row, col, file_name):
super().__init__(row, col, file_name)
def get_agent_path(self, game_map, goal):
path = [game_map[self.row][self.col]]
row = self.row
col = self.col
while True:
if row != goal[0]:
row = row + 1 if row < goal[0] else row - 1
elif col != goal[1]:
col = col + 1 if col < goal[1] else col - 1
else:
break
path.append(game_map[row][col])
return path
class Tile(BaseSprite):
def __init__(self, row, col, file_name):
super(Tile, self).__init__(row, col, file_name)
def __str__(self) -> str:
return "(" + str(self.row) + ", " + str(self.col) + ")"
def position(self):
return self.row, self.col
def cost(self):
pass
def kind(self):
pass
class Stone(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'stone.png')
def cost(self):
return 1000
def kind(self):
return 's'
class Water(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'water.png')
def cost(self):
return 500
def kind(self):
return 'w'
class Road(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'road.png')
def cost(self):
return 2
def kind(self):
return 'r'
class Grass(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'grass.png')
def cost(self):
return 3
def kind(self):
return 'g'
class Mud(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'mud.png')
def cost(self):
return 5
def kind(self):
return 'm'
class Dune(Tile):
def __init__(self, row, col):
super().__init__(row, col, 'dune.png')
def cost(self):
return 7
def kind(self):
return 's'
class Goal(BaseSprite):
def __init__(self, row, col):
super().__init__(row, col, 'x.png', config.DARK_GREEN)
class Trail(BaseSprite):
def __init__(self, row, col, num):
super().__init__(row, col, 'trail.png', config.DARK_GREEN)
self.num = num
def draw(self, screen):
text = config.GAME_FONT.render(f'{self.num}', True, config.WHITE)
text_rect = text.get_rect(center=self.rect.center)
screen.blit(text, text_rect)
class Aki(Agent):
def init(self, row, col, file_name):
super().__init__(row, col, file_name)
def get_agent_path(self, game_map: [[Tile]], goal):
stack = [[game_map[self.row][self.col]]]
while stack:
partial_path = stack.pop(0)
node = partial_path[-1]
if node.row == goal[0] and node.col == goal[1]:
return partial_path
children_nodes = dict()
different_costs = []
if node.row - 1 >= 0:
child_node = game_map[node.row - 1][node.col]
children_nodes['n'] = child_node
if not child_node.cost() in different_costs:
different_costs.append(child_node.cost())
if node.row + 1 < len(game_map):
child_node = game_map[node.row + 1][node.col]
children_nodes['s'] = child_node
if not child_node.cost() in different_costs:
different_costs.append(child_node.cost())
if node.col - 1 >= 0:
child_node = game_map[node.row][node.col - 1]
children_nodes['w'] = child_node
if not child_node.cost() in different_costs:
different_costs.append(child_node.cost())
if node.col + 1 < len(game_map[0]):
child_node = game_map[node.row][node.col + 1]
children_nodes['e'] = child_node
if not child_node.cost() in different_costs:
different_costs.append(child_node.cost())
different_costs.sort(key=lambda elem: -elem)
sorted_children_nodes = []
for cost in different_costs:
if 'w' in children_nodes:
west_node = children_nodes.get('w')
if west_node.cost() == cost:
sorted_children_nodes.append(west_node)
if 's' in children_nodes:
south_node = children_nodes.get('s')
if south_node.cost() == cost:
sorted_children_nodes.append(south_node)
if 'e' in children_nodes:
east_node = children_nodes.get('e')
if east_node.cost() == cost:
sorted_children_nodes.append(east_node)
if 'n' in children_nodes:
north_node = children_nodes.get('n')
if north_node.cost() == cost:
sorted_children_nodes.append(north_node)
for child in sorted_children_nodes:
if child not in partial_path:
new_partial_path = partial_path + [child]
stack.insert(0, new_partial_path)
class Jocke(Agent):
def __init__(self, row, col, file_name):
super().__init__(row, col, file_name)
def get_agent_path(self, game_map, goal):
queue = [[game_map[self.row][self.col]]]
while queue:
partial_path = queue.pop(0)
node = partial_path[-1]
if node.row == goal[0] and node.col == goal[1]:
return partial_path
children_nodes = dict()
different_costs = []
if node.row - 1 >= 0:
row = node.row - 1
col = node.col
north_node = game_map[row][col]
number_of_neighbors = 0
collective_cost = 0.0
if row - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row - 1][col]
collective_cost += neighbor_node.cost()
if col - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row][col - 1]
collective_cost += neighbor_node.cost()
if col + 1 < len(game_map[0]):
number_of_neighbors += 1
neighbor_node = game_map[row][col + 1]
collective_cost += neighbor_node.cost()
average_cost = collective_cost / number_of_neighbors
north_node_with_cost = [north_node, average_cost]
children_nodes['n'] = north_node_with_cost
if average_cost not in different_costs:
different_costs.append(average_cost)
if node.row + 1 < len(game_map):
row = node.row + 1
col = node.col
south_node = game_map[row][col]
number_of_neighbors = 0
collective_cost = 0.0
if row + 1 < len(game_map):
number_of_neighbors += 1
neighbor_node = game_map[row + 1][col]
collective_cost += neighbor_node.cost()
if col - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row][col - 1]
collective_cost += neighbor_node.cost()
if col + 1 < len(game_map[0]):
number_of_neighbors += 1
neighbor_node = game_map[row][col + 1]
collective_cost += neighbor_node.cost()
average_cost = collective_cost / number_of_neighbors
south_node_with_cost = [south_node, average_cost]
children_nodes['s'] = south_node_with_cost
if average_cost not in different_costs:
different_costs.append(average_cost)
if node.col - 1 >= 0:
row = node.row
col = node.col - 1
west_node = game_map[row][col]
number_of_neighbors = 0
collective_cost = 0.0
if row + 1 < len(game_map):
number_of_neighbors += 1
neighbor_node = game_map[row + 1][col]
collective_cost += neighbor_node.cost()
if row - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row - 1][col]
collective_cost += neighbor_node.cost()
if col - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row][col - 1]
collective_cost += neighbor_node.cost()
average_cost = collective_cost / number_of_neighbors
west_node_with_cost = [west_node, average_cost]
children_nodes['w'] = west_node_with_cost
if average_cost not in different_costs:
different_costs.append(average_cost)
if node.col + 1 < len(game_map[0]):
row = node.row
col = node.col + 1
east_node = game_map[row][col]
number_of_neighbors = 0
collective_cost = 0.0
if row + 1 < len(game_map):
number_of_neighbors += 1
neighbor_node = game_map[row + 1][col]
collective_cost += neighbor_node.cost()
if row - 1 >= 0:
number_of_neighbors += 1
neighbor_node = game_map[row - 1][col]
collective_cost += neighbor_node.cost()
if col + 1 < len(game_map[0]):
number_of_neighbors += 1
neighbor_node = game_map[row][col + 1]
collective_cost += neighbor_node.cost()
average_cost = collective_cost / number_of_neighbors
east_node_with_cost = [east_node, average_cost]
children_nodes['e'] = east_node_with_cost
if average_cost not in different_costs:
different_costs.append(average_cost)
different_costs.sort(key=lambda elem: elem)
sorted_children_nodes = []
for cost in different_costs:
if 'n' in children_nodes:
north_node = children_nodes.get('n')
if north_node[1] == cost:
sorted_children_nodes.append(north_node)
if 'e' in children_nodes:
east_node = children_nodes.get('e')
if east_node[1] == cost:
sorted_children_nodes.append(east_node)
if 's' in children_nodes:
south_node = children_nodes.get('s')
if south_node[1] == cost:
sorted_children_nodes.append(south_node)
if 'w' in children_nodes:
west_node = children_nodes.get('w')
if west_node[1] == cost:
sorted_children_nodes.append(west_node)
for child in sorted_children_nodes:
child_node = child[0]
if child_node not in partial_path:
new_partial_path = partial_path + [child_node]
queue.append(new_partial_path)
class Draza(Agent):
def __init__(self, row, col, file_name):
super().__init__(row, col, file_name)
def get_agent_path(self, game_map, goal):
start_node = game_map[self.row][self.col]
stack = [[[start_node], start_node.cost()]]
while stack:
partial_path_with_cost = stack.pop(0)
partial_path_cost = partial_path_with_cost[1]
partial_path = partial_path_with_cost[0]
node = partial_path[-1]
if node.row == goal[0] and node.col == goal[1]:
return partial_path
children_nodes = []
if node.row - 1 >= 0:
child_node = game_map[node.row - 1][node.col]
child_node_with_cost = [child_node, child_node.cost()]
children_nodes.append(child_node_with_cost)
if node.row + 1 < len(game_map):
child_node = game_map[node.row + 1][node.col]
child_node_with_cost = [child_node, child_node.cost()]
children_nodes.append(child_node_with_cost)
if node.col - 1 >= 0:
child_node = game_map[node.row][node.col - 1]
child_node_with_cost = [child_node, child_node.cost()]
children_nodes.append(child_node_with_cost)
if node.col + 1 < len(game_map[0]):
child_node = game_map[node.row][node.col + 1]
child_node_with_cost = [child_node, child_node.cost()]
children_nodes.append(child_node_with_cost)
for child_node_with_cost in children_nodes:
child_node = child_node_with_cost[0]
if child_node not in partial_path:
new_partial_path = partial_path + [child_node]
new_partial_path_cost = partial_path_cost + child_node_with_cost[1]
new_partial_path_with_cost = [new_partial_path, new_partial_path_cost]
stack.insert(0, new_partial_path_with_cost)
stack.sort(key=lambda elem: elem[1])
minimum_cost = stack[0][1]
paths_with_minimum_cost = []
for path in stack:
if path[1] == minimum_cost:
paths_with_minimum_cost.append(stack.pop(0))
else:
break
paths_with_minimum_cost.sort(key=lambda elem: len(elem[0]))
stack = paths_with_minimum_cost + stack
class Bole(Agent):
def __init__(self, row, col, file_name):
super().__init__(row, col, file_name)
def get_agent_path(self, game_map, goal):
start_node = game_map[self.row][self.col]
stack = [[[start_node], start_node.cost()]]
while stack:
partial_path_with_cost = stack.pop(0)
partial_path_cost = partial_path_with_cost[1]
partial_path = partial_path_with_cost[0]
node = partial_path[-1]
if node.row == goal[0] and node.col == goal[1]:
return partial_path
children_nodes = []
if node.row - 1 >= 0:
child_node = game_map[node.row - 1][node.col]
heuristic_cost = math.sqrt(math.pow(goal[0] - child_node.row, 2) + math.pow(goal[1] - child_node.col, 2))
cost = child_node.cost() + heuristic_cost
child_node_with_cost = [child_node, cost]
children_nodes.append(child_node_with_cost)
if node.row + 1 < len(game_map):
child_node = game_map[node.row + 1][node.col]
heuristic_cost = math.sqrt(math.pow(goal[0] - child_node.row, 2) + math.pow(goal[1] - child_node.col, 2))
cost = child_node.cost() + heuristic_cost
child_node_with_cost = [child_node, cost]
children_nodes.append(child_node_with_cost)
if node.col - 1 >= 0:
child_node = game_map[node.row][node.col - 1]
heuristic_cost = math.sqrt(math.pow(goal[0] - child_node.row, 2) + math.pow(goal[1] - child_node.col, 2))
cost = child_node.cost() + heuristic_cost
child_node_with_cost = [child_node, cost]
children_nodes.append(child_node_with_cost)
if node.col + 1 < len(game_map[0]):
child_node = game_map[node.row][node.col + 1]
heuristic_cost = math.sqrt(math.pow(goal[0] - child_node.row, 2) + math.pow(goal[1] - child_node.col, 2))
cost = child_node.cost() + heuristic_cost
child_node_with_cost = [child_node, cost]
children_nodes.append(child_node_with_cost)
for child_node_with_cost in children_nodes:
child_node = child_node_with_cost[0]
if child_node not in partial_path:
new_partial_path = partial_path + [child_node]
new_partial_path_cost = partial_path_cost + child_node_with_cost[1]
new_partial_path_with_cost = [new_partial_path, new_partial_path_cost]
stack.insert(0, new_partial_path_with_cost)
stack.sort(key=lambda elem: elem[1])
minimum_cost = stack[0][1]
paths_with_minimum_cost = []
for path in stack:
if path[1] == minimum_cost:
paths_with_minimum_cost.append(stack.pop(0))
else:
break
paths_with_minimum_cost.sort(key=lambda elem: len(elem[0]))
stack = paths_with_minimum_cost + stack