forked from pedralmeida22/IA-Bomberman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunused.py
262 lines (212 loc) · 8.91 KB
/
unused.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
from mapa import Map
from path import choose_move, choose_random_move, dist_to, goToPosition
from Node import astar
def choose_key2(mapa, ways, my_pos, positions, wall, oneal, last_pos_wanted):
# já sabe o caminho
if positions != []:
key = goToPosition(my_pos, positions[0])
positions.pop(0)
if positions:
return key,positions[-1]
else:
return key,[]
else: # pesquisar caminho
if oneal is not None:
positions = astar(mapa.map, my_pos, oneal)
print('positions enemie: ' + str(positions))
if positions == [] or positions == None:
positions = astar(mapa.map, my_pos, wall)
print('positions wall: ' + str(positions))
if positions == [] or positions == None:
print('Caminho nao encontrado...')
# return choose_move(my_pos,ways,goal)
return choose_random_move(ways),''
goal = wall
goal = oneal
else:
positions = astar(mapa.map, my_pos, wall)
print('positions wall: ' + str(positions))
if positions == [] or positions == None:
print('Caminho nao encontrado...')
# return choose_move(my_pos,ways,goal)
return '', ''
goal = wall
if len(positions)>1:
positions.pop(0)
if len(positions) <= 1 and last_pos_wanted:
return choose_move(my_pos, ways, goal),goal
return goToPosition(my_pos, positions[0]),goal
def choose_key3(mapa, ways, my_pos, positions, wall, oneal, last_pos_wanted):
# já sabe o caminho
if positions != []:
print('Não precisa de pesquisar...')
print('positions: ' + str(positions))
if dist_to(my_pos, positions[0]) > 1:
print("Next_pos invalida!!")
return choose_move(my_pos, ways, wall), [], wall
key = goToPosition(my_pos, positions[0])
goal = positions[-1]
positions.pop(0)
return key, positions, goal
'''
if positions:
return key, positions, positions[-1]
else:
return key, [], positions[-1]
'''
else: # pesquisar caminho
# procura caminho para inimigo
if oneal is not None:
# procura caminho para o inimigo
positions = astar(mapa.map, my_pos, oneal, mapa)
print('positions enemie: ' + str(positions))
# se nao encontra caminho para o inimigo
# então procura caminho para a parede
if positions == [] or positions == None:
print('Caminho nao encontrado para o inimigo...')
positions = astar(mapa.map, my_pos, wall, mapa)
print('positions wall: ' + str(positions))
# nao encontra caminho para a parede
if positions == [] or positions == None:
print('Caminho nao encontrado para a parede...')
# usa outra função para encontrar caminho
return choose_move(my_pos, ways, wall), [], wall
# caminho para parede
# se a proxima posiçao for igual à minha posiçao atual
while dist_to(my_pos, positions[0]) == 0:
print('my_pos == next_pos')
print('apagar posições inuteis')
positions.pop(0)
# positions.pop(0) # *Problemas*
return goToPosition(my_pos,positions[0]), positions, positions[-1]
else:
# caminho para inimigo
# se a proxima posiçao for igual à minha posiçao atual
while dist_to(my_pos, positions[0]) == 0:
print('my_pos == next_pos')
print('apagar posições inuteis')
positions.pop(0)
# positions.pop(0)
return goToPosition(my_pos, positions[0]), positions, positions[-1]
else: # procura caminho para parede (ja nao ha inimigos)
positions = astar(mapa.map, my_pos, wall)
print('positions wall: ' + str(positions))
if positions == [] or positions == None:
print('Caminho nao encontrado...')
# return choose_move(my_pos,ways,goal)
return choose_move(my_pos,ways,wall), [], wall
if len(positions)>1:
positions.pop(0)
if len(positions) <= 1 and last_pos_wanted:
return choose_move(my_pos, ways, wall), positions, wall
return goToPosition(my_pos, positions[0]), positions, wall
# dando uma key retorna a sua inversa
def inverse(key):
if key == 'a':
return 'd'
elif key == 'd':
return 'a'
elif key == 's':
return 'w'
elif key == 'w':
return 's'
#evita os inimigos
def avoid(my_pos,en_pos,mapa):
# if en_pos[0] == my_pos[0]:
# if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] - 1]): # Bomberman para baixo
# print("BAIXO")
# return 's'
# else:
# print("CIMA")
# return 'w'
#
# elif en_pos[1]==mu
if en_pos[0]>my_pos[0]: #Inimigo à direita
if not Map.is_blocked(mapa,[my_pos[0]-1,my_pos[1]]): #BOmberman vai à esquerda
print("ESQUERDA")
return 'a'
else: #Pedra à esquerda
if en_pos[1]>my_pos[1]: #Inimigo abaixo
if not Map.is_blocked(mapa,[my_pos[0], my_pos[1]-1]): #Bomberman para cima
print("CIMA")
return 'w'
elif en_pos[1] < my_pos[1]: # Inimigo acima
if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] + 1]): # Bomberman para baixo
print("BAIXO")
return 's'
else:
print("rip")
else: # INIMIGO NO MESMO NIVEL
if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] - 1]): # Bomberman para cima
print("CIMA")
return 'w'
else:
print("BAIXO")
return 's'
elif en_pos[0]<my_pos[0]: #Inimigo à esquerda
if not Map.is_blocked(mapa,[my_pos[0] + 1, my_pos[1]]): # BOmberman vai à direita
print("DIREITA")
return 'd'
else: # Pedra à direita
if en_pos[1] > my_pos[1]: # Inimigo abaixo
if not Map.is_blocked(mapa,[my_pos[0], my_pos[1] - 1]): # Bomberman para cima
print("CIMA")
return 'w'
else:
print("rip")
return ''
elif en_pos[1] < my_pos[1]:
if not Map.is_blocked(mapa,[my_pos[0], my_pos[1] + 1]): # Bomberman para baixo
print("BAIXO")
return 's'
else:
print("rip")
return ''
else: # Inimigo NO MESMO NIVEL
if not Map.is_blocked(mapa,[my_pos[0], my_pos[1] - 1]): # Bomberman para cima
print("CIMA")
return 'w'
else:
print("BAIXO")
return 's'
else: #INIMIGO EM LINHA
if en_pos[1] > my_pos[1]: # Inimigo acima
if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] - 1]): # Bomberman para cima
print("CIMA")
return 'w'
else:
print("rip")
return ''
elif en_pos[1] < my_pos[1]:
if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] + 1]): # Bomberman para baixo
print("BAIXO")
return 's'
else:
print("rip")
return ''
else: # Inimigo NO MESMO NIVEL
if not Map.is_blocked(mapa, [my_pos[0], my_pos[1] - 1]): # Bomberman para cima
print("CIMA")
return 'w'
else:
print("BAIXO")
return 's'
def vector2dir(vx, vy):
m = max(abs(vx), abs(vy))
if m == abs(vx):
if vx < 0:
d = 'a' # 'a'
else:
d = 'd' # 'd'
else:
if vy > 0:
d = 's' # s
else:
d = 'w' # w
return d
def goto(origem, destino):
if len(origem) != 2 or len(destino) != 2:
return ''
ox, oy = origem
dx, dy = destino
return vector2dir(dx - ox, dy - oy)