forked from pedralmeida22/IA-Bomberman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.py
140 lines (101 loc) · 3.52 KB
/
path.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
from Node import *
from mapa import Map
import random
from defs2 import dist_to
def getKey(pos):
if len(pos) != 2:
return ''
if pos == [1,0]:
return 'd'
elif pos == [-1,0]:
return 'a'
elif pos == [0,1]:
return 's'
elif pos == [0,-1]:
return 'w'
else:
return ''
def goToPosition(my_pos, next_pos):
mx,my = my_pos
nx,ny = next_pos
res = [nx-mx, ny-my]
return getKey(res)
# da lista de possiveis caminhos escolhe o primeiro caminho
def choose_random_move(ways):
if len(ways) != []:
return random.choice(ways)
def choose_move(my_pos, ways, goal):
if len(ways) == 0:
return ''
mx, my = my_pos
custo_min = []
if 'a' in ways:
custo_min.append(('a', dist_to([mx-1, my], goal)))
if 's' in ways:
custo_min.append(('s', dist_to([mx, my+1], goal)))
if 'd' in ways:
custo_min.append(('d', dist_to([mx+1, my], goal)))
if 'w' in ways:
custo_min.append(('w', dist_to([mx, my-1], goal)))
custo_min.sort(key= lambda x: x[1]) # ordenar por custo (distancia)
return custo_min[0][0]
def choose_key(mapa, ways, my_pos, positions, goal, last_pos_wanted):
# já sabe o caminho
if positions != []:
while my_pos == positions[0]:
positions.pop(0)
key = goToPosition(my_pos, positions[0])
positions.pop(0)
return key, positions
else: # pesquisar caminho
positions = astar(mapa.map, my_pos, goal, mapa, last_pos_wanted)
if positions == [] or positions == None:
return choose_move(my_pos, ways, goal), []
if len(positions) > 1:
positions.pop(0)
return goToPosition(my_pos, positions[0]),positions
# retorna a key para um inimigo ou '' caso nao encontre
def pathToEnemy(mapa, my_pos, enemy_pos):
# procura caminho para inimigo
if enemy_pos is not None:
# procura caminho para o inimigo
positions = astar(mapa.map, my_pos, enemy_pos, mapa, False)
if positions != [] and positions is not None:
if len(positions) == 1:
return 'B'
# se a posiçao seguinte for igual à posiçao atual
# tira essa posição da lista
while dist_to(my_pos, positions[0]) == 0:
positions.pop(0)
return goToPosition(my_pos, positions[0])
# nao encontrou caminho
return ''
# pesquisa caminho para objetivo
def findPath(mapa, my_pos, goal, exact_pos):
positions = astar(mapa.map, my_pos, goal, mapa, exact_pos)
# nao encontra caminho para o objetivo
if positions == [] or positions == None:
return []
# se a proxima posiçao for igual à minha posiçao atual
while dist_to(my_pos, positions[0]) == 0:
positions.pop(0)
return positions
# retorna a key para uma parede
def keyPath(my_pos, positions):
# nao precisa de pesquisar caminho
if positions != []:
key = goToPosition(my_pos, positions[0])
goal = positions[-1]
positions.pop(0)
return key, positions, goal
def goTo(mapa, my_pos, ways, positions, goal, exact_pos):
# nao sabe o caminho -> pesquisa
if positions == []:
path = findPath(mapa, my_pos, goal, exact_pos)
# nao foi possivel encontrar caminho -> usa outra função
if path == []:
key = choose_move(my_pos, ways, goal)
return key, [], goal
positions = path
# ja tem caminho
return keyPath(my_pos, positions)