-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake705.py
291 lines (261 loc) · 11.5 KB
/
snake705.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
from snake import Snake
from constants import *
import math
import pygame
class MyAgent705(Snake):
def __init__(self,body=[(0,0)] , direction=(1,0), name="Jewpacabra"):
super().__init__(body,direction,name=name)
self.last = None
self.temp_len_players_positions = None
self.closedNodes = []
#self.openNodes = []
self.food_found = False
self.can_use_astar_regular = False
def pathlen(self,a,b):
distX = abs(a[0]-b[0])
distY = abs(a[1]-b[1])
if distX > 60/2:
distX = 60- distX;
if distY > 40/2:
distY = 40 - distY;
return distX + distY
def add(self,a,b):
return (a[0]+b[0])%60,(a[1]+b[1])%40
def update(self,points=None, mapsize=None, count=None,agent_time=None):
#self.points=points
self.mapsize=mapsize
self.count=count
self.agent_time=agent_time
def updateDirection(self,maze):
#print("===================================")
begin_time = pygame.time.get_ticks();
temp_len = self.temp_len_players_positions
self.temp_len_players_positions = len(maze.playerpos)
if temp_len != self.temp_len_players_positions:
self.last = None
self.closedNodes = []
#self.openNodes = []
self.food_found = False
# print("CHANGED")
olddir=self.direction
position=self.body[0]
complement=[(up,down),(down,up),(right,left),(left,right)]
invaliddir=[x for (x,y) in complement if y==olddir]
validdir=[dir for dir in directions if not ( dir in invaliddir )]
if len(self.body) > len(maze.playerpos):
validdir=[dir for dir in validdir if not (self.add(position,dir) in maze.obstacles or self.add(position,dir) in maze.playerpos)]
else:
# possible enemy positions
enemy_head = [pos for pos in maze.playerpos if pos not in self.body][0]
possible_next_enemy_position = [self.add(enemy_head, dir) for dir in directions]
validdir=[dir for dir in validdir if not (self.add(position,dir) in maze.obstacles or self.add(position,dir) in maze.playerpos or self.add(position,dir) in possible_next_enemy_position)]
olddir= olddir if olddir in validdir or len(validdir)==0 else validdir[0]
shortest=self.pathlen(self.add(position,olddir) , maze.foodpos)
# avoid if enemy is considerable closer than us
"""
if(self.pathlen(enemy_head,maze.foodpos) + 5 < shortest):
# print("AVOID")
self.direction=olddir
self.last = None
self.closedNodes = []
self.food_found = False
# print("DIR1: " + str(self.direction))
# avoid food if we are +5 larger than enemy
if len(self.body) > (len(maze.playerpos) - len(self.body) + 200):
# print("AVOID 2")
self.direction = olddir
self.last = None
self.closedNodes = []
self.food_found = False
# astar saving the path
"""
if not self.food_found:
#print("FIGHT FOR IT")
path = self.aa_improved(position, self.direction, maze, begin_time)
#print("BLA")
if path and path[-1].dir in validdir:
print("SEARCHING")
#print("--- FOLLOWED PATH DIR")
dir = path[-1].dir
else:
print("SEARCHING -> Begin")
dir = olddir
self.last = None
self.closedNodes = []
self.food_found = False
#self.openNodes = []
#print("--- FOLLOWED OLDDIR")
#print("DIR2: " + str(dir))
# self.direction = dir # if self.path está por segurança
#print("DIR2.2: " + str(self.direction))
# regular astart
else:
self.closedNodes = []
self.can_use_astar_regular = False
path = self.aa_regular(position, self.direction, maze, begin_time)
if self.can_use_astar_regular and path and path[-1].dir in validdir:
print("******************* CAN AASTAR *******************")
self.last = None
dir = path[-1].dir
else:
if not self.last:
print(" CAN AASTAR -> Begin")
self.food_found = False
path = self.aa_improved(position, self.direction, maze, begin_time)
if path and path[-1].dir in validdir:
print("FOOD FOUND, KEEP RETURNING")
#print("--- FOLLOWED PATH DIR")
dir = path[-1].dir
else:
print("RETURNING -> Begin")
dir = olddir
self.last = None
self.closedNodes = []
self.food_found = False
self.direction = dir
def aa_regular(self,startPos, startDir, maze, begin_time):
startNode=Node(startPos, dir=startDir)
targetNode=Node(maze.foodpos)
startNode.hCost = self.pathlen((startPos[0],startPos[1]),(targetNode.x,targetNode.y))
openNodes=[]
closedNodes=[]
openNodes.append(startNode)
while openNodes!=[]:
currentNode = openNodes[0]
for node in openNodes:
if node.fCost() < currentNode.fCost():
currentNode = node
if currentNode in openNodes:
openNodes.remove(currentNode)
closedNodes.append(currentNode)
if currentNode == targetNode:
print("===================AASTAR FOUND IT=====================")
self.can_use_astar_regular = True
return self.retracePath(Node(startPos),currentNode)
if pygame.time.get_ticks() - begin_time > self.agent_time - 1:
self.can_use_astar_regular = False
return self.retracePath(Node(startPos),currentNode)
for n in self.getNeighbours(currentNode, targetNode, maze):#otimizar
if n not in closedNodes and n not in openNodes:
openNodes.append(n)
def aa_improved(self,startPos, startDir, maze, begin_time):
targetNode=Node(maze.foodpos)
if self.food_found:
print("FOOD FOUND = TRUE")
# print("bla1")
# print("DEBUG 1")
return self.retracePath(Node(startPos),self.last)
if self.last:
# print("bla2")
# print("DEBUG 2")
startNode = self.last
if self.last in self.closedNodes:
self.closedNodes.remove(self.last)
else:
# print("bla3")
startNode=Node(startPos, dir=startDir)
startNode.hCost = self.pathlen((startPos[0],startPos[1]),(targetNode.x,targetNode.y))
#print("DEBUG 3")
openNodes=[]
#closedNodes=[]
openNodes.append(startNode)
#print("DEBUG 4")
while openNodes!=[]:
currentNode = openNodes[0]
# print("DEBUG 5")
for node in openNodes:
if node.fCost() < currentNode.fCost():
currentNode = node
#print("DEBUG 6")
if currentNode in openNodes:
openNodes.remove(currentNode)
self.closedNodes.append(currentNode)
# print("DEBUG 7")
if currentNode == targetNode:
self.food_found = True
# print("DEBUG 8")
return self.retracePath(Node(startPos),currentNode)
if pygame.time.get_ticks() - begin_time > self.agent_time - 1:
# print("DEBUG 9")
return self.retracePath(Node(startPos),currentNode)
# print("DEBUG 10")
for n in self.getNeighbours(currentNode, targetNode, maze):#otimizar
if n not in self.closedNodes and n not in openNodes:
openNodes.append(n)
# print("DEBUG 11")
self.last = None
self.closedNodes = []
self.food_found = False
self.openNodes = []
def retracePath(self,startNode, endNode):
path=[]
currentNode = endNode
self.last = endNode
# print("startNode: " + str(startNode))
#print("DEBUG 20")
while currentNode != startNode:
# print("DEBUG 21")
# print("currentNode: " + str(currentNode))
path.append(currentNode)
currentNode = currentNode.parent
# print("DEBUD 22 --- currentNode = " + str(currentNode))
return path #if path else [startNode]
def getNeighbours(self,node,foodNode,maze):
neighbours = []
validdirs = self.getValidDirs(node,maze)
node_for_diag = None
for dir in validdirs:
coord = self.add((node.x,node.y),dir)
newnode = Node(coord, dir=dir,gCost=node.gCost+1,parent=node)
if dir == node.dir:
node_for_diag = newnode
newnode.hCost = self.pathlen((newnode.x,newnode.y),(foodNode.x,foodNode.y))
neighbours.append(newnode)
if node.dir in validdirs:
for diagDir in self.getValidDirsDiag(node,maze):
coord = self.add((node.x,node.y),diagDir)
if node.dir == up or node.dir == down:
if diagDir == (-1,-1) or diagDir == (-1,1):
dir = left
else:
dir = right
else:
if diagDir == (-1,-1) or diagDir == (1,-1):
dir = up
else:
dir = down
if dir in validdirs:
newnode = Node(coord, dir=dir,gCost=node.gCost+2,parent=node_for_diag)
newnode.hCost = self.pathlen((newnode.x,newnode.y),(foodNode.x,foodNode.y))
neighbours.append(newnode)
return neighbours
def getValidDirs(self,node,maze):
position=node.get_pos()
dirs = []
complement=[(up,down),(down,up),(right,left),(left,right)]
invaliddir=[x for (x,y) in complement if y==node.dir]
validdir = [dir for dir in directions if not ( dir in invaliddir )]
return [dir for dir in validdir if not self.add(position,dir) in maze.obstacles and not self.add(position,dir) in maze.playerpos] #verificar se não vai contra o corpo
def getValidDirsDiag(self,node,maze):
position=node.get_pos()
diagComplement =[(up,(-1,1)), (up, (1,1)) , (down, (1,-1)), (down, (-1,-1)), (left, (1, 1)), (left, (1, -1)), (right, (-1,1)), (right, (-1, -1))]
diagDirections = [(1,1),(1,-1),(-1,1),(-1,-1)]
invaliddir = [y for (x,y) in diagComplement if x == node.dir]
validdir = [dir for dir in diagDirections if not ( dir in invaliddir )]
return [dir for dir in validdir if not self.add(position,dir) in maze.obstacles and not self.add(position,dir) in maze.playerpos]
class Node:
def __init__(self,coord,dir=(0,0), gCost=0, hCost=0, parent=None):
self.x=coord[0]
self.y=coord[1]
self.dir=dir
self.parent=parent
self.gCost=gCost
self.hCost=hCost
def __str__(self):
return str((self.x,self.y))
def __eq__(self,other):
return self.x==other.x and self.y==other.y
def fCost(self):
return self.hCost+self.gCost
def get_pos(self):
return (self.x, self.y)