-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
386 lines (334 loc) · 16 KB
/
server.py
File metadata and controls
386 lines (334 loc) · 16 KB
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
import os
import random
import json
import numpy
import cherrypy
from termcolor import colored
import calculations
import pathfind2
import floodfill
"""
Simple code for Danger Noodle. Very messy and unorganized. Originally created from the Battlesnake starter code
"""
turn = 0
hunt = False
aggroRange = 2
murdercd = 0
class Battlesnake(object):
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self):
return {
"apiversion": "1",
"author": "Lawrence Zhang",
"color": "#5E8773",
"head": "tiger-king",
"tail": "hook",
}
@cherrypy.expose
@cherrypy.tools.json_in()
def start(self):
global turn
turn = 0
data = cherrypy.request.json
xSize = data['board']['height']
ySize = data['board']['width']
# boardData = [ [0] * xSize for _ in range(ySize)]
#print(numpy.matrix(boardData))
return "ok"
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def move(self):
global turn
print(colored("Turn count is: " + str(turn),"magenta"))
turn+=1
#TDefine and init the size and locs of some stuff
#--------------------------------------
data = cherrypy.request.json
xSize = data['board']['height']
ySize = data['board']['width']
boardData = [ [0] * xSize for _ in range(ySize)]
snakeList = data['board']['snakes']
xHead = data['you']['head']['x']
yHead = ySize - 1 - data['you']['head']['y']
#-------------------------------------
#print("xHead is")
#print(xHead)
#print("yHead is")
#print(yHead)
Food = []
for i in data['board']['food']:
Food.append((xSize - 1 - i['y'],i['x']))
#Begin initializing and loading board data
tuples = []
for x in snakeList:
body = x['body']
# head = x['head']
for y in body[:-1]:
boardData[ySize - 1 - y['y']][y['x']] = 1
for x in snakeList:
head = x['head']
if x['name'] != data['you']['name']:
tuples.append((ySize - 1 - head['y'] - 1,head['x'])) # 1 above
tuples.append((ySize - 1 - head['y'] + 1,head['x'])) # below
tuples.append((ySize - 1 - head['y'],head['x'] - 1)) # left
tuples.append((ySize - 1 - head['y'],head['x'] + 1)) # right
# ----------------------------------------------
#
# FIGHT OR FLIGHT MODULE
# Determines if the snake should avoid the closet enemy head,
# or aim towards it to kill it.
# Code is messy as path2head is broken
# TODO: FIX path2head
#
#-------------------------------------------------
global hunt
global aggroRange
global murdercd
if murdercd >= 3:
murdercd = 5
if tuples:
closestSnake = calculations.getClosestSnake(ySize,xHead,yHead,snakeList)
sH = calculations.path2head(yHead,xHead,tuples)
if len(sH) > 1 and closestSnake['length'] >= data['you']['length']:
print("SH IS: ")
print(sH)
print("ENEMY HEAD LOC: ")
hunt = False
if sH[0][0] < ySize and sH[0][0] > -1 and sH[0][1] > -1 and sH[0][1] < xSize:
print("Added block at " + str(sH[0][0]) + str(sH[0][1]))
boardData[ sH[0][0] ] [ sH[0][1] ] = 1 #First closest area
if sH[1][0] < ySize and sH[1][0] > -1 and sH[1][1] > -1 and sH[1][1] < xSize:
print("Added block at " + str(sH[1][0]) + str(sH[1][1]))
boardData[ sH[1][0] ] [ sH[1][1] ] = 1 #Second closest
if sH[2][0] < ySize and sH[2][0] > -1 and sH[2][1] > -1 and sH[2][1] < xSize:
print("Added block at " + str(sH[2][0]) + str(sH[2][1]))
boardData[ sH[2][0] ] [ sH[2][1] ] = 1 #Third closest area
if sH[3][0] < ySize and sH[3][0] > -1 and sH[3][1] > -1 and sH[3][1] < xSize:
print("Added block at " + str(sH[3][0]) + str(sH[3][1]))
boardData[ sH[3][0] ] [ sH[3][1] ] = 1 #Fourth closest area
# if sH[3][0] < ySize and sH[3][0] > -1 and sH[3][1] > -1 and sH[3][1] < xSize:
# print("Added block at " + str(sH[3][0]) + str(sH[3][1]))
# boardData[ sH[3][0] ] [ sH[3][1] ] = 1 #Fourth closest area
elif len(sH) > 1 and closestSnake['length'] < data['you']['length'] and calculations.simpleDist((yHead,xHead),(ySize - 1 - closestSnake['head']['y'],closestSnake['head']['x'])) < aggroRange:
hunt = True
if sH[0][0] < ySize - 1 and sH[0][0] - 1 > 0 and sH[0][1] > -1 and sH[0][1] < xSize and boardData[ sH[0][0] ] [ sH[0][1] ] != 1:
print("Added TARGET at " + str(sH[0][0]) + str(sH[0][1]))
Food.append((sH[0][0],sH[0][1]))
if sH[1][0] < ySize - 1 and sH[1][0] - 1 > 0 and sH[1][1] > -1 and sH[1][1] < xSize and boardData[ sH[1][0] ] [ sH[1][1] ] != 1:
print("Added TARGET at " + str(sH[1][0]) + str(sH[1][1]))
Food.append((sH[1][0],sH[1][1]))
if sH[2][0] < ySize - 1 and sH[2][0] - 1 > 0 and sH[2][1] > -1 and sH[2][1] < xSize and boardData[ sH[2][0] ] [ sH[2][1] ] != 1:
print("Added TARGET at " + str(sH[2][0]) + str(sH[2][1]))
Food.append((sH[2][0],sH[2][1]))
# if sH[3][0] < ySize - 1 and sH[3][0] - 1 > 0 and sH[3][1] > -1 and sH[3][1] < xSize and boardData[ sH[3][0] ] [ sH[3][1] ] != 1:
# print("Added TARGET at " + str(sH[3][0]) + str(sH[3][1]))
# Food.append((sH[3][0],sH[3][1]))
#boardData[xSize - 1 - head['y']][head['x']] = 1
print("Distance to nearest is: ")
print(calculations.simpleDist((yHead,xHead),(ySize - 1 - closestSnake['head']['y'],closestSnake['head']['x'])))
#print("-----------\nSTATE UPDATED:")
#print(numpy.matrix(boardData))
#print("-----------")
#---------------------
#print("Size is: ")
#print(size)
# Choose a random direction to move in.
path = []
if data['you']['health'] > 20 and hunt is True and data['you']['length'] > 4 and murdercd < 3:
print(colored("Aggro Hunt","red"))
murdercd +=2
path = pathfind2.astar(boardData, (yHead,xHead), calculations.distanceSort(xHead,yHead,Food)[0])
elif data['you']['health'] > 30 and data['you']['length'] > 5:
print(colored("Survive","red"))
path = pathfind2.astar(boardData, (yHead,xHead),(ySize - 1 - data['you']['body'][-1]['y'],data['you']['body'][-1]['x']))
else:
print(colored("Seek food","yellow"))
path = pathfind2.astar(boardData, (yHead,xHead), calculations.distanceSort(xHead,yHead,Food)[0])
moves = []
if path:
for step in path:
boardData[step[0]][step[1]] = 2
for row in boardData:
line = []
for col in row:
if col == 1:
line.append(colored("□","green"))
elif col == 0:
line.append(colored("□","white"))
elif col == 2:
line.append(colored("□","red"))
print("".join(line))
print(path)
else:
print(boardData)
#print(colored("Path is: ","cyan"))
#print(colored(path,'yellow'))
#print("head: " + str(xHead) + " " + str(yHead))
#print(colored("TARGET MOVE:","cyan"))
#print(path[1])
#print(colored("MOVES LIST: ","cyan"))
moveClone = ['null']
if path is not None:
target = path[1]
if target[1] > xHead:
#print(colored("right","yellow"))
moves.append('right')
if target[1] < xHead:
#print(colored("left","yellow"))
moves.append('left')
if target[0] > yHead:
#print(colored("down","yellow"))
moves.append('down')
if target[0] < yHead:
#print(colored("up","yellow"))
moves.append('up')
if 'right' not in moves:
moves.append('right')
if 'left' not in moves:
moves.append('left')
if 'down' not in moves:
moves.append('down')
if 'up' not in moves:
moves.append('up')
moveClone = moves[:]
else:
print("ERROR: - PATHFIND RETURN NULL -")
print("RUNNING SURVIVAL MODE UNTIL PATHFIND RECONNECTS")
moves = ['left','right','down','up']
print(numpy.matrix(boardData))
moveClone = moves[:]
#print("Move removals: ")
#print(xHead)
#print(yHead)
print("Precheck: " + str(moves))
if moves:
moveCheck(xHead,yHead,xSize,ySize,boardData,moves)
print("CLONE ES: " + str(moveClone))
if not moves:
print("ERROR: - PATHFIND RETURN WRONG SOLUTION -")
print("RUNNING SURVIVAL MODE UNTIL PATHFIND RECONNECTS")
moves = moveClone[:]
if 'left' in moves:
print("Checking Left")
if (xHead - 1 < 0) or (boardData[yHead][xHead - 1] == 1):
#if boardData[yHead][xHead - 1] == 1 or xHead - 1 < 0:
print(colored("Illegal move: LEFT removed","red"))
moves.remove('left')
if 'right' in moves:
print("Checking Right")
if xHead + 1 > (xSize - 1) or boardData[yHead][xHead + 1] == 1:
#if boardData[yHead][xHead + 1] == 1 or xHead + 1 > xSize:
print(colored("Illegal move: RIGHT removed","red"))
moves.remove('right')
if 'up' in moves:
print("Checking Up")
if yHead - 1 < 0 or boardData[yHead - 1][xHead] == 1:
#if boardData[yHead - 1][xHead] == 1 or yHead - 1 < 0:
print(colored("Illegal move: UP removed","red"))
moves.remove('up')
if 'down' in moves:
print("Checking Down")
if yHead + 1 > (ySize - 1) or boardData[yHead + 1][xHead] == 1:
#if boardData[yHead + 1][xHead] == 1 or yHead + 1 > ySize:
print(colored("Illegal move: DOWN removed","red"))
moves.remove('down')
if not moves:
print("Survival mode check failed. Running gamble")
moves = moveClone[:]
if sH[0][0] < ySize - 1 and sH[0][0] -1 > -1 and sH[0][1] > -1 and sH[0][1] < xSize:
print("Removing block at " + str(sH[0][0]) + str(sH[0][1]))
boardData[ sH[0][0] ] [ sH[0][1] ] = 0 #First closest area
if sH[1][0] < ySize and sH[1][0] -1 > -1 and sH[1][1] > -1 and sH[1][1] < xSize:
print("Removing block at " + str(sH[1][0]) + str(sH[1][1]))
boardData[ sH[1][0] ] [ sH[1][1] ] = 0 #Second closest
if sH[2][0] < ySize and sH[2][0] - 1 > -1 and sH[2][1] > -1 and sH[2][1] < xSize:
print("Removing block at " + str(sH[2][0]) + str(sH[2][1]))
boardData[ sH[2][0] ] [ sH[2][1] ] = 0 #Third closest area
if sH[3][0] < ySize and sH[3][0] -1 > -1 and sH[3][1] > -1 and sH[3][1] < xSize:
print("Removing block at " + str(sH[3][0]) + str(sH[3][1]))
boardData[ sH[3][0] ] [ sH[3][1] ] = 0 #Fourth closest area
for x in snakeList:
body = x['body']
# head = x['head']
for y in body[:-1]:
boardData[ySize - 1 - y['y']][y['x']] = 1
if 'left' in moves:
print("Checking Left")
if (xHead - 1 < 0) or (boardData[yHead][xHead - 1] == 1):
#if boardData[yHead][xHead - 1] == 1 or xHead - 1 < 0:
print(colored("Illegal move: LEFT removed","red"))
moves.remove('left')
if 'right' in moves:
print("Checking Right")
if xHead + 1 > (xSize - 1) or boardData[yHead][xHead + 1] == 1:
#if boardData[yHead][xHead + 1] == 1 or xHead + 1 > xSize:
print(colored("Illegal move: RIGHT removed","red"))
moves.remove('right')
if 'up' in moves:
print("Checking Up")
if yHead - 1 < 0 or boardData[yHead - 1][xHead] == 1:
#if boardData[yHead - 1][xHead] == 1 or yHead - 1 < 0:
print(colored("Illegal move: UP removed","red"))
moves.remove('up')
if 'down' in moves:
print("Checking Down")
if yHead + 1 > (ySize - 1) or boardData[yHead + 1][xHead] == 1:
#if boardData[yHead + 1][xHead] == 1 or yHead + 1 > ySize:
print(colored("Illegal move: DOWN removed","red"))
moves.remove('down')
murdercd -=1
#print(moves).
if moves:
print("Debug list: xHead: " + str(xHead) + " yHead: " + str(yHead) + "Tail x: " + str(data['you']['body'][-1]['x']) + "Tail y: " + str(ySize - 1 - data['you']['body'][-1]['y']))
print("Path is: " + str(path))
print("Possible moves are: " + str(moves))
print("Target: " + str(moves[0]))
return {"move": moves[0]}
#.def safety(clone,x,y,num): d
def moveCheck(xHead,yHead,xSize,ySize,boardData,moves):
if 'left' in moves:
print("Checking Left")
if (xHead - 1 < 0) or (boardData[yHead][xHead - 1] == 1) or floodfill.safety(xSize,ySize,yHead,xHead - 1,boardData,yHead,xHead,5):
#if boardData[yHead][xHead - 1] == 1 or xHead - 1 < 0:
print(colored("Illegal move: LEFT removed","red"))
moves.remove('left')
if 'right' in moves:
print("Checking Right")
if xHead + 1 > (xSize - 1) or boardData[yHead][xHead + 1] == 1 or floodfill.safety(xSize,ySize,yHead,xHead + 1,boardData,yHead,xHead,5):
#if boardData[yHead][xHead + 1] == 1 or xHead + 1 > xSize:
print(colored("Illegal move: RIGHT removed","red"))
moves.remove('right')
if 'up' in moves:
print("Checking Up")
if yHead - 1 < 0 or boardData[yHead - 1][xHead] == 1 or floodfill.safety(xSize,ySize,yHead - 1,xHead,boardData,yHead,xHead,5):
#if boardData[yHead - 1][xHead] == 1 or yHead - 1 < 0:
print(colored("Illegal move: UP removed","red"))
moves.remove('up')
if 'down' in moves:
print("Checking Down")
if yHead + 1 > (ySize - 1) or boardData[yHead + 1][xHead] == 1 or floodfill.safety(xSize,ySize,yHead + 1,xHead,boardData,yHead,xHead,5):
#if boardData[yHead + 1][xHead] == 1 or yHead + 1 > ySize:
print(colored("Illegal move: DOWN removed","red"))
moves.remove('down')
return
@cherrypy.expose
@cherrypy.tools.json_in()
def end(self):
# data = cherrypy.request.json
# print(data)
print(" ")
print(" ")
print("END")
global turn
turn = 0
return "ok"
if __name__ == "__main__":
server = Battlesnake()
cherrypy.config.update({"server.socket_host": "0.0.0.0"})
cherrypy.config.update(
{"server.socket_port": int(os.environ.get("PORT", "8080")),}
)
print("Starting Battlesnake Server...")
cherrypy.quickstart(server)