-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
433 lines (377 loc) · 18.2 KB
/
ai.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
# Students:
# Bárbara Nóbrega Galiza - 105937
# Pedro Daniel Fidalgo de Pinho - 109986
"""
Project evaluated in :
- Performance
- Quality of Architecture and Implementation
- Originality
The work aims in using three main chapters of AI
- Agent Architectures
- Search Techniques
- Automated Problem Solving
Points are attributed based on :
- Type
- Position
- Kill weapon
- Time
# Important:
Killing with rocks provides more points
Killing Fygars provide more points if killed horrizontally
"""
from tree_search import *
from consts import *
import math
import random
from path_to_enemies import *
SIZE_X = 48
SIZE_Y = 24
# Returns key
def get_key(map, digdug, enemy, target_neighbors, dug_dir, rocks, step, num_enemies):
if can_shoot(digdug, enemy, target_neighbors, map, dug_dir):
return "A"
else:
map_path = PathFinder(map, rocks, math.dist(digdug, enemy.get("pos")), step)
p = SearchProblem(map_path, digdug, enemy.get("pos"))
t = SearchTree(p)
path = t.search()
if path is None or []: # check for empty list to avoid index out of range bug when colides but still calculated path
return " "
x_dug = digdug[0]
y_dug = digdug[1]
next_x = path[1][0]
next_y = path[1][1]
key = decide_move(x_dug, y_dug, next_x, next_y, enemy, target_neighbors, rocks, step, num_enemies)
return key
# Compares enemy positions and returns closest enemy to dug
def enemy_prio(dug, enemies):
closest_dist = None
target_enemy = None
for e in enemies:
if closest_dist == None:
closest_dist = math.dist(dug, e.get("pos"))
target_enemy = e
elif closest_dist > math.dist(dug, e.get("pos")):
closest_dist = math.dist(dug, e.get("pos"))
target_enemy = e
# Get all enemies within 5 blocks of target enemy
target_neighbors = [e for e in enemies if e != target_enemy and (math.dist(e.get("pos"), target_enemy.get("pos")) <= 5)]
return target_enemy, target_neighbors
# Get dug dir based on key
def update_digdug_dir(key):
if key == "w":
return Direction.NORTH
if key == "d":
return Direction.EAST
if key == "s":
return Direction.SOUTH
if key == "a":
return Direction.WEST
return None
# Update map state based on key
def update_map(key, map, digdug):
if key == "a":
map[digdug[0] - 1][digdug[1]] = 0
return
if key == "d":
map[digdug[0] + 1][digdug[1]] = 0
return
if key == "w":
map[digdug[0]][digdug[1] - 1] = 0
return
if key == "s":
map[digdug[0]][digdug[1] + 1] = 0
return
def decide_move(x_dug, y_dug, next_x, next_y, enemy, target_neighbors, rocks, step, num_enemies):
key = " "
options = []
if next_x - x_dug == 0 and next_y - y_dug == -1:
death, options = move_kills_us(next_x, next_y, enemy, target_neighbors, "w", rocks)
if death:
key = options[random.randint(0, len(options) - 1)]
return key
else:
return "w"
elif next_x - x_dug == 0 and next_y - y_dug == 1:
death, options = move_kills_us(next_x, next_y, enemy, target_neighbors, "s", rocks)
if death:
key = options[random.randint(0, len(options) - 1)]
return key
else:
return "s"
elif next_x - x_dug == 1 and next_y - y_dug == 0:
if enemy.get("name") == "Fygar" and (step >= 1000 or (num_enemies == 1 and step >= 700)): # stuck with smart fygar or fygar trapped with rock
return "d"
death, options = move_kills_us(next_x, next_y, enemy, target_neighbors, "d", rocks)
if death:
key = options[random.randint(0, len(options) - 1)]
return key
else:
return "d"
elif next_x - x_dug == -1 and next_y - y_dug == 0:
if enemy.get("name") == "Fygar" and (step >= 1000 or (num_enemies == 1 and step >= 700)): # stuck with smart fygar or fygar trapped with rock
return "a"
death, options = move_kills_us(next_x, next_y, enemy, target_neighbors, "a", rocks)
if death:
key = options[random.randint(0, len(options) - 1)]
return key
else:
return "a"
return key
# Checks if our next move would kill us
def move_kills_us(next_x, next_y, target_enemy, target_neighbors, key, rocks):
dangerous_enemies = []
fygars_list = []
target_neighbors.append(target_enemy)
for each in target_neighbors:
if math.dist([next_x, next_y], each.get("pos")) <= 5: # if distance from digdug to enemy is <= 5, its a dangerous enemy (danger zone)
dangerous_enemies.append(each)
if each.get("name") == "Fygar":
fygars_list.append(each) # save fygars to check if we are in their fire range
return check_around(dangerous_enemies, next_x, next_y, key, rocks, fygars_list)
def check_around(dangerous_enemies, next_x, next_y, key, rocks, fygars_list):
death = False
options = []
fire_pos_list = []
for f in fygars_list:
en_x = f.get("pos")[0]
en_y = f.get("pos")[1]
fire_pos_list = [[x, en_y] for x in range(en_x - 4, en_x + 4) if x>= 0 and x<=47] # 4 because fygar can move and shoot immediately
if [next_x, next_y] in fire_pos_list:
death = True
for enemy in dangerous_enemies:
en_x = enemy.get("pos")[0]
en_y = enemy.get("pos")[1]
en_dir = enemy.get("dir")
en_next_x , en_next_y = enemy_next_pos(en_x, en_y, en_dir)
############## KEY = 'd' #################
if key == "d":
# check behind
if ((next_x - 2 == en_x and next_y == en_y) or # behind 1 house, same y
(next_x - 2 == en_next_x and next_y == en_y) or # behind 2 houses, same y
(next_x - 2 == en_x and next_y - 1 == en_y) or # behind 1 house, 1 above
(next_x - 2 == en_x and next_y + 1 == en_y) or # behind 1 house, 1 below
[next_x - 2, next_y] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x - 2, next_y)):
options.append("a")
# check above
if ((next_x - 1 == en_x and next_y - 1 == en_y) or # above 1 house, same x
(next_x - 1 == en_next_x and next_y - 1 == en_y) or # above 2 houses, same x
(next_x - 2 == en_x and next_y - 1 == en_y) or # above 1 house, 1 house behind
(next_x == en_x and next_y - 1 == en_y) or # above 1 house, 1 house in front
[next_x - 1, next_y - 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x - 1, next_y - 1)):
options.append("w")
# check below
if ((next_x - 1 == en_x and next_y + 1 == en_y) or # below 1 house, same x
(next_x - 1 == en_next_x and next_y + 1 == en_y) or # below 2 houses, same x
(next_x - 2 == en_x and next_y + 1 == en_y) or # below 1 house, 1 house behind
(next_x == en_x and next_y + 1 == en_y) or # below 1 house, 1 house in front
[next_x - 1, next_y + 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x - 1, next_y + 1)):
options.append("s")
# Check for direct death
if ((next_x == en_x and next_y == en_y) or
(next_x == en_next_x and next_y == en_y) or
(next_x + 1 == en_x and next_y == en_y) or
([next_x, next_y] in fire_pos_list) or
(rock_or_out_of_bounds(rocks, next_x, next_y))):
death = True
############## KEY = 'a' #################
elif key == "a":
# check above
if ((next_x + 1 == en_x and next_y - 1 == en_y) or # above 1 house, same x
(next_x + 1 == en_next_x and next_y - 2 == en_y) or # above 2 houses, same x
(next_x + 2 == en_x and next_y - 1 == en_y) or # above 1 house, 1 house behind
(next_x == en_x and next_y - 1 == en_y) or # above 1 house, 1 house in front
[next_x + 1, next_y - 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x + 1, next_y - 1)):
options.append("w")
# check below
if ((next_x + 1 == en_x and next_y + 1 == en_y) or # below 1 house, same x
(next_x + 1 == en_next_x and next_y + 2 == en_y) or # below 2 houses, same x
(next_x + 2 == en_x and next_y + 1 == en_y) or # below 1 house, 1 house behind
(next_x == en_x and next_y + 1 == en_y) or # below 1 house, 1 house in front
[next_x + 1, next_y + 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x + 1, next_y + 1)):
options.append("s")
# check behind
if ((next_x + 2 == en_x and next_y == en_y) or # behind 1 house, same y
(next_x + 2 == en_next_x and next_y == en_y) or # behind 2 houses, same y
(next_x + 2 == en_x and next_y - 1 == en_y) or # behind 1 house, 1 above
(next_x + 2 == en_x and next_y + 1 == en_y) or # behind 1 house, 1 below
[next_x + 2, next_y] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x + 2, next_y)):
options.append("d")
# Check for direct death
if ((next_x == en_x and next_y == en_y) or
(next_x == en_next_x and next_y == en_y) or
(next_x - 1 == en_x and next_y == en_y) or
([next_x, next_y] in fire_pos_list) or
(rock_or_out_of_bounds(rocks, next_x, next_y))):
death = True
############## KEY = 'w' #################
elif key == "w":
# check behind
if ((next_x == en_x and next_y + 2 == en_y) or # below 1 house, same x
(next_x == en_x and next_y + 2 == en_next_y) or # below 2 houses, same x
(next_x - 1 == en_x and next_y + 2 == en_y) or # below 1 house, 1 house behind
(next_x + 1 == en_x and next_y + 2 == en_y) or # below 1 house, 1 house in front
[next_x, next_y + 2] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x, next_y + 2)):
options.append("s")
# check right
if ((next_x + 1 == en_x and next_y + 1 == en_y) or # right 1 house, same x
(next_x + 1 == en_next_x and next_y + 1 == en_y) or # right 2 houses, same x
(next_x + 1 == en_x and next_y == en_y) or # right 1 house, 1 above
(next_x + 1 == en_x and next_y + 2 == en_y) or # right 1 house, 1 below
[next_x + 1, next_y + 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x + 1, next_y + 1)):
options.append("d")
# check left
if ((next_x - 1 == en_x and next_y + 1 == en_y) or # left 1 house, same x
(next_x - 1 == en_next_x and next_y + 1 == en_y) or # left 2 houses, same x
(next_x - 1 == en_x and next_y == en_y) or # left 1 house, 1 above
(next_x - 1 == en_x and next_y + 2 == en_y) or # left 1 house, 1 below
[next_x - 1, next_y + 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x - 1, next_y + 1)):
options.append("a")
# Check for direct death
if ((next_x == en_x and next_y == en_y) or
(next_x == en_x and next_y == en_next_y) or
(next_x == en_x and next_y - 1 == en_y) or
([next_x, next_y] in fire_pos_list) or
(rock_or_out_of_bounds(rocks, next_x, next_y))):
death = True
############## KEY = 's' #################
elif key == "s":
# check behind
if ((next_x == en_x and next_y - 2 == en_y) or # above 1 house, same x
(next_x == en_x and next_y - 2 == en_next_y) or # above 2 houses, same x
(next_x - 1 == en_x and next_y - 2 == en_y) or # above 1 house, 1 house behind
(next_x + 1 == en_x and next_y - 2 == en_y) or # above 1 house, 1 house in front
[next_x, next_y - 2] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x, next_y - 2)):
options.append("w")
# check left
if ((next_x - 1 == en_x and next_y - 1 == en_y) or # left 1 house, same x
(next_x - 1 == en_next_x and next_y - 1 == en_y) or # left 2 houses, same x
(next_x - 1 == en_x and next_y - 2 == en_y) or # left 1 house, 1 above
(next_x - 1 == en_x and next_y == en_y) or # left 1 house, 1 below
[next_x - 1, next_y - 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x - 1, next_y - 1)):
options.append("a")
# check right
if ((next_x + 1 == en_x and next_y - 1 == en_y) or # right 1 house, same x
(next_x + 1 == en_next_x and next_y - 1 == en_y) or # right 2 houses, same x
(next_x + 1 == en_x and next_y - 2 == en_y) or # right 1 house, 1 above
(next_x + 1 == en_x and next_y == en_y) or # right 1 house, 1 below
[next_x + 1, next_y - 1] in fire_pos_list):
death = True
else:
if not (rock_or_out_of_bounds(rocks, next_x + 1, next_y - 1)):
options.append("d")
# Check for direct death
if ((next_x == en_x and next_y == en_y) or
(next_x == en_x and next_y == en_next_y) or
(next_x == en_x and next_y + 1 == en_y) or
([next_x, next_y] in fire_pos_list) or
(rock_or_out_of_bounds(rocks, next_x, next_y))):
death = True
# find which options are valid
opt_count = {i:options.count(i) for i in options}
options = []
for opt in opt_count:
if opt_count.get(opt) == len(dangerous_enemies): # option is valid for all enemies
options.append(opt)
if len(options) == 0:
options.append(" ")
return (death, options)
def rock_or_out_of_bounds(rocks, x, y):
if x >= SIZE_X or x < 0:
return True
elif y >= SIZE_Y or y < 0:
return True
for rock in rocks:
if rock.get("pos") == [x,y]:
return True
return False
def enemy_next_pos(en_x, en_y, en_dir):
next_x = en_x
next_y = en_y
if en_dir == Direction.NORTH:
next_y = en_y - 1
elif en_dir == Direction.EAST:
next_x = en_x + 1
elif en_dir == Direction.SOUTH:
next_y = en_y + 1
elif en_dir == Direction.WEST:
next_x = en_x - 1
return (next_x, next_y)
def can_shoot(digdug, enemy, target_neighbors, map, dug_dir):
en_pos = enemy.get("pos")
if shooting_kills_us(digdug[0], digdug[1], target_neighbors):
return False
# Check if map position is block or tunnel (all 4 possibilities)
if (digdug[1] + 1 < 24) and ((map[digdug[0]][digdug[1] + 1] == 1 and digdug[1] != en_pos[1])):
return False
if (digdug[0] + 1 < 48) and (map[digdug[0] + 1][digdug[1]] == 1 and digdug[0] != en_pos[0]):
return False
if (map[digdug[0] - 1][digdug[1]] == 1 and digdug[0] != en_pos[0]):
return False
if (map[digdug[0]][digdug[1] - 1] == 1 and digdug[1] != en_pos[1]):
return False
# Check if we can shoot enemy within 3 spaces
for i in range(3, 0, -1):
if ((digdug[0] + i == en_pos[0]) and (digdug[1] == en_pos[1]) and dug_dir == Direction.EAST):
return True
if ((digdug[0] == en_pos[0]) and (digdug[1] + i == en_pos[1]) and dug_dir == Direction.SOUTH):
return True
if ((digdug[0] - i == en_pos[0]) and (digdug[1] == en_pos[1]) and dug_dir == Direction.WEST):
return True
if ((digdug[0] == en_pos[0]) and (digdug[1] - i == en_pos[1]) and dug_dir == Direction.NORTH):
return True
return False
def shooting_kills_us(dug_x, dug_y, target_neighbors):
fire_pos_list = []
fygars = [e for e in target_neighbors if e.get("name") == "Fygar"]
for f in fygars:
en_x = f.get("pos")[0]
en_y = f.get("pos")[1]
fire_pos_list = [[x, en_y] for x in range(en_x - 4, en_x + 4) if x>= 0 and x<=47]
if [dug_x, dug_y] in fire_pos_list:
return True
for enemy in target_neighbors:
en_x = enemy.get("pos")[0]
en_y = enemy.get("pos")[1]
if en_x == dug_x and en_y == dug_y - 1:
return True
elif en_x == dug_x and en_y == dug_y + 1:
return True
elif en_x == dug_x + 1 and en_y == dug_y:
return True
elif en_x == dug_x - 1 and en_y == dug_y:
return True
return False