-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.js
535 lines (452 loc) · 15.8 KB
/
snake.js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
const aStar = require('./a-star');
const SEARCH_TIMEOUT = 50;
const COST_HEAVY = 1000;
const COST_MODERATE = 250;
const COST_LIGHT = 100;
const STARVING = 15;
const HUNGRY = 50;
let state = {};
// Handle start requests
module.exports.start = function(req, res) {
state = req.body;
// Response data
var data = {
color: "#FF1493",
name: "RUTHLESS",
head_url: "https://raw.githubusercontent.com/stewartlord/battlesnake/master/head.png",
taunt: "START",
};
return res.json(data);
}
// Handle move requests
module.exports.move = function(req, res) {
console.log('-');
state = req.body;
let ourSnake = getSnake(state);
let ourHead = getHeadNode(ourSnake);
let ourTail = getTailNode(ourSnake);
let result;
let results = [];
// compute paths to food
let foodPaths = [];
for (let i = 0; i < state.food.data.length; i++) {
// enable reckless search when STARVING
result = aStarSearch(state, ourHead, [state.food.data[i]], ourSnake.health <= STARVING);
if (result.status != 'success') continue;
result.goal = 'FOOD';
foodPaths.push(result);
}
// eliminate unsafe food paths
results = foodPaths.filter((result) => {
// eliminate paths we can't reach in time
if (result.path.length > ourSnake.health) return false;
// eliminate food close to the head of a bigger enemy snake
if (enemyDistance(state, result.path[result.path.length - 1]) < 3) return false;
// eliminate paths we can't fit into (compute space size pessimistically)
if (getSpaceSize(state, result.path[1], true) < ourSnake.body.data.length) return false;
return true;
});
// determine closest food
let closestFood = results.length && results.reduce((closest, current) => {
return Math.min(current.path.length, closest);
}, Number.MAX_SAFE_INTEGER);
// we want to the be closest snake to at least one piece of food
// determine how close we are vs. how close our enemies are
let foodDistances = [];
for (let i = 0; i < results.length; i++) {
result = results[i];
let foodNode = result.path[result.path.length - 1];
let ourDistance = distance(ourHead, foodNode);
let otherDistance = enemyDistance(state, foodNode);
foodDistances.push({
foodNode,
ourDistance,
enemyDistance: otherDistance,
advantage: otherDistance - ourDistance
})
}
let foodAdvantages = foodDistances.slice().sort((a, b) => b.advantage - a.advantage);
let foodOpportunities = foodDistances.slice().sort((a, b) => b.enemyDistance - a.enemyDistance);
let foodAdvantage = foodAdvantages.length && foodAdvantages[0];
let foodOpportunity = foodOpportunities.length && foodOpportunities[0];
// 'must eat' if STARVING or steps to food consume >=60% of health
// 'should eat' if HUNGRY or steps to food consume >=30% of health
// 'chase food' if food advantage is < 5
let safeFood = results.length > 0;
let mustEat = ourSnake.health <= STARVING || closestFood >= (ourSnake.health * .6);
let shouldEat = safeFood && (ourSnake.health <= HUNGRY || closestFood >= (ourSnake.health * .3));
let chaseFood = safeFood && foodAdvantage && foodAdvantage.advantage < 5;
console.log('MUST/SHOULD/CHASE', mustEat, shouldEat, chaseFood);
// if we must eat, but can't reach food, re-introduce unsafe paths
let reachableFood = results.find(result => result.path.length < ourSnake.health);
if (mustEat && !reachableFood) {
results = foodPaths.slice();
}
// if eating is optional, seek tail nodes
if (!mustEat || !results.length) {
let tailTargets = goodNeighbors(state, ourTail);
if (!isGrowing(ourSnake)) tailTargets.push(ourTail);
for (let i = 0; i < tailTargets.length; i++) {
result = aStarSearch(state, ourHead, [tailTargets[i]]);
if (result.status != 'success') continue;
if (result.path.length === 1) continue;
result.goal = 'TAIL';
results.push(result);
}
}
// if eating is optional, consider head shots
if (!mustEat) {
try {
let headShots = goodNeighbors(state, ourHead, true);
for (let i = 0; i < headShots.length; i++) {
// can only head shot smaller snakes
let smallerSnake = isPossibleNextMove(state, getSmallerSnakes(state), headShots[i]);
if (!smallerSnake) continue;
// favor our guess at their next move
let guessNext = guessNextMove(state, smallerSnake);
results.push({
goal: 'HEADSHOT',
path: [ourHead, headShots[i]],
cost: guessNext && isSameNode(headShots[i], guessNext) ? 0 : 1
})
}
} catch (error) {
// this code was added game day, don't trust it ^^
}
}
// adjust the cost of paths
for (let i = 0; i < results.length; i++) {
let result = results[i];
let path = result.path;
let endNode = path[path.length - 1];
let startNode = path[1];
// heavily if we would starve en-route
if (result.path.length > ourSnake.health) {
result.cost += COST_HEAVY;
}
// heavily if end point has no path back to our tail
if (!hasPathToTail(state, endNode, ourSnake)) {
result.cost += COST_HEAVY;
}
// heavily/moderately/lightly if not a food path and we must-eat/should-eat/chase-food
if (result.goal !== 'FOOD') {
if (mustEat) {
result.cost += COST_HEAVY;
} else if (shouldEat) {
result.cost += COST_MODERATE;
} else if (chaseFood) {
result.cost += COST_LIGHT;
}
}
// lightly if a food path and we should not be eating
if (result.goal === 'FOOD' && (!shouldEat && !mustEat && !chaseFood)) {
result.cost += COST_LIGHT;
}
// lightly if: food path, multiple food paths, not our advantage and not most available
if (result.goal === 'FOOD'
&& state.food.data.length > 1
&& foodAdvantage
&& (getNodeHash(endNode) !== getNodeHash(foodAdvantage.foodNode) || foodAdvantage.advantage < 1)
&& foodOpportunity
&& getNodeHash(endNode) !== getNodeHash(foodOpportunity.foodNode)
) {
result.cost += COST_LIGHT;
}
}
// if we found paths to goals, pick cheapest one
if (results.length) {
results.sort((a, b) => {
return a.cost - b.cost;
});
results.forEach(result => console.log(result.goal, result.cost, result.path.length));
return moveResponse(
res,
direction(ourHead, results[0].path[1]),
'A* BEST PATH TO ' + results[0].goal
);
}
// no best moves, pick the direction that has the most open space
// first be pessimistic: avoid nodes next to enemy heads and spaces too small for us
// if that fails, be optimistic: include nodes next to enemy heads and small spaces
let moves = getSpaciousMoves(state, ourSnake, true);
moves = moves.length ? moves : getSpaciousMoves(state, ourSnake);
moves.sort((a, b) => {
// avoid nodes bigger enemy snakes might move into
if (a.spaceSize === b.spaceSize && a.isNextMove !== b.isNextMove) {
return a.isNextMove - b.isNextMove;
}
// don't cut off escape routes
if (a.spaceSize === b.spaceSize) {
return a.wallCost - b.wallCost;
}
return b.spaceSize - a.spaceSize;
});
if (moves.length) {
return moveResponse(
res,
direction(ourHead, moves[0].node),
'NO PATH TO GOAL, LARGEST SPACE'
);
}
// no valid moves
return moveResponse(res, 'up', 'FML');
}
function getSpaciousMoves(state, ourSnake, pessimistic) {
let moves = [];
let ourHead = getHeadNode(ourSnake);
let headNeighbors = pessimistic
? goodNeighbors(state, ourHead, true)
: validNeighbors(state, ourHead);
for (let i = 0; i < headNeighbors.length; i++) {
let neighbor = headNeighbors[i];
let spaceSize = getSpaceSize(state, neighbor, pessimistic);
if (pessimistic && spaceSize < ourSnake.body.data.length) continue;
moves.push({
node: neighbor,
direction: direction(ourHead, neighbor),
spaceSize: spaceSize,
wallCost: getWallCost(state, neighbor),
isNextMove: isPossibleNextMove(state, getBiggerSnakes(state), neighbor)
});
}
return moves;
}
function moveResponse(res, move, taunt) {
taunt = taunt + ' ' + move;
console.log(taunt);
return res.json({move, taunt});
}
function enemyDistance(state, node) {
let enemySnakes = getOtherSnakes(state);
return enemySnakes.reduce((closest, current) => {
let headNode = getHeadNode(current);
return Math.min(distance(node, headNode), closest);
}, Number.MAX_SAFE_INTEGER);
}
function getSpaceSize(state, node, pessimistic) {
let validNodes = [node];
let seenNodes = {};
seenNodes[getNodeHash(node)] = true;
for (let i = 0; i < validNodes.length; i++) {
// compute distance from current node to start node and subtract it from tails
let tailTrim = distance(node, validNodes[i]);
let neighbors = pessimistic
? goodNeighbors(state, validNodes[i], false, tailTrim)
: validNeighbors(state, validNodes[i], tailTrim);
for (let j = 0; j < neighbors.length; j++) {
if (!seenNodes[getNodeHash(neighbors[j])]) {
seenNodes[getNodeHash(neighbors[j])] = true;
validNodes.push(neighbors[j]);
}
}
}
return validNodes.length;
}
function hasPathToTail(state, startNode, snake) {
let snakeTail = getTailNode(snake);
let result = aStarSearch(state, startNode, validNeighbors(state, snakeTail));
return result.status == 'success';
}
function getHeadNode(snake) {
return snake.body.data.slice(0,1)[0];
}
function getTailNode(snake) {
return snake.body.data.slice(-1)[0];
}
function getSnake(state, snakeId) {
if (!snakeId) snakeId = state.you.id;
for (let snake of state.snakes.data) {
if (snake.id == snakeId) return snake;
}
}
function getOtherSnakes(state, snakeId) {
if (!snakeId) snakeId = state.you.id;
return state.snakes.data.filter((snake) => {
return snake.id != snakeId;
});
}
function getBiggerSnakes(state, snakeId) {
if (!snakeId) snakeId = state.you.id;
let subjectSnake = getSnake(state, snakeId);
return state.snakes.data.filter((snake) => {
return snake.id != snakeId && snake.body.data.length >= subjectSnake.body.data.length;
});
}
function getSmallerSnakes(state, snakeId) {
if (!snakeId) snakeId = state.you.id;
let subjectSnake = getSnake(state, snakeId);
return state.snakes.data.filter((snake) => {
return snake.id != snakeId && snake.body.data.length < subjectSnake.body.data.length;
});
}
function isSameNode(a, b) {
return a.x === b.x && a.y === b.y;
}
function isInNodes(node, nodes, tailTrim) {
tailTrim = tailTrim > 0 ? tailTrim : 0;
for (let i = 0; i < (nodes.length - tailTrim); i++) {
if (node.x === nodes[i].x && node.y === nodes[i].y) return true;
}
return false;
}
function isAdjacent(a, b) {
if (a.x == b.x) {
return a.y == b.y-1 || a.y == b.y+1
} else if (a.y == b.y) {
return a.x == b.x-1 || a.x == b.x+1
}
return false;
}
function isSnake(state, node, tailTrim) {
// @todo don't tail trim snakes that are tail chasing
for (let i = 0; i < state.snakes.data.length; i++) {
if (isInNodes(node, state.snakes.data[i].body.data, tailTrim)) {
return true;
}
}
return false;
}
function isFood(state, node) {
return isInNodes(node, state.food.data);
}
function isWall(state, node) {
return node.x < 0 || node.x >= state.width || node.y < 0 || node.y >= state.height;
}
function distance(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
function neighbors(node) {
return [
{x: node.x - 1, y: node.y},
{x: node.x + 1, y: node.y},
{x: node.x, y: node.y - 1},
{x: node.x, y: node.y + 1}
];
}
function validNeighbors(state, node, tailTrim) {
return neighbors(node).filter((node) => {
// walls are not valid
if (isWall(state, node)) return false;
// don't consider occupied nodes unless they are moving tails
if (isSnake(state, node, tailTrim) && !isMovingTail(state, node)) return false;
// looks valid
return true;
});
}
function goodNeighbors(state, node, headShot, tailTrim) {
let otherSnakes = headShot ? getBiggerSnakes(state) : getOtherSnakes(state);
return validNeighbors(state, node, tailTrim).filter((node) => {
// don't consider nodes adjacent to the head of another snake
return !isPossibleNextMove(state, otherSnakes, node);
});
}
function isMovingTail(state, node) {
for (let i = 0; i < state.snakes.data.length; i++) {
let body = state.snakes.data[i].body.data;
// if it's not the tail node, consider next snake
if (!isSameNode(node, body[body.length - 1])) continue;
// if snake is growing, tail won't move
if (isGrowing(state.snakes.data[i])) return false;
// must be a moving tail
return true;
}
return false;
}
function isGrowing(snake) {
let body = snake.body.data;
return body.length > 1 && isSameNode(body[body.length - 1], body[body.length - 2]);
}
function isPossibleNextMove(state, snakes, node) {
let filtered = snakes.filter((snake) => {
return isInNodes(node, neighbors(getHeadNode(snake)));
});
return filtered.length ? filtered[0] : false;
}
function getProximityToSnakes(state, snakes, node) {
let proximity = 0;
let quarterBoard = (Math.min(state.width, state.height) - 1) / 4;
for (let i = 0; i < snakes.length; i++) {
if (snakes[i].id === state.you.id) continue;
let headNode = getHeadNode(snakes[i]);
let gap = distance(headNode, node);
// insignificant proximity if > 1/4 of the board away
if (gap >= quarterBoard) continue;
proximity += (quarterBoard - gap) * 10
}
return proximity;
}
function heuristic(state, node) {
// cost goes up if node is close to a wall because that limits escape routes
let cost = getWallCost(state, node);
// cost goes up if node is close to another snake
cost += getProximityToSnakes(state, getOtherSnakes(state), node);
return cost;
}
function direction(fromNode, toNode) {
if (fromNode.y > toNode.y) return 'up';
if (fromNode.y < toNode.y) return 'down';
if (fromNode.x > toNode.x) return 'left';
if (fromNode.x < toNode.x) return 'right';
}
function aStarSearch(state, startNode, targets, reckless) {
let options = {
start: startNode,
isEnd: (node) => isInNodes(node, targets),
neighbor: (node, path) => {
return reckless
? validNeighbors(state, node, path.length)
: goodNeighbors(state, node, node === startNode, path.length);
},
distance: distance,
heuristic: (node) => reckless ? 0 : heuristic(state, node),
hash: getNodeHash,
timeout: SEARCH_TIMEOUT
}
return aStar(options);
}
function getNodeHash(node) {
return `${node.x},${node.y}`
}
function getWallCost(state, node) {
let halfWidth = (state.width - 1) / 2;
let halfHeight = (state.height - 1) / 2;
let deviation = [
Math.abs(node.x - halfWidth) / halfWidth,
Math.abs(node.y - halfHeight) / halfHeight
];
return Math.round(Math.max(...deviation) * ((halfWidth + halfHeight) / 4));
}
function getOccupiedNodes(snakes) {
let nodes = [];
for (let snake of snakes) {
for (let i = 0; i < snake.body.data.length; i++) {
nodes.push(snake.body.data[i]);
}
}
return nodes;
}
function snakeDirection(snake) {
if (snake.body.data.length < 2) return;
let headNode = getHeadNode(snake);
let nextNode = snake.body.data[1];
return direction(nextNode, headNode);
}
function guessNextMove(state, snake) {
let headNode = getHeadNode(snake);
let possible = validNeighbors(state, headNode);
let nextInLine;
let currentDirection = snakeDirection(snake);
if (currentDirection === 'up') {
nextInLine = { x: headNode.x, y: headNode.y - 1 };
} else if (currentDirection === 'down') {
nextInLine = { x: headNode.x, y: headNode.y + 1 };
} else if (currentDirection === 'left') {
nextInLine = { x: headNode.x - 1, y: headNode.y };
} else if (currentDirection === 'right') {
nextInLine = { x: headNode.x + 1, y: headNode.y };
}
if (isInNodes(nextInLine, possible)) {
return nextInLine;
}
return possible.length ? possible[0] : null;
}